如何将较旧的提交设置为 HEAD?

我意识到我在 HEAD 上犯了一些错误,检查了一个旧的提交,并从那里开始编码。当我尝试推送时,我被告知我当前的提交已经落后,我需要与 HEAD 合并。Git 推荐“ Git pull”。但是,HEAD 有我想要忽略的代码。我如何解决这个问题?

流程图:

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

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

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.

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?

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:

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

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

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.

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 revert commit-id

Cheers

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 -f might be a little problematic for everyone else in the development team

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

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

Please read the documentation first before executing this command.

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

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

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.

This also preserves your repo history.

The steps that worked for me perfectly are following --

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)

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

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)

5) That's it :)

You can update the remote branch more safely with:

git push --force-with-lease

If your remote branch has been committed to since your last pull, it will not reset the branch, but if this bad commit is the last commit, it will update the branch with your current version of the branch. (I've personally aliased this in my .gitconfig file:

[alias]
please = push --force-with-lease

So I can just do

git please

at the command line. :)