Git Branching Model

what you should know

  • status of files, “untracked, unmodified, modified, staged”
  • branch include master, brance A, brance B, and so on


when you don’t know what to do first, you can use the help command.

  • git --help
  • git sub-command --help, e.g: git commit --help, git checkout --help


  • ssh-keygen -t rsa -C "
  • git config --global core.editor "vim"
  • git config --global ""
  • git config --global "matrix207"
  • git config --global color.status auto
  • git config --global color.diff auto
  • git config --global color.branch auto
  • git config --global color.branch auto
  • git config --global color.paper "less -r" avoid escape characters in git log|diff
  • git mergetool --tool=vimdiff
  • list configuration: git config -l
  • cat ~/.gitconfig
  • git auto completion, git-completion.bash


  • git clone url
  • git pull fetch data and try to merge it the working code
  • git fetch only fetch data
  • git status
    git status -uno not show untracked files
  • git diff
    • git diff --cached view difference of staged files
    • git diff COMMIT_ONE COMMIT_TWO file view difference for specify file between two commit version
    • git diff fa510^ fa510 view difference between previous and specify commit version
  • git rm files
    git rm -r path remove directory
  • git add files
    • git add . add all files in current directory
    • git add -u ./ only add tracked files to stage
  • git mv path_fileA path_fileB
  • git checkout featureA switch to branch featureA
  • git checkout a.h restore file, if you also have a branch named a.h, should use git checkout – a.h”
  • git reset file unstage file (remove from staged to modified status)
  • git commit commit message with specify editor
    git commit file -m "comment here"
  • git remote -v
  • git log
    • git log -p -2 difference between the latest 2 update
    • git log --stat show detail of changed files
    • git log --author="dennis" filter log by author
    • git log commit_version
    • git log -S 'XXX' file view commit info of specify code of file
    • git log --no-merges not show merge log
  • git clean -f -n Show what will be deleted with the -n option
    git clean -f remove untracked files
    git clean -fd remove untracked files
    git help clean for more information
  • git stash add current modify files to stash
    • git stash pop pop stash files
    • git stash list
    • git stash show
    • git help stash


  • git reset file undo git add
    git checkout HEAD /path/file undo git operation(rm/modify and so on) on file
    git rm $(git ls-files --deleted) undo git rm multiple files
    git reset --hard origin/master cancel local modify
    git reset --soft HEAD~1
    git help reset
  • git merge
    • git merge --squash merge code without commit
    • git merge --no-commit
  • git branch show all local branch, tell you which is the current branch
    git branch -a show all branch, both local and remote
    git branch -vv print the name of the upstream branch
    git branch --contains <commit> find which local branch contain the specify commit
    git branch -r --contains <commit> find which remote branch contain the specify commit
    git help branch for more
  • fork+pull
  • git commit --amend
  • git blame file view all change info of each line
  • git push origin :branch_name delete remote branch
  • git remote update --prune update remote branch information on local
  • git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)'\ |sort -k5n -k2M -k3n -k4n list remote git branches by author
  • git status --short |awk '$1 ~/^M|A|U/ {print $2}' only show modified files
    vim $(git status --short |awk '$1 ~/^M|A|U/ {print $2}') editor all modified files by vim
  • git rev-parse
    • git rev-parse HEAD show commit SHA1 of HEAD
  • git log -L show lines history

other command

  • git commit –fix-up


  • git tag -a v0.1 -m 'new tag version 0.1' add tag
  • git push origin master v0.1 push tag to origin


  • reset repository to specify commit version
    • git clone [remote_address_here] my_repo
    • cd my_repo
    • git reset --hard [ENTER HERE THE COMMIT HASH YOU WANT]
  • view history version of specify file
    • git show HEAD:[THE FILE YOU WANT]
    • git show HEAD:[THE FILE YOU WANT] > NEW_NAME checkout history commit file
    • git log FILE_PATH, e.g: git log ./
  • restore file which was deleted at latest commit.
    • first, checkout the file git checkout HEAD^ -- a.txt
    • second, commit git add a.txt &&git commit -m "recover a.txt" &&git push
  • multi line comment for commit

    [dennis@localhost git]$ mkdir abc
    [dennis@localhost git]$ cd abc
    [dennis@localhost abc]$ git init
    [dennis@localhost abc]$ echo "123">>1.txt
    [dennis@localhost abc]$ git commit -m "first commit
    > - test log1
    > - test log2"
    [dennis@localhost abc]$ git log
    commit 98e983f0fdae5ef292083bb5ce288e9344a46751
    Author: Dennis <>
    Date:   Fri Aug 1 09:02:52 2014 +0800
        first commit
        - test log1
        - test log2
  • merge commit history

    • git rebase -i HEAD~2, modify the second ‘pick’ to ‘squash’ to merge the
      last two commit into one; If want merge more, just modify 2 to other digit.
    • git push --force origin LOCAL-branch:REMOTE-branch, push to remote repository
  • modify the latest commit (or using for merge commit history)

    • git reset --soft HEAD~1, or git reset --soft <commit id> to edit last
      serval commits.
    • … do something else for the modification …
    • git commit -c ORIG_HEAD to changed the commit message, or use
      git commit -C ORIG_HEAD to reuse the previous message
    • reference git help commit, see the --amend
  • summary statics

    • git log --author="$(git config --get" --pretty=tformat: --numstat \ |awk '{add+=$1;subs+=$2;loc+=$1-$2} END{printf "added lines: %s removed lines \ : %s total lines: %s\n",add,subs,loc}' - count total submit lines of code by author
    • git shortlog -s -n count summary commits by author and sort
    • git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5 List top 5 commiter
  • add local repository to remote

    • git add remote git-url
    • git remote -v
    • git push origin master

Find the modified commit quickly

Assume you find that a function was delete in the current commit, and want to
find which commit changed this.

Normally, you will work as below:

  1. git log FILE-INCLUDE-THE-FUNCTION to list the commit history of the file
    which function belong to.
  2. git show COMMIT-SHA and search by the function name

The disadvantage of this method:

  1. If there lots commit history it would be hard to find.
  2. It depend the human eyes, ;), which easy to make mistake sometimes.

So, we want a script to execute this task:

  1. Use git log --pretty=tformat:%h --after 2015-01-05 FILE filter by date, and
    only show commit hash. git rev-list --all --after 2013-03 FILE work as well
  2. Use git show COMMIT-SHA |grep 'KEY-WORDS' to do searching.

With such two skills, we can write bash command as below:
git log --pretty=tformat:%h --after 2013-01 FILE |xargs git show |grep -i KEY-WORDS

Conflict handle


Auto pull project, bash script


exclude_dir="-I test -I hello"

for i in `ls $exclude_dir`
    if [ -d $i ]; then
        (cd $i; echo update `pwd`; git pull)


define ignore files in .gitignore

# ignore all .*.swp files
# ignore all *.out files
# ignore all *.o files
# files in folder

another example:

# Object files

# Libraries

# Shared objects (inc. Windows DLLs)

# Executables






good commit message

Structure your commit message like this:


Short (50 chars or less) summary of changes

More detailed explanatory text, if necessary.  Wrap it to about 72
characters or so.  In some contexts, the first line is treated as the
subject of an email and the rest of the text as the body.  The blank
line separating the summary from the body is critical (unless you omit
the body entirely); tools like rebase can get confused if you run the
two together.

Further paragraphs come after blank lines.

 - Bullet points are okay, too

 - Typically a hyphen or asterisk is used for the bullet, preceded by a
   single space, with blank lines in between, but conventions vary here

 - Use a hanging indent 

Team work A

  • git clone url
  • git checkout -b featureA // create a branch, named “featureA”
  • coding
  • git commit -am “ADD COMMENT HERE”
  • git rebase -i // make all commit to be one
  • git push origin featureA // push your branch to origin
    The general sequence is something like this

Team work B

  • step 1: git check -b branchT1 remotes/origin/branchT1 (you can change your local branch “branchT1” other name if you want)
  • step 2: do your coding
  • step 3: commit your changeds code to your local branch “branchT1”
  • step 4: loop step 2 and step 3
  • step 5: git pull origin branchT1 (fetch and merge code, if you not mean do merge, using fetch instead?)
  • step 6: git push remotes/origin/branchT1