====== Git ======
[[wp>Git_(software)|Git]]是一个分布式版本控制系统,目前主要用于管理代码版本。
===== Common command =====
==== add ====
按节暂存(stage)/提交(commit)文件:
git add -p
==== commit ====
指定 commit 时间:
d='2017-01-02T15:33:44'
GIT_AUTHOR_DATE="$d" GIT_COMMITTER_DATE="$d" git cm -m 'msg'
# or
GIT_AUTHOR_DATE="$d" git cm -m 'msg' --date "$d"
==== tag ====
删除标签:
git tag -d
删除远程 origin 中的标签:
git push --delete origin
===== Useful command =====
==== clean ====
清理当前工作区为干净工作区:
git clean -d -x -f
==== bisect ====
Debug利器,通过二分法查找是哪个 ''commit'' 导致的bug
开始,设定在两个commit见二分查找(good为工作正常的commit,bad为工作错误的commit):
git bisect start
git bisect bad
git bisect good
# run and test
如果该commit工作仍然异常:
git bisect bad # 将自动切换到下一个测试commit
直到工作正常为止:
git bisect good # 将输出引入bug的commit
==== patch ====
生成 ''patch'' 文件:
git format-patch -1
git format-patch -1 HEAD # or
git format-patch # format patch from to HEAD
应用 ''patch'' 文件:
git am ./change.patch
===== Trick =====
[[https://robots.thoughtbot.com/how-to-fix-rm-f-git-index|How to fix `rm -f .git/index`]]
If you try to delete ''.git/index.lock'' but accidentally delete ''.git/index'', you can recover this repo by using this commands(All unstaged changes will lose, sorry, blame yourself):
git read-tree --reset HEAD
----
Ignore some tracked files
git update-index --assume-unchanged
# retrack
git update-index --no-assume-unchanged
----
**Change commited username/email**
git filter-branch -f --env-filter \
"GIT_AUTHOR_NAME='NAME'; GIT_AUTHOR_EMAIL='EMAIL'; \
GIT_COMMITTER_NAME='NAME'; GIT_COMMITTER_EMAIL='EMAIL';" HEAD
# need to push force
git push --force
==== 管理多个ssh密钥 ====
Linux是通过.ssh/config手动指定每个Host的ssh key来实现对多个ssh密钥的管理的,这个方法对于在需要github上切换使用多个帐号的人来说同样有效。
[[https://gist.github.com/jexchan/2351996#gistcomment-1332446|参考]]
Host my-user-name.github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
Host company.github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_company
IdentitiesOnly yes