git - 将文件更改从一次提交移至工作副本

标签 git

假设我刚刚进行了 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/

相关文章:

Git:如何分析具有多文件历史记录的代码?

git - 为一位用户禁用 SSH key 身份验证

git - 哪些 Git 提交统计信息很容易提取

git - 'git branch -r' 和 'git remote show origin' 的区别

linux - 如何使用 755/644 和 apache 进行 sftp 上传和 git 推送 :apache rights/owner?

git - 如何将 MLfLow 与私有(private) git 存储库一起使用?

git - 在使用 Hudson/Jenkins 等 CI 工具时,我如何通过 GitHub(或 GitHub 企业版)成功使用 Maven 发布插件

git - 在 Visual Studio Code 中设置自定义 git diff 命令 (--word-diff)

visual-studio-2010 - 从 GIT pull 时自动包含新添加的文件

git checkout refs/heads/master 分离 HEAD