在git中修改旧提交 [英] Modifying old commit in git
问题描述
我有回到旧的提交git的习惯,并修改我的工作目录。例如,我有一系列的提交
A <-B <-C <-D <-E <-F
提交BI添加了一些文本文件。现在我想在该提交中添加更多文本文件并将其记录在那里。
我听说git rebase和git reset --soft应该在这里使用但不明白如何在实践中使用它们?
我应该使用以下命令:
$ p $ g $ reb $ $ $ $ $ $ $ $ $ $ $ $
您可以使用
git rebase -i HEAD〜5
这会打开一个窗口, / b
挑选362trhy4
挑选ewq3w5y4
挑选34642wfg
挑选34265wey
pick 34652366
rebase blabla转换为blabla
请注意,提交哈希只是组成。你想要做的是继续你想编辑的提交行,并将选择
更改为 edit
。然后退出编辑器(nano,vim等)并确认。
它会将您带回要编辑的提交。您现在可以自由进行更改。一旦你完成了, git rm
你的变更文件,提交修改它们(因为你想改变你的提交)
git commit --amend -m新提交信息
请注意,这会改变您的提交哈希。然后做
git rebase --continue`
这将使rebase进程继续。因为所有其他提交都只是 pick
s,git会尝试将它们重新应用到您编辑的提交中。
如果没有冲突出现:很好,现在就完成了。如果他们这样做:以任何你喜欢的方式解决冲突,并用 git add
或<$来标记解决冲突。 c $ c> git rm 。然后再次 git rebase --continue
。重复此步骤,直到解决所有冲突。你现在应该回到你开始的地方,但是改变了历史。
I have a habit of going back to my old commits in git and modify my working directory.For example I have a series of commits
A<-B<-C<-D<-E<-F
In commit B I added some text files.Now I want to add more text files in that commit and record it there.
I have heard that git rebase and git reset --soft should be used here But didn't understand how to use them in practice? Should I use the following command
git rebase HEAD^5
You can use
git rebase -i HEAD~5
this will open a window with a couple of lines that look something like
pick 362trhy4
pick ewq3w5y4
pick 34642wfg
pick 34265wey
pick 34652366
rebase blabla onto blabla
Note that the commit hashes are just made up. What you want to do is go on the line of the commit you want to edit, and change pick
to edit
. Then quit the editor (nano, vim, etc) and confirm.
It will 'bring you back' to the commit you want to edit. You are now free to make changes. Once you are done, git add
or git rm
your changed files, commit amend them (because you want to alter your commit)
git commit --amend -m "new commit message"
Note that this will change your commit hash. Then do
git rebase --continue`
this will make the rebase process continue. Because all the other commits are just pick
s, git will try to reapply them onto your edited commit.
If no conflicts arise: great, you are now done.
If they do: solve the conflicts any way you like and mark the conflicts solved with git add
or git rm
. Then again git rebase --continue
. Repeat this step until all conflicts are resolved. You should now be back at where you started, but with a changed history.
这篇关于在git中修改旧提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!