由 CIA 带来的 Git 使用技巧

由 CIA 带来的 Git 使用技巧_第1张图片

简评:从泄漏的文档中我们可以看到 CIA 的工程师们也用 Android, iOS 和 Windows 操作系统,也用 JIRA 和 Git,也从 StackOverflow 中找答案,他们也没有什么不同(当然,前提是这份资料是真的)。

肯定很多人都已经听说了维基解密放出了一波 CIA 的内部资料 - Vault 7,包含了超过 7,500 页的内容,包括使用的工具、how-to 指南、最佳实践等等。

这里要介绍的就是 CIA 内部分享的 Git 技巧:

Oh,我还不想提交呢

# undo last commit and bring changes back into staging (i.e. reset to the commit one before HEAD)
$ git reset --soft HEAD^

这个改动傻逼了,赶紧回滚

# undo last commit and destroy those awful changes you made (i.e. reset to the commit one before HEAD)
$ git reset --hard HEAD^

找出是哪一个提交导致了 build 失败

# Made lots of local commits and haven't run any tests...
$ [unittest runner of choice]
# Failures... now unclear where it was broken.

# git bisect to rescue. 
$ git bisect start # to initiate a bisect
$ git bisect bad   # to tell bisect that the current rev is the first spot you know was broken.
$ git bisect good 
$ git bisect run [unittest runner of choice]
# Some runs.
# BLAMO -- git shows you the commit that broke
$ git bisect reset #to exit and put code back to state before git bisect start
# Fix code. Run tests. Commit working code. Make the world a better place.

分离某个子目录到新的仓库

$ git clone ssh://stash/proj/mcplugins.git
$ cd mcplugins
$ git checkout origin/master -b mylib
$ git filter-branch --prune-empty --subdirectory-filter plugins/mylib mylib
$ git push ssh://stash/proj/mylib.git mylib:master

我合并了冲突,但是我知道哪个版本是正确的。Ours vs. Theirs

# in master
$ git merge a_branch
CONFLICT (content): Merge conflict in conflict.txt
Automatic merge failed; fix conflicts and then commit.
$ git status -s
UU conflict.txt
 
# we know the version of the file from the branch is the version we want.
$ git checkout --theirs conflict.txt
$ git add conflict.txt
$ git commit
 
# Sometimes during a merge you want to take a file from one side wholesale.
# The following aliases expose the ours and theirs commands which let you
# pick a file(s) from the current branch or the merged branch respectively.
#
# N.b. the function is there as hack to get $@ doing
# what you would expect it to as a shell user.
# Add the below to your .gitconfig for easy ours/theirs aliases. 
#    ours   = "!f() { git checkout --ours $@ && git add $@; }; f"
#    theirs = "!f() { git checkout --theirs $@ && git add $@; }; f"

整份文件内容非常丰富,这里介绍的仅仅是 7,500 页中的一页,建议感兴趣的同学点击原文查看全部内容。

ps: 原文为维基解密网站,访问需科学上网。: )
原文:Some Git tips courtesy of the CIA

日报延伸阅读

  • 美国政府部门的开源代码

欢迎关注

  • 知乎专栏「极光日报」,每天为 Makers 导读三篇优质英文文章。
  • 网易云电台「极光日报**」,上下班路上为你读报。

你可能感兴趣的:(由 CIA 带来的 Git 使用技巧)