Git Cheat Sheet

Git is a free and open source distributed version control system. Followings are some of the advantages of using git version control system:

  • branching capabilities
  • light weight
  • speed
  • reliable
  • secure
  • open source

Refer to following table to find most commonly used git commands:

Command Description
git init <dir> creates empty git repo in specified directory on local machine
git clone <repo> <name> clones remote repository to local machine with specified name
git config --global <name> defines author name for all commits
git config --global <email> defines author email for all commits
git add <dir> stage all changes in specified directory on local machine
git commit -m <message> creates a new commit with specified commit message
git status checks the status of current git repo, which files are staged, unstaged, and untracked
git diff shows differnce between last commit and current changes on local machine
git revert <commit> undo changes of specified commit
git reset <file> unstages a file without overwriting any changes
git clean -n shows which files would be removed from working directory
git log -<limit> shows lists of commits limited by -<limit> option
git log --stat Include which files were altered and the relative number of lines that were added or deleted from each of them
git log --author=<pattern> search for commits by a particular author.
git log --grep=<pattern> search for commits by a particular commit message.
git log -p display the full diff of each commit.
git branch list all of the branches in your repo.
git branch <name> create a new branch with the name .
git checkout -b <name> create and check out a new branch named <name>. Drop the -b flag to checkout an existing branch.
git merge <branch> merge <branch> into the current branch.
git pull pull all remote changes to local branch
git pull <branch> fetch the specified remote’s copy of current branch and immediately merge it into the local copy
git fetch <remote> <branch> fetches a specific <branch>, from the repo.
git push <remote> <branch> push the branch to , along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.
git diff HEAD show difference between working directory and last commit.
git diff --cached show difference between staged changes and last commit
git reset reset staging area to match most recent commit, but leave the working directory unchanged.
git reset --hard reset staging area and working directory to match most recent commit and overwrites all changes in the working directory.