git note 2

How To Set Up Automatic Deployment with Git with a VPS

1
2
cd project.git/hook
vim post-receive

Add the following lines into post-receive

1
2
#!/bin/sh
git --work-tree=/path/of/project --git-dir=/path/of/project.git checkout -f

Then make post-receive executable

1
chmod +x post-receive

Create a Zip archive that contains the contents of the latest commit on the current branch.

1
git archive -o latest.zip HEAD

How many lines of code differs between two commits?

1
git diff --shortstat commit1 commit2

git merge tool

1
2
3
4
5
6
7
8
9
10
11
#configure
git config [--global] merge.tool <toolname> #vimdiff opendiff meld or kdiff3
git config --global mergetool.keepBackup false
#using kdiff3
git config --global merge.tool kdiff3
git config --global mergetool.kdiff3.path /Applications/kdiff3.app/Contents/MacOS/kdiff3
#remove .orig backup file [Git mergetool generates unwanted .orig files](http://stackoverflow.com/a/26218700/2480488)
#merge using specific tool
git mergetool -t <toolname>

resolve merge conflicts

1
2
3
4
5
6
grep -lr '<<<<<<<' . #output conflict files
git checkout --ours PATH/FILE #solution is current branch
git checkout --theirs PATH/FILE #solution is others branch
grep -lr '<<<<<<<' . | xargs git checkout --ours
grep -lr '<<<<<<<' . | xargs git checkout --theirs

fetch from origin with deleted remote branches

1
git fetch -p

revert git commit on server
Undoing a git push

git reset --hard <commit>
git push -f origin <commit>:<branch>
#reset local and origin <branch> to <commit>

reduce git repository size

git reflog expire --all --expire=now
git gc --prune=now --aggressive

git total count

git rev-list HEAD --count

git rebase

git rebase --onto <newbase> <since> <till>

 git rebase -i <newbase>    

git help config

git help config

git checkout

git checkout 

git reset

git reset --soft <commit>    # 不修改工作区和暂存区,只重置master
git reset --hard <commit>    # 替换工作区,暂存区,master <commit>
git reset --mixed 

git reset

git add .
git reset HEAD .    # 撤销 git add .

git reset <commit> <paths> # 重置暂存区为<commit>

git reflog

git reflog             #查看之前几次修改 SHA1值
git reset --hard HEAD@{num} 

commit count

git rev-list HEAD --count

git clean

git clean -f -d 

git stash

# 保存当前工作状态
git add .                     # 加入index
git stash                     # 保存 ,当前工作区所有

git stash list                 # 查看 stash list
git checkout -b new_branch

# do some thing ...

git checkout master
git stash pop               # 多个stash 使用git stash pop stash@{num}

后悔药

git checkout HEAD^ -- <file path>

HEAD^         后悔一步
HEAD^^         后悔两步
HEAD^^^     ...
HEAD^4        ...
HEAD^5        以此类推

角色: 最近六次提交分别是ABCDEF

$git log --online -6
XXXXXXX modify hello.h                        # F    XXXXXXX add hello.h                            # E
XXXXXXX ignore object files                    # D
XXXXXXX move .file outside also works.        # C
XXXXXXX Hello world initialized                # B
XXXXXXX README is frome welcome.txt        # A

坏蛋: 提交 D。

目的就是:干掉提交D
需要拣选指令–git cherry-pick
含义是从众多的提交中挑选出一个提交应用在当前的工作分支中。该命令需要提供一个提交ID作为参数,操作过程相当于讲该提交到出为补丁文件,然后在当前HEAD上重放,形成无论内容还是提交说明都一致的提提交
首先对版本库要‘参演’的角色进行标记,使用git tag 命令

git tag F
git tag E HEAD^
git tag D HEAD^^
git tag C HEAD^^^
git tag B HEAD^4
git tag A HEAD^5

1.第一幕干掉坏蛋 D
(1) 执行 git checkout ,暂时将 HEAD 头指针切换到 C。
切换过程显示非跟踪状态的警告,没有关系,因为剧情需要。

$git checkout C
$git checkout -b new_branch_name

(2) 执行拣选操作将 E 提交在

change some history comment

git rebase -i <commit number>

change last comment

git commit --amend    

安装自动补全命令

brew install bash-completion

.gitignore

# 这是注释行 -- 被忽略
*.a         # 忽略所有以 .a 为扩展名的文件。
!lib.a        # 但是 lib.a 文件或者目录不要忽略,即使前面设置了对 *.a 的忽略。
/TODO         # 只忽略此目录下的 TODO 文件,子目录的 TODO 文件不忽略。
build/      # 忽略所有 build/ 目录下的文件
doc/*.txt     # 忽略文件如 doc/notes.txt,但是文件如 doc/server/arch.txt 不被忽略。

“# a comment - this is ignored
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the root TODO file, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .txt files in the doc/ directory
doc/**/*.txt”

remove ignored files

1
2
3
git rm -r --cached .
git add .
git commit -am "Remove ignored files"

git rev-list HEAD | wc -l

git rev-list HEAD | wc -l   # 查看历史提交次数

git diff

git diff B A             # 比较里程碑 B 和里程碑 A
git diff A                # 比较工作区和里程碑 A
git diff --cached A     # 比较暂存区和里程碑 A
git diff --cached        # 比较工作区和暂存区
git diff HEAD             # 比较工作区和HEAD

若只有一个参数就是比较工作区和 这个参数
两个参数就是比较两个参数
参数可以是 里程碑 commitId 工作区 等等

git diff <commit1> <commit2> -- <paths>

git blame

git blame <file path>     # 文件追溯