Git: Quick How To

Git: Quick How To

(updated 1/14/2013)

Initializing git

To get credit for a commit, a git user must have a local account associated with git. To do this, we must set up the variables and :

$    git config --global “firstName lastName”
$    git config --global “

We only do this once.

Creating a repository

To create a repository, go to the desired directory and type:

$    git init

We can now add files in the desired directory.
On the other hand,

$    git init projectName

will create a repository with projectName as the directory name. cd into this directory and add files to be tracked inside.

Tracking files

There are two parts in tracking files:


First, add the desired files to be included in the index of files to be staged

$    git add filename1 filename2

where filename1 and filename2 are the text files to be staged. We can also add all the files in the current directory by executing:

$    git add .

Note the dot above


Second, record the modifications. This will permanently log the change from the last “commit.” To record  the changes that have occurred since the last commit, execute:

$    git commit -m “describe changes here”

The -m option provides us the ability to include messages along with the commit. This is important since this is how we document the changes that happened in the particular commit we are about to record.

When modifying files, one must add them to the list of files to be logged every after commit. To skip the “staging area,” one can execute:

$    git commit -a

This command does both the staging and committing in one go. Executing this command opens up a text editor (usually vim) to allow us to document the changes. This option automatically stages all modified files (excluding newly created and untracked files).


Suppose we want to create another version of a program. Say we have a working version which we’ll call version 1 and we want to implement a new function or improve an existing one. Instead of creating another folder and copying all contents of version 1, we can instead “branch out” from the existing version. We will call this new branch version 2. To do so, first execute

$    git branch

If we haven’t made any checkouts or branches, most likely we will have only one branch. This branch is called master by default. An asterisk to the left of the branch name indicates that this particular branch is the current working branch (much like the current working directory).

To branch out from a commit, we must first find out which version to revert to. To do so, execute:

$    git log

This command will output all commits starting from the most recent commit. To limit the number of commits shown starting from the most recent, add the option -n, where n is the number of commits to be shown.

$    git log -2

The above command shows the 2 most recent commits.
To branch to a particular commit, execute:

$    git checkout -b newBranchName CHECKSUMVALUE

where CHECKSUMVALUE is the value of the checksum and newBranchName is the name we want for this newly created branch. In our case, we want newBranchName = version_2. We can also type in the first n digits of the checksum where n > 3 for the CHECKSUMVALUE.

The checksum is the string after the word “commit” in the log file.

This will create a new branch. In this case, the root of this new branch will be from CHECKSUMVALUE. All changes and commits made in this branch  will not affect the previous branch or any branches.
The git checkout command is use to change focus of our working branch (much like how cd changes the directory). The -b option adds the option of creating a branch.
Now execute:

$    git branch

We should now see newBranchName (version_2) with an asterisk to its left, indicating that this is the current branch we’re working on.
We can now add files and commit changes to this branch.

Reverting/Changing to old/other branches

First execute:

$    git branch

This command will show us all the local branches.
To go to another  branch, execute:

$    git checkout branchName

where branchName is the name of the branch we want to go.
For example,

$    git checkout master

will get us to the master branch.

Renaming a branch

$    git branch -m newName

where newName is the name we want for the current branch.
If we are in (no branch), just create a new branch via

$    git branch someBranchName

and switch to this branch via

$    git checkout someBranchName