假设我刚刚进行了 commit1
,其中有两项更改:
- 修改
fileA.txt
- 修改
fileB.txt
然后我意识到我犯了一个错误 - 我还没有准备好提交我的 fileB.txt
更改!我希望 fileB.txt
修改位于我的工作副本中,并且我想修改之前的 commit1
以仅包含 fileA.txt
修改。所以最终的结果是:
提交1
- 修改
fileA.txt
工作副本
- 修改
fileB.txt
实现这一目标的最佳方法是什么?是否可以在不改变分支的情况下做到这一点?
最佳答案
当然......这样做:
git reset --soft HEAD~1 # asking git to move the branch _pointer_ one revision back.... files won't change, both files will end up on index
git reset fileB.txt # take fileB out of index
git commit -m "Here´s the revision, only fileA is affected"
现在 fileB 在工作树上被修改。另一种方法是:
git checkout HEAD~ -- fileB.txt # get fileB as it was before
git commit --amend --no-edit
git checkout HEAD@{1} -- fileB.txt # get fileB from the previous broken revision
git commit -m "some comment"
这应该有效。 (如果您对使用 HEAD@
引用没有信心,请在第二次提交时使用修订 ID。修订 ID 可以在 git reflog
中看到)
关于git - 将文件更改从一次提交移至工作副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55301996/