有什么方法可以帮助在git中恢复部分历史记录提交?

时间:2012-07-10 04:05:56

标签: git git-revert

假设我在git存储库中有以下提交历史记录(所有更改都在一个文件上进行)

  1. 删除功能foo()bar(),修改功能baz()
  2. 改变别的东西
  3. 现在我想重新添加函数foo()。我想要的是手动交互方式来做到这一点。例如,编辑器以并排差异模式打开。一方面是提交#1之前的版本(包括函数foo()),另一方面是我的本地工作副本。这样我就可以将我想要的任何东西从历史方面复制到工作副本。

    是否有任何git命令可以帮助我这样做?

    由于

1 个答案:

答案 0 :(得分:0)

这是一个解决方案,这是我可能会做的:

# 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 .
相关问题