如何在 Git 中制作较旧的提交 HEAD?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13956207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:07:07  来源:igfitidea点击:

How can I make an older commit HEAD in Git?

git

提问by user1449855

I'm still trying to learn the (basic?) finer points of Git and have managed to get myself into trouble. I realized that I made some mistakes on HEAD, checked out an older commit and started coding from there. When I attempt a push I'm told that my current commit is behind and I need to merge with HEAD. Git recommends "git pull". However, HEAD has the code I want to ignore. How do I solve this problem? Thanks so much for the help.

我仍在努力学习 Git 的(基本?)更精妙之处,并设法让自己陷入困境。我意识到我在 HEAD 上犯了一些错误,检查了一个较旧的提交并从那里开始编码。当我尝试推送时,我被告知我当前的提交落后了,我需要与 HEAD 合并。Git 推荐“git pull”。但是, HEAD 有我想忽略的代码。我该如何解决这个问题?非常感谢帮忙。

Flowchart:

流程图:

-------- HEAD (bad) ---------------------- + (behind conflict, requires
     \                                    /   merge with HEAD, which is
      \------- Current commit (good) ----/    bad and needs to be ignored)

采纳答案by meagar

If your repository isn't being used by other people, you can safely do git push -fto overwrite the remote branch.

如果您的存储库没有被其他人使用,您可以安全地git push -f覆盖远程分支。

回答by radtek

Here is what you can do:

您可以执行以下操作:

git checkout <branch-to-modify-head>
git reset --hard <commit-hash-id-to-put-as-head>
git push -f

If you don't force the push, git will throw this error: Updates were rejected because the tip of your current branch is behind.

如果你不强制推送,git 会抛出这个错误:更新被拒绝,因为你当前分支的提示在后面。

Note that this will tamper your git history, so another way of doing this is revert each commit you don't want. That way you retain your history:

请注意,这会篡改您的 git 历史记录,因此另一种方法是还原您不想要的每个提交。这样你就可以保留你的历史:

git revert commit-id

Cheers

干杯

回答by Elena

The way I do it is:

我这样做的方式是:

git reset --hard <commit-SHA>
git push origin HEAD:<name-of-remote-branch>

It's the way git recommends and doing git push -fmight be a little problematic for everyone else in the development team

这是 git 推荐的方式,并且这样做git push -f对开发团队中的其他人来说可能有点问题

回答by Mike Monkiewicz

ANeves is right, "git push -f" only works because you were the only person using the repository. This is not an acceptable solution for most people.

ANeves 是对的,“git push -f”之所以有效,是因为您是唯一使用存储库的人。对于大多数人来说,这不是一个可接受的解决方案。

Here's your current commit history:

这是您当前的提交历史记录:

---A-B < HEAD (bad)
    \
     C < my_branch (good)

This has the solutions you want: How do I 'overwrite', rather than 'merge', a branch on another branch in Git?

这有您想要的解决方案: 如何在 Git 中的另一个分支上“覆盖”而不是“合并”一个分支?

To recap,

回顾一下,

git checkout my_branch
git merge -s ours HEAD

This will stomp all the changes on HEAD's branch, and give you the following:

这将践踏 HEAD 分支上的所有更改,并为您提供以下信息:

--A-B-D < HEAD, my_branch (both good)
   \ /
    C

D is effectively the same as C in this case, it just has different parents.

在这种情况下,D 实际上与 C 相同,只是有不同的父母。

回答by igronus

The only thing that worked for me:

唯一对我有用的东西:

git checkout <OLD_COMMIT>
git branch temp
git checkout temp
git branch -f master temp
git checkout master
git branch -d temp

回答by LemonadeGrenade

For those of us working on protected branches, push -f isn't an option.

对于我们这些在受保护分支上工作的人来说,push -f 不是一个选项。

Instead:

反而:

Checkout HEAD
diff {hash of desired commit to use as new HEAD} > myChange.patch
git apply 
commit 
push

If you have changes you'd like to merge into the new version of HEAD like OP, I would back them up first, correct the remote repo, then apply the changes.

如果您希望将更改合并到新版本的 HEAD 中,例如 OP,我会先备份它们,更正远程存储库,然后应用更改。

This also preserves your repo history.

这也会保留您的回购历史记录。

回答by Johan Swanepoel

I'm a bit late to the party - I had to do:

我参加聚会有点晚了 - 我必须这样做:

git push -f origin HEAD:<name-of-branch>

回答by Anil M

The steps that worked for me perfectly are following --

对我来说非常有效的步骤如下——

1) git log --oneline

1) git log --oneline

2) Grab the commit that you want to rollback (most likely the commit before your last commit at HEAD and push)

2) 获取您想要回滚的提交(很可能是在 HEAD 上一次提交之前的提交并推送)

3) git checkout (this is the commit id to where you want your work to rollback to)

3)git checkout(这是您希望工作回滚到的提交ID)

4) git push -f origin HEAD:master (-f will force the push overriding any rejection that would happen if pushed branch is behind the remote) HEAD:master(This is to ensure you are pushing the rollback to the master branch and at HEAD of the remote repo)

4) git push -f origin HEAD:master (-f 将强制推送覆盖如果推送分支在远程后面会发生的任何拒绝) HEAD:master(这是为了确保你将回滚推送到主分支和远程仓库的负责人)

5) That's it :)

5) 就是这样:)