【问题标题】:How can I efficiently navigate a Git repo to follow project development?如何有效地导航 Git 存储库以跟踪项目开发?
【发布时间】:2015-03-06 13:10:08
【问题描述】:

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

代码中没有太多文档,但存储库迄今已收到

  1. checkout 第一次提交;看一些代码
  2. checkout 稍后提交约 5-6 次;看看它是如何变化的
  3. 冲洗并重复 #2 直到我是最新的

.. 而不是现在只查看 all master 的 HEAD 处的代码,这要复杂得多。

我的想法的问题是,一旦我git checkout $commit_1,我就会进入一个分离的头部状态,所以要“升级”到任何更新的提交,我必须git checkout master再次,并且然后努力完成我想要的提交。有没有更方便的方法来做到这一点?即检查一个旧的提交,然后让 git 显示新的提交并移动到其中一个。

【问题讨论】:

  • git checkout master~82 ... git checkout master~77
  • @Zeeker 相当简洁方便!
  • progit 书的Revision Selection 章节可能会让您感兴趣。

标签: git revision-history


【解决方案1】:

无需一直检查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
...

【讨论】:

  • 非常酷!这是迄今为止我见过的最方便的方式。
【解决方案2】:

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 的评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多