GIT - 返回到上一个提交并修改它



我有一个项目,其中有以下提交顺序:

* 800cedc (HEAD, origin/master, origin/HEAD, master) Commit 6
* 1d51716 Commit 5
* 5232f4b Commit 4
* 01838a5 Commit 3
* 3aeb34d Commit 2
* 9b75b72 Commit 1

出于开发原因,我需要做的是:1. 转到提交 5。2. 做一些小的改变。3. 将更改发布到生产环境。4. 将更改添加到提交中。

当我看到我更改的文件在提交 6 中也发生了更改时,问题就来了。

我该怎么做才能更改提交 5 中的文件并将更改与提交 6 合并以保持良好的跟踪所有内容?

谢谢!!

正如@NilsWerner的评论所说,重写已发表的历史不是一个好主意。但如果你真的想,

git reset 1d51716 --hard
# modify the file and
git add path/to/file
git commit --amend
# apply Commit 6 again
git cherry-pick 800cedc 
# there might be conflicts and if any,
# resolve the conflicts, and then
git add path/to/file
git cherry-pick --continue
# force push "master"
git push origin -f master

强制推送后,请告诉其他贡献者您已经重写了master,他们应该强制更新他们的本地master并将他们的未推送提交应用到他们的新本地master,这是一项相当麻烦的任务。

您可以执行以下步骤。

  1. 从 commit5 中创建新分支。

    git branch branchname <sha1-of-commit5>
    git checkout branchname 
    
  2. 您不能对单个文件进行挑选。您只能在提交级别进行挑选。执行要从 commit6 合并的 Commit5(例如 file_a(的特定文件的本地副本。完成此操作后,从 commit6 中签出特定文件。

    git checkout <sha1-ofcommit6> path/to/file_a

  3. 现在,手动执行 commit5 文件(本地副本(与 commit6 文件(在 git 分支中(的合并

  4. 完成更改后,暂存它们并推送到远程。

    git add .
    git commit -m "manual merge for file_a"
    git push origin branchname```
    

尝试:

git revert 1d51716
git revert --no-commit HEAD

然后,做出改变。

不确定,请仔细验证。

顺便说一句:我认为最好的方法是做出新的承诺:-(

最新更新