January 01, 2015

Configuration / Preferences

  1. System - every user on system /etc/gitconfig
  2. Global (= per User) ~/.gitconfig
  3. Local (= per repository) »project folder«/.git/
ch branch.master.rebase true

git config --list # == cat ~/.gitconfig
git config -l --local
git config -l --global
git config -l --system

List branches

git branch --merged
git branch --no-merged

# branches ordered by last commit time
for-each-ref --sort=-committerdate --format="%(committerdate:relative)%09%(refname:short)" refs/heads
git branch     # list local branches
git branch -a  # list all branches
git branch -v  # verbose
git branch -d cat
git branch --track [branch_name] [repo/branch] # create tracking branch
git checkout -b admin # create & checkout
git push origin :shopping_cart # delete remote branch

Commiting (& adding)

git add --all # stages all (== git add .; git add -u)
git add .     # stages new and modified, without deleted
git add -u    # stages modified and deleted, without new
git add -p    # patch mode, allows you to stage parts of a changed file, instead of the entire file.

git commit -a -m "message" # Add changes from all tracked files, Doesn’t add new (untracked) files

# Ammend last commit, reuse message
git commit --amend -C HEAD


  • Clean local branches
  • Clean remote branches
  • Clean stashes
  • Update local branches
# List all branches that are already merged into master
git branch --merged master

# Remove branches that have already been merged with master
`git branch --merged master | grep -v '^\*' | xargs -n 1 git branch -d`

List all branches and their upstreams, as well as last commit on branch

git branch -vv

# delete local branches that were deleted upstream
git fetch -p
git-sweep preview
git-sweep cleanup

Browse history (log)

git log --full-history -- [file path] # File history (even deleted)

Syncing with remotes (push, pull, fetch)

git remote -v # list remotes git remote add origin https://... git remote rm git remote show origin

git push origin --force

git pull  # git fetch && git merge origin/master
git fetch # do not update local code

Cleaning working directory

# Get rid of untracked files and directories in your working copy
git clean -f -d

git merge --abort
git reset --hard HEAD
git reset HEAD^

git reset --soft HEAD^
git reset --soft HEAD^ # Undo last commit, changes to staging
git reset --hard HEAD^ # Undo last commit, undo changes
  • git reset without a —hard or —soft moves your HEAD to point to the specified commit, without changing any files.
  • HEAD^ refers to the (first) parent commit of your current commit, which in your case is the commit before the temporary one./


All changes, a one-line change is normally shown with 3 lines of context

git diff --function-context
git diff --unified=10 # more context lines
git diff HEAD # both staged and unstaged files
git diff HEAD~5 # five commits ago


respects .gitignore

git grep "needle"
git grep --untracked "needle"


Join two or more development histories together

git merge --no-ff
git merge --no-ff --edit # custom message when merging, with `core.editor`


git rebase -i HEAD~3 # squash the last 3 commits into one

pick df94881 First commit
pick a7323e5 Second commit
pick 3ead26f Third commit
# => 
pick df94881 First commit
squash a7323e5 Second commit
squash 3ead26f Third commit


# Get file from different branch
git checkout feature132 flash/foo
git stash --include-untracked
git stash list
git stash apply
git stash clear


  • All the branch pointers are kept in .git/refs/heads, HEAD is in .git/HEAD and tags are in .git/refs/tags


Find by binary search the change that introduced a bug

Git Submodules


git submodule update --init to clone them locally

Simple intro