Git cheat sheet

git has become industry standard for version control system so if you are software developer, there’s no way to escape git. I’ve been using git for about a year for now and have developed my own workflow. My core philosophy is to make sure the commits are as linear as possible, this basically means I’m using rebase as much as I can. In addition, I use git mostly via command line and only use gui for browsing. This allows me to be as cross platform as possible.

Table of Contents

  1. New machine setup
  2. Workflow recipes
  3. Submodule
  4. Misc commands

New machine setup

Use git config --list to view your config.

User setup

git config --global <user_name>
git config --global <myemail@domain>

Credential setup

I am using Github’s Personal Access Token (PAT).

Diff and merge tool

I am using Beyond Compare (you need to have the pro version for merge) for both diff and merge tool. It works for all platforms I care (Windows, Mac, Linux). For non-Windows platform, make sure you can open Beyond Compare from terminal, type bcompare.

git config --global diff.tool bc
git config --global difftool.bc.trustExitCode true
git config --global merge.tool bc
gif config --global mergetool.bc.trustExitCode true

For Windows:

git config --global difftool.bc.path "c:/Program Files (x86)/Beyond Compare 4/bcomp.exe"
git config --global mergetool.bc.path "c:/Program Files (x86)/Beyond Compare 4/bcomp.exe"

Workflow Recipes

Sync to latest

My main philosophy is to keep master (or main) branch clean and synced to origin.

Fetch and get info about latest changes

git fetch origin

Make sure local repo is clean

git status
git log --oneline -n 10

Proceed with merge if current repo is indeed clean

git merge origin/master

Whenever you are ready to make a change, create a branch:

git checkout -b <branch_name>

Generate and Applying Patchfile

Suppose you have been working on branch1 (and has complicated history) and want to start fresh on branch2. You can generate difference between branch1 and master to a patch file (Make sure you’ve merged master to branch1) and simply apply this patch file to branch2:

git diff master branch1 > ../patchfile 
git checkout master
git checkout -b branch2 
git apply ../patchfile (or git apply --reject ../patchfile)

Updating last commit message

You can only update the last commit message

git commit --amend

To allow # in commit message, do

git commit --amend --cleanup=whitespace

Reviewing pull request

The idea is to create a branch pointing to the full request and then switch to that branch:

git fetch origin pull/<pr_number>/head:<local_branch_name>
git checkout <local_branch_name>

Diff tool

How to diff a specific file with master

git difftool master -- <path_to_file>

Removing untracked files

If you want to delete untracked files/folder:

git clean -fd


I think of git stash similar to P4 shelve/unshelve. Here’s some useful stashing related (self explanatory) commands:

git stash
git stash list

// This will apply the last one
git stash apply 

// Applying a specific stash
git stash apply stash@{NUMBER}

git stash show stash@{0}


Some commonly used submodule commands:

// Listing submodule url
git config --file=.gitmodules -l

// Updating submodule url
git submodule set-url <submodule_name> <url>

// Put the submodule in the right state
git submodule update --init --recursive <submodule_name>

// Update submodule to latest
git submodule update --remote --recursive <submodule_name>

Misc commands

Fixing detached head

Think of detached HEAD as unnamed branch. The idea is to give this branch a name and manipulate it as you want:

git checkout -b <branch_name>
# Manipulate as the new branch as you wish

Removing (and ignoring) committed file

First, add the file/folder in .gitignore and then to remove from your repository and keep local copy:

git rm --cache <path-to-file>

Update git to latest version

git update-git-for-windows