如何高效地在Git仓库中导航以跟踪项目开发进展?

3

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

代码中没有太多文档,但该仓库迄今已经有不到100次的提交,如果我做一些如下的事情,我可以看到自己更好地理解正在发生的事情:

  1. checkout 第一个提交; 查看一些代码
  2. checkout 大约五六个提交后; 查看它是如何变化的
  3. 重复步骤 #2 直至最新

.. 而不仅仅是查看现在在 master HEAD 的所有代码,那样会更加复杂。

我的想法的问题在于,一旦我 git checkout $commit_1 ,我就进入了分离头状态,所以要转到任何更新的提交,我必须再次 git checkout master ,然后从那里向下工作,直到我想要的提交。有没有更方便的方法来做到这一点?也就是说,检出一个旧的提交,然后让 git 显示给我更新的提交并移动到其中之一。


4
git checkout master~82 ... git checkout master~77 等等是 Git 命令,用于切换到在 Git 中标记为 "master" 的分支中的某个先前提交的版本。 ~ 后面的数字表示要向后回滚的提交次数。 - Sascha Wolf
@Zeeker 非常简洁和方便! - ArjunShankar
3
《Pro Git》书中的修订版本选择一章可能会对您有所帮助。 - Sascha Wolf
2个回答

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
非常酷!这是我迄今为止见过的最方便的方式。 - ArjunShankar

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)
$ 

当你完成查看提交历史和各种感兴趣的提交后,你可以返回到主分支:

$ 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

你也可以使用相对编号,按照Zeeker的评论中所述,逐步浏览SHA。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接