Git — Cheat Sheet

This cheat sheet is summarized from “Pro Git” written by Scott and Ben. And some other awesome blogs are included.

What is Git?

Git is a distributed version control system. In git, data is more likely be thought as a sequence of snapshots. So nearly every operation adds data to Git database. A file in Git could be at one of the three possible states: modified, staged, and committed. And the three corresponding working sections in a Git project are working directory, stage area and git directory.

Git directory section refers to the part that contains the git database and the metadata. This is the core of Git and the place where the compressed snapshots stored. Working directory section is where we work on the files, which are the checkouts of one version of the compressed snapshots. Stage area stores the information about the git directory and the working directory, like which file has been modified since last commit.

How to use Git (basics)?

Identity and Initialization

Before initialize a tracking of a project, we’d better tell git who is going to make some modifications. And every time you commit, git could bake the information into the snapshots. This is important and could be done with:

// globally set identity
git config --global <user name>
git config --global <user email>

To start tracking a project with git. Just go to the directory of the project and create a git repository skeleton with

git init  //start a git version tracking 

Basic Actions

  1. add modified files to stage area: git add <file/dir name>
  2. commit what has been staged to database: git commit -m <message>
  3. get git repository status: git status
  4. For more detailed changes in git status:(1) difference between what is changed but not yet staged: git diff (2) difference between what is staged and last commit: git diff --staged
  5. remove files (1) remove from both working directory and staged area: git rm <file name>(2) remove only from the staged area: git rm --cached <file name>(3) remove only from the working directory: rm <file name> (4) remove from both the working directory and the staged area when the file status is inconsistent between these two sections: git rm -f <file name>
  6. rename a file git mv <filename> <renamed name>
  7. commit history git log . (1) to see difference introduced in each commit git log -p -2 //(-2) for the latest two commits (2) see difference with summarized info git log --stat (3) change the output format git log --pretty = oneline git log --pretty = format : %h %an %s --graph (4) filter log git log --since=<2.weeks> //relative date git log --since <2017-05-17> //absolute date git log --author=<"frank">
  8. undo modifying a file from last commit git checkout --<filename>
  9. undo staging a file git reset HEAD <staged filename>
  10. amend a commit to overwrite previous commit message or add new files to the last commit git commit --amend (to add new files do git add <filename> before amend)

Remote Repositories

  1. show remote repos, git remote -v
  2. add remote repos git remote add <repo alias > <repo url>
  3. inspect a particular repository git remote show <remote name>
  4. rename the alias git remote rename <prev name> <wanted name>
  5. fetch data from remote without merge git fetch <remote name>
  6. fetch data from remote with automatic merge git pull
  7. push data to remote git push <remote repo> <your branch name>
  8. push data to different branch on the remote git push <remote repo> < your branch name> : <remote branch>
  9. remove a remote git remote rm <remote repo>

How to use Git (branching)?

A branch in Git is a lightweight movable pointer to the commit. Every time you have a new commit, the pointer move forward automatically. HEAD pointer points to the local branch you are currently on.

Basic actions

  1. creating a new branch git branch <new branch name>
  2. switching branches git checkout <branch name> . Note switching branches changes files in your working directory
  3. to see the branches history movement git log --oneline --decorate --graph --all
  4. remove a branch (1) remove local branch git branch -d <branch name> (2) remove a branch in remote repogit push <remote repo name> --delete <branch name>


There are two types of strategies used to merge branches. Fast-forward merge and three-way merge. When there is no divergent work between the current branch and the merging-in branch, a fast-forward strategy is used. When the commit on the branch you are on is not the ancestor of the branch you are merging in, a three-way strategy is used. The three-way merge uses the two snapshots pointed by the branches and the common ancestor of the two.

  1. Merge the to-be merged branch to the current branchgit merge <to-be merged>
  2. When merge conflict happens, you need to resolve the conflict manually. After that, do a git add <files causing conflict> and git commit -m <message> to finish the merging
  3. Get the merged branches to the current branch git branch --merged
  4. Get the not-yet merged branches git branch --no-merged

Remote branches

Remote branches are pointers pointing to the state of of branches in your remote repositories. They take the name <remote repo> / <remote branch name> . The remote branches cloned/fetched from the remote repository is static on your local. They don’t move forward unless any network communication is involved. For example git fetch <remote repo> Whenever a new remote branch is fetched from the remote repo, you have to do merge to get a editable local copy.

  1. check all the remote branches git branch -r
  2. remove a branch in the remote repository git push <remote repo> --delete <branch name>

How to use git (submodules)?

Great post on submodules!

Like what you read? Give Binary0101 a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.