Setting Up Git/GitHub On Linux

PART I: Install and Configure Git

  1. Go to https://git-scm.com/download/linux and find the command to install git to your given Linux distribution.
    • I'm doing this on openSUSE, so I entered sudo zypper install git.
    • NOTE: Mac and Windows users can view the install guides for their platforms here.
  2. Set your identity (name and email address) for your commits:
    • git config --global user.name "John Doe"
    • git config --global user.email johndoe@example.com
  3. Set your text editor. Enter one of the following:
    • git config --global core.editor emacs
    • git config --global core.editor vim
    • git config --global core.editor "atom --wait"
  4. Check your settings using git config --list.
    • Example output:
      • user.name=John Doe
        user.email=johndoe@example.com
        core.editor=vim
        color.status=auto
        color.branch=auto
        color.interactive=auto
        color.diff=auto
  5. { HELP! } If at any time you run into problems or aren't sure how to proceed, just ask for help. For example, you can try:
    • git help config
    • git help <command/verb>
    • git <command/verb> --help
    • man git-<command/verb>
    • read through the Git Documentation
    • ask for help on the #git or #github channels on the Freenode IRC server.

PART II: Begin Using Git

  1. Move into a local project directory where you have some files for which you would like to track changes. I'll be using a sample.pl (Perl) program in my /home/adam/bin/gitEx directory for the purposes of illustration:

    • Initially, my project directory contains only one file called sample.pl.
    • At this point my sample.pl file contains the following code:
      • #!/usr/bin/perl
        print "Hello, World!";
        <STDIN>;
  2. Run git init after moving into the project directory. This creates a new subdirectory named ".git" that contains all your basic repository management files, such as 'index', which is used to store changes that have been staged.

    • You should see something like this as output:
      • Initialized empty Git repository in /home/adam/bin/gitEx/.git/
    • You will now have a .git subdirectory which contains the following files and subdirectories: branches, config, description, HEAD, hooks, info, objects, refs.
  3. It is time to begin tracking individual files. Type git add <filename> on any number of files in your project directory (where you ran git init just now).

    • My example: git add sample.pl
  4. After adding the files to be tracked, we must make an initial commit. Run the following command with your own description:

    • git commit -m "initial Sample.pl project version"
  5. Every time you modify a file you are checking, if you want the changes to be tracked and saved properly by git, you must run git add <filename> again.

  6. Before making additional commits to your Master branch, do git status to check that you don't have any untracked changes or any new files which you forgot to add or "stage" for the commit. You may need to run git add <filename> again if you have recent changes.


At this point I recommend pausing to read the Git book section on Recording Changes to the Repository. This covers additional topics which may be useful to beginners, such as:

  • How to use the .gitignore file.
  • How to interpret git status -s (shortened output)
  • The git diff command to compare file versions.
  • Using the "-a" flag, as in git commit -a -m '<description>', to automatically stage new or updated files.

Now let's talk about getting things connected to GitHub...


[ TO BE CONTINUED... ]

While you wait for updates from Kernel Mastery, consider trying out Code School's Free Git Course*, created in collaboration with GitHub.

* Sign-up required; free as in beer.