如何有效地导航Git仓库以跟进项目开发?

时间:2015-01-08 12:07:57

标签: git revision-history

我正在尝试理解在git存储库中开发的一个相对较新但非平凡的工具。

代码中没有太多文档,但是到目前为止,存储库已经提取了<100个提交,如果我做了类似的事情,我可以看到自己更好地理解了什么:

  1. checkout第一次提交;看一些代码
  2. checkout说5-6后提交;看看它是如何变化的
  3. 冲洗并重复#2,直到我及时更新
  4. ..而不是仅仅在主人的HEAD上查看所有代码,这要复杂得多。

    我的想法的问题是,一旦我git checkout $commit_1,我就进入一个独立的头状态,所以去&#34; up&#34;对于任何较新的提交,我必须再次git checkout master ,然后按照我想要的提交方式工作。有更方便的方法吗?检查一个旧的提交,然后让git向我显示更新的提交并移动到其中一个提交。

2 个答案:

答案 0 :(得分:4)

无需再次查看master,或在一张纸上涂抹大量的SHA-1。在任何阶段,您都可以运行

git log --oneline --decorate --graph master

为方便起见,您可能希望定义以下别名,正如许多Git用户所做的那样:

git config alias.lg "log --oneline --decorate --graph"

然后上面的命令变成

git lg master

这将输出master分支的整个祖先的日志(尽管以简明的形式),无论你在提交图中的“你在哪里”。

通过相应的简短SHA-1哈希,这是一个很好的方法来确定你想要检查或检查的提交。请注意,使用--decorate标记会显示您的位置(HEAD)。

实施例

以下是我导航我自己的一个Git存储库的示例:

$ git branch
* master
$ git tag
v0.1
v0.2
v0.3
$ git checkout v0.3
Note: checking out 'v0.3'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at cfc71e4... mention compatibility with subset of Octave syntax
$ git log --oneline --decorate --graph master
* 73ec762 (origin/master, origin/HEAD, master) improve brace style
* ffc8d67 remove superfluous word; delete trailing whitespace
* 3f8b8db remove obsolete comment about mcode
* f9f9fd0 remove .DS_Store file that was accidentally added
* cc855ab clarify description of mlonlyheader option
* 7553ccf change contact email
* 4d860e9 correct remarks on shell-escape and backtick
* 9a2ef02 corrected typo in Tips & tricks
* f0badb5 minor improvements in documentation
* cfc71e4 (HEAD, tag: v0.3) mention compatibility with subset of Octave syntax
* 7db2c88 Preventive bugfix: replace \toks@ by \toks@mlpr
* 01fdc43 delete OS-specific files from .gitignore
...

答案 1 :(得分:1)

git log决定哪些提交是有意义的。记下他们的SHA,然后检查出来。无需每次都掌握主人。

示例:

$ git checkout 315e25
Note: checking out '315e25'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 315e25a... 85192566 - Use Linker js namespace for verifying link
07:35:55 durrantm Castle2012 /home/durrantm/Dropnot/_/rails_apps/linker (detached from 315e25a)
$ git checkout d93b9c
Previous HEAD position was 315e25a... 85192566 - Use Linker js namespace for verifying link
HEAD is now at d93b9c7... 85192548 - Use Linker js domain for show group members
07:36:13 durrantm Castle2012 /home/durrantm/Dropnot/_/rails_apps/linker (detached from d93b9c7)
$ 

当您完成签出提交历史记录和各种感兴趣的提交后,您可以返回到master:

$ git checkout master
Previous HEAD position was d93b9c7... 85192548 - Use Linker js domain for show group members
Switched to branch 'master'
07:37:37 durrantm Castle2012 /home/durrantm/Dropnot/_/rails_apps/linker master
$ g
On branch master
nothing to commit, working directory clean

您也可以使用相对编号来逐步浏览SHA,如Zeeker评论中所述。