假设我在 git 存储库中有以下提交历史记录(所有更改都在一个文件上进行)
- 删除函数
foo()
和bar()
,修改函数baz()
- 改变别的东西
现在我想重新添加功能foo()
。我想要的是一种手动交互的方式来做到这一点。例如,编辑器以并排差异模式打开。一方面是提交 #1 之前的版本(包括功能foo()
),另一方面是我的本地工作副本。这样我就可以将任何我想要的东西从历史端复制到工作副本端。
有没有git
命令可以帮助我做到这一点吗?
谢谢
假设我在 git 存储库中有以下提交历史记录(所有更改都在一个文件上进行)
foo()
和bar()
,修改函数baz()
现在我想重新添加功能foo()
。我想要的是一种手动交互的方式来做到这一点。例如,编辑器以并排差异模式打开。一方面是提交 #1 之前的版本(包括功能foo()
),另一方面是我的本地工作副本。这样我就可以将任何我想要的东西从历史端复制到工作副本端。
有没有git
命令可以帮助我做到这一点吗?
谢谢
这是一种解决方案,我可能会这样做:
# Stash any current modifications to be safe.
git stash
# Make a new commit that re-adds foo, bar and maz
git revert <sha of the commit with your function>
# Remove the commit you just created, but leave the file changes.
git reset HEAD^
# Interactively decide which chunks of code you actually want.
# Just follow the instructions, type '?' for help.
git add -p
# Commit all of the changes that you staged.
git commit
# Discard all the other stuff you didn't accept.
git checkout .