Git Essentials

Git has to be by far my favorite version control system. Here are the git commands I use ninety percent of the time. There are many other git commands(e.g., reflog and stash) and variation of the commands I listed that might be considered essentials by others. However, I get by with just the following most of the time.

git init

will convert a directory into a git repository. It will create directory called .git and save all git related data in it.

git add .

will add all the content of the directory and sub directories to the repository's index(staging area). This will allow git to track the files.

git status

will tell you which files are being tracked and which ones are not, the differences between the working directory and the index, the difference between the the last commit and the index.

git commit -m 'initial commit'

will commit what's in the index.

git commit -am 'second file'

will add the content of the working directory to the index and commit it.

git add -p

will interactively show you the changes you have made in the working directory and ask you if you want stage the changes.

git rm -r one-of-the-directories
git commit -m "Removed a directory"

will remove a directory. The rm command can be used to delete file the same way.

git log

will display the history of the repository. Each commit has forty character sha1 hash that is used as an identifier.

git diff

shows you the difference between your index and your working directory. Basically it will show you what you have changed but have not staged yet.

git diff --staged

Shows you your staged content that will go into your next commit.

git diff commit_x commit_y

Displays the differences between two commits or branches.

git checkout -b feature_x

creates a new branch feature_x if it doesn't exist and sets your working directory and index to it.

git checkout branch_name

will simply check the branch and update your working directory and your index. If your working directory has files that are not tracked then they those files will unaffected.

git checkout SHA1_for_commit path/to/file.ext

checks out a specific file from a specific commit.

git branch -v

displays list of branches and their last commits.

git merge feature_x

will merge the changes in the feature_x branch with the master branch. Basically it's creating a new commit that has two parents, the last commit of the master branch and the last commit of the feature_x branch. In a fast forward merge where the master branch hasn't been changed the the latest commit simply moves the end of the master branch and the latest commit simply has only one parent. Git will inform you if the merge causes conflict. You are expected to manually

git rebase

will flatten two forked branches into one branch with a linear history. When you merge two commits Master5 and Forked5 it will create a new commit Master5' that will contain the changes made in both Master5 and Forked5. However, when you rebase you will create Forked1', Forked2', Forked3', Forked4', Forked5' and tack them to the end of Master5. Rebase only works on local repositories.

git clone /path/to/repository

will copy a local repository.

git clone git(@)

will copy remote private repository. You will require a SSH key.

git clone http://

will copy remote public repository.

git pull origin

will update a local repository with changes from a Github repository.

git remote add origin git(@)

adds a remote repository to your existing local repository.

git push -u origin my_branch

push local changes to remote repository.