Git In Case Of

Git In Case Of

2013-05-01. Category & Tags: Soft&Skills Soft&Skills

Motivation #



Config #

git config -l # ls all configs

Frequently Used #

git help <command>

git show            # show content of a commit/file-obj.
git show $id
git show object
git show $REV:$FILE
git show somebranch:from/the/root/myfile.txt
git show HEAD^^^:test/

git co  -- <file>   # discard modifications in workspace // co = Check Out
git co  .           # discard modifications for all (previously existing?) files in workspace
git co ea083b .     # discard modifications in workspace and co all files from commit ea083b to overwrite files, but without changing branch/commit

git add <file>      # 将工作文件修改提交到本地暂存区
git add .           # 将所有修改过的工作文件提交暂存区
git st/status       # workspace status
git commit -m'msg'  # commit with a msg
git tag v1.0 ea083b # tagging ea083b with a tag "v1.0"
git tag -d ea083b   # delete tag of commit ea083b

git rm <file>       # 从版本库中删除文件
git rm <file> --cached  # 从版本库中删除文件,但不删除文件

git ci <file>
git ci .
git ci -a           # 将git add, git rm和git ci等操作都合并在一起做
git ci -am "some comments"
git ci --amend      # 修改最后一次提交记录

git revert <$id>    # 恢复某次提交的状态,恢复动作本身也创建了一次提交对象
git revert HEAD     # 恢复最后一次提交的状态

git blame [commit] -Ln1,n2 -- prev/file # see sub-section below

Diff #

git diff <file>     # 比较当前文件和暂存区文件差异
git diff
git diff <$id1> <$id2>   # 比较两次提交之间的差异
git diff <branch1>..<branch2> # 在两个分支之间比较
git diff --staged   # 比较暂存区和版本库差异
git diff --cached   # 比较暂存区和版本库差异
git diff --stat     # 仅仅比较统计信息

Reset & Resore (inc. Stage) #

git reset           # = `undo add ` 从暂存区恢复所有文件到工作文件
git reset <file>    # = `undo add <file>` 从暂存区恢复到工作文件
git reset -- .      # 从暂存区恢复到工作文件
git reset --hard    # 恢复最近一次提交过的状态,放弃上次提交后的所有本地修改,用的也很多!!
git reset --hard origin/master
git reset --hard <a_commit>
git reset --soft HEAD~1  # 撤消最后一次commit,所有的修改都保留, 用的最多 !!!

git restore --staged  # Unstage one file (undo git add). Copies the last version of from repo to index
git restore --staged .            # Unstage all files (undo git add)

Log #

git log
git log <file>      # 查看该文件每次提交记录
git log -p <file>   # 查看每次详细修改内容的diff
git log -p -2       # 查看最近两次详细修改内容的diff
git log --stat      # 查看提交统计信息`</pre>

Local Branch #

take a look #

git br -r           # 查看远程分支
git br -v           # 查看各个分支最后提交信息
git br --merged     # 查看已经被合并到当前分支的分支
git br --no-merged  # 查看尚未被合并到当前分支的分支
git br <new_branch> # 创建新的分支(不常用!)
git co -b <new_branch> # 从当前commit 创建新的分支,并且切换过去 (常用!)

change a branch #

git co <branch>     # 切换到某个分支
git co <branch> .   # co某个分支所有文件,但是不切换分支
git co -b <new_branch> <branch>  # 基于branch创建新的new_branch
git co $id          # 把某次历史提交记录checkout出来,但无分支信息,切换到其他分支会自动删除
git co $id -b <new_branch>  # 把某次历史提交记录checkout出来,创建成一个分支

delete a branch #

git br -d <branch>  # 删除某个分支
git br -D <branch>  # 强制删除某个分支 (未被合并的分支被删除的时候需要强制)

merge & rebase #

git merge <branch>               # 将branch分支合并到当前分支
git merge origin/master --no-ff  # 不要Fast-Foward合并,这样可以生成merge提交

git pull --rebase
git rebase master <branch>       # 将master rebase到branch,相当于:
git co <branch> && git rebase master && git co master && git merge <branch>

blame (track changes) #

Use -L to control which lines to check.

Option 1: use blame whose layout looks like a hex editor, i.e. line by line #

git blame [commit] -- /path/to/ # note: add a space after --
git blame [commit] -L 10,15 -- /path/to/
git blame [commit] -L 10,+5 -- /path/to/

Option 2: use -u in log, which differs codes of users as blocks similar to diff. #

git log -u /path/to/
git log -u -L 10,15:/path/to/
git log --pretty=short -u -L 10,+5:/path/to/


Remote Branch #

git pull                         # 抓取远程仓库所有分支更新并合并到本地
git pull --no-ff                 # 抓取远程仓库所有分支更新并合并到本地,不要快进合并
git fetch origin                 # 抓取远程仓库更新
git merge origin/master          # 将远程主分支合并到本地当前分支
git co --track origin/branch     # 跟踪某个远程分支创建相应的本地分支
git co -b <local_branch> origin/<remote_branch>  # 基于远程分支创建本地分支,功能同上

git push                         # push所有分支
git push origin master           # 将本地主分支推到远程主分支
git push -u origin master        # 将本地主分支推到远程(如无远程主分支则创建,用于初始化远程仓库)
git push origin <local_branch>   # 创建远程分支, origin是远程仓库名
git push origin <local_branch>:<remote_branch>  # 创建远程分支
git push origin :<remote_branch>  #先删除本地分支(git br -d <branch>),然后再push删除远程分支

Get Remote Branch #

Use git fetch to fetch all remote branches, then run:
git checkout -b <local-branch-name> origin/<remote-branch-name&gt
就可以将远程分支映射到本地命名为 的分支 (如果没有此分支则自动建立)。


Remote Repo #

frequently used #

git remote -v                    # 查看远程服务器地址和仓库名称
git remote show origin           # 查看远程服务器仓库状态 & URL
git remote add origin git@github:robbin/robbin_site.git         # 添加远程仓库地址
git remote set-url origin [email protected]:robbin/robbin_site.git # 设置远程仓库地址(用于修改远程仓库地址)
git remote rm <repository>       # 删除远程仓库

create a remote repo #

git clone --bare robbin_site robbin_site.git  # 用带版本的项目创建纯版本仓库
scp -r my_project.git [email protected]:~      # 将纯仓库上传到服务器上

mkdir robbin_site.git && cd robbin_site.git && git --bare init # 在服务器创建纯仓库
git remote add origin [email protected]:robbin/robbin_site.git    # 设置远程仓库地址
git push -u origin master                                      # 客户端首次提交
git push -u origin develop  # 首次将本地develop分支提交到远程develop分支,并且track

git remote set-head origin master   # 设置远程仓库的HEAD指向master分支

Follow a Remote/Local Branch #

git branch --set-upstream master origin/master
git branch --set-upstream develop origin/develop`</pre>

Patch (方便在多台机器上开发同步时用) #

git diff > ../sync.patch         # 生成补丁
git apply ../sync.patch          # 打补丁
git apply --check ../sync.patch  # 测试补丁能否成功

Stash (暂存) #

git stash                        # 暂存
git stash list                   # 列所有stash
git stash apply                  # 恢复暂存的内容
git stash drop                   # 删除暂存区`</pre>

Force Overwrite a Local Branch #

git fetch --all
git reset --hard origin/master

git fetch downloads the latest from remote without trying to merge or rebase anything. Then thegit reset resets the master branch to what you just fetched.

Force Delete All History #

Suppose to delete the history of master (current branch).

git checkout --orphan temp_branch
git add -A
git commit -m "the first commit"
git branch -D master
git branch -m master
git push -f origin master



When merge conflicts occur, the merge progress will automatically PAUSE with conflict error status. git status shows some files under “both modifed”.

Option 1: for simple conflicts.

  • Use text editor to open the conflict file.
  • Fix conflicts manually. Alternatively, mv .LOCAL or .REMOTE file to overwrite the conflict file.
  • Delete symbols, markers etc. added by Git (if not using mv).
  • Use git add <conflict_filename> to tell Git that the conflict file is resolved.

Option 2: for complex conflicts.

  • Use some tools, such as diff tools, to compare BASE vs. LOCAL and BASE vs. REMOTE.
  • Fix conflicts manually. Alternatively, mv .LOCAL or .REMOTE file to overwrite the conflict file.
  • Delete symbols, markers etc. added by Git (if not using mv).
  • Use git add <conflict_filename> to tell Git that the conflict file is resolved.

Option 3: for advanced users.

  • git mergetool

Then the Git status becomes “All conflicts fixed but you are still merging”.

git commit ... to continue the merge.


The point is to make use of the SSH config file.

Host remote_1
    User git
    IdentityFile ~/.ssh/key_for_remote_1

Host remote_2
    User git
    IdentityFile ~/.ssh/key_for_remote_2

Explanation: In config, the Host is a nickname for HostName and SSH resolves Host to HostName. When doing git clone (first time) or git remote add (none-empty dir), just replace the real HostName by Host according to which key to use.


gitExtensions #

  • recommended for experienced git users
  • requires Git-for-Windows (git-scm)
  • first time init. needs to config nickname, nick-email, merge tool (WinMerge/KDiff3), compare tool (WinMerge/KDiff3), Linux sh dir (e.g. C:\Program Files\Git\usr\bin).
  • Note: it is recommended to install all software into a dir without spaces in path (e.g. C:\green\xxx)


github desktop #

recommended for new users with easy internet connections

smartGit #

  • may be recommended
  • similar to git-extention
  • will show a notice for 30s every time (free version)

sourceTree #

  • not suggested
  • good GUI but low speed and the few existing functions are just soso compared to git-extension
  • require MS .Net installation which leads to OS restart
    For full list, see here

gitKraken #

  • not suggested
  • pure-open-source only (free version), does not allow private or local repos

megit #

  • may not suggested
  • requires some java knowledge

ref git-scm


ignore file permission (file mode) changes #

git config --global core.fileMode false

ignore sensitive/secret lines #

add a global filter ‘sensitiveLineIgnoreFilter’: #

git config --global filter.sensitiveLineIgnoreFilter.clean "sed '/#gitignore#/'d"  # thus, any line ended with '#gitignore' will be ignored, but you will see it when doing diff.
git config --global filter.gitignore.smudge cat # do nothing when pulling file from repo
# i did in windows git shell, didn't try cmd

apply the global filter to a git project: #

vim .gitattributes
<project root>/.gitattributes will be committed into repo
<project root>/.git/info/attributes won’t be.
add the filter to .gitattributes: filter=sensitiveLineIgnoreFilter

add ‘#gitignore#’ to the sensitive lines: #

If ‘#gitignore#’ is added BEFORE 1st commit, then after a commit, the line will disappear.
/_ Otherwise, the line will remain, but be ignored (? not tested) _/


teamwork examples #


  • chapter 5. Distributed Git
  • ch5.1 Distributed Workflows
  • ch5.2 Contributing to a Project


Problem: need proxy to bypass firewalls.

Note: git does NOT recognize the http(s)_proxy set by export.

Solution 1 (suggested): install proxy software & config git/ssh.

For https…git:

git config --global http.

For git@…git, edit ~/.ssh/config:

    ProxyCommand /usr/bin/nc -X connect -x %h %p

Solution 2 (not suggested): install proxy software & use proxychains.

brew install proxychains-ng
vim /usr/local/etc/proxychains.conf
remote_dns_subnet 224
tcp_read_time_out 15000
tcp_connect_time_out 8000

http 12345


proxychains4 brew update

Problem: git status 不能显示中文,而是显示转义字符(斜杠)。

Reason: show UTF by default.
Solution: git config --global core.quotepath false ref: git status 显示中文和解决中文乱码

Problem: go back to master with current modification.

Solution: “ours” policy.


git checkout 307a5cd        # check out the commit that you want to reset to
git checkout -b fixy        # create a branch named fixy to do the work
git merge -s ours master    # merge master's history without changing any files
git checkout master         # switch back to master
git merge fixy              # and merge in the fixed branch
git push                    # done, no need to force push!


registry (docker images hub) #

Go to menu: /settings/repository > “Deploy Tokens”.
When “create”, Gitlab will create a “Your New Deploy Token” which contains a pair of “username as a login” and “token as a password”.
Tip 1: it is shown only once.
Tip 2: this can be used in k8s/kube to pull images.


main ref
ref1 the book
ref2 cn
other verson control softwares inc. SVN Subversion, Git, Mercurial, Bazaar 2014:
p 2: this can be used in k8s/kube to pull images.