【问题标题】:How to git rebase -i edit and restore all the change of that commit如何 git rebase -i 编辑和恢复该提交的所有更改
【发布时间】:2021-10-02 06:17:18
【问题描述】:

在问题How to git rebase -i edit and see past commits changes 中,作者给出了使用git reset 的解决方案。但是,该解决方案丢失了已编辑提交的提交消息。有什么方法可以编辑特定的提交(不是最新的提交)并在编辑时恢复所有更改?

例如,这是我执行的命令。

user$ ~/test_git(main○)» touch a
user$ ~/test_git(main⚡)» git add -A                                                                                                              1 ↵
user$ ~/test_git(main⚡)» git commit -m "a"
[main (root-commit) 50fc2a7] a
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a
user$ ~/test_git(main○)» touch b
user$ ~/test_git(main⚡)» git add -A
user$ ~/test_git(main⚡)» git commit -m "b"
[main ec313ba] b
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b
user$ ~/test_git(main○)» git rebase HEAD~1 -i

> edit ec313ba b

Stopped at ec313ba...  b
You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue

我实际上从git status得到什么

user$ ~/test_git(ec313ba○)» git status
interactive rebase in progress; onto 50fc2a7
Last command done (1 command done):
   edit ec313ba b
No commands remaining.
You are currently editing a commit while rebasing branch 'main' on '50fc2a7'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

nothing to commit, working tree clean

我想得到什么

user$ ~/test_git(50fc2a7⚡)» git status
interactive rebase in progress; onto 50fc2a7
Last command done (1 command done):
   edit ec313ba b
No commands remaining.
You are currently editing a commit while rebasing branch 'main' on '50fc2a7'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   b

我希望将我编辑的提交更改为 Changes to be committed 并保留该消息。

【问题讨论】:

  • “恢复所有更改”是什么意思?

标签: git


【解决方案1】:

好吧,如果您执行git rebase -i &lt;a-point-in-the-history&gt;,然后用edit 标记所需的提交,您实际上会得到提交消息和所有其他属性。发生这种情况是因为首先应用了选择进行编辑的提交(在选定的 HEAD 上挑选了樱桃),然后 git 让您有机会修改它。因此,您可以进行所有必要的更改,然后使用git add/git commit --amend 对提交进行修改。后一个命令git commit --amend 允许您保留提交消息和其他提交属性。您还可以使用--author--date--reuse-message=&lt;commit&gt; 选项覆盖某些属性。

为了模拟请求的“新提交”行为,当 git 停止特定提交以进行修改时,可以将 HEAD 重置为先前的提交,同时使用 git reset --soft HEAD^ 保持工作树和索引完好无损:

<git applies the commit marked `edit` and pauses rebasing> (0)
$ git reset --soft HEAD^      (1)
<edit>                        (2)
$ git commit -a -c ORIG_HEAD  (3)

但是,我相信git commit --amend 以及 IDE 和其他用于 GIT 的 GUI 工具的相应功能是实现相同结果的更干净的方式。

【讨论】:

  • 此步骤不会对标记为edit 的提交进行更改,显示在Changes to be committed 部分中。我想通过我的编辑器使用的git diff 查看更改。
  • 您仍然可以看到提交中所做的更改。几乎每个具有良好 Git 支持的编辑器都可以选择修改最后一次提交(Git Gui 工具中的“修改最后一次提交”复选框,IntelliJ Idea 中的“修改”复选框等等)。因此,很有可能,您已经可以在git rebase 期间查看标记为edit 的提交中的更改,只需单击复选框即可。但是,您可以使其与您想要的更加相似。如果您在 git rebase -i 被暂停修改后立即创建 git reset --soft HEAD^,您可能会得到您想要的。
  • 如何在调用git reset --soft HEAD^ 时重用原始提交消息?我只能收到git reflog的消息吗?
猜你喜欢
  • 2021-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-01
  • 2021-07-29
  • 2011-03-15
  • 2011-12-07
相关资源
最近更新 更多