How can I make an older commit HEAD in Git?

Git

Git Problem Overview


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.

Flowchart:

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

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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

Solution 3 - Git

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

Solution 4 - Git

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

Solution 5 - Git

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: https://stackoverflow.com/questions/4624357/git-how-to-overwrite-rather-than-merge-a-branch-on-another-branch

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.

Solution 6 - Git

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.

Solution 7 - Git

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.

Solution 8 - Git

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 :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1449855View Question on Stackoverflow
Solution 1 - GitradtekView Answer on Stackoverflow
Solution 2 - Gituser229044View Answer on Stackoverflow
Solution 3 - GitElenaView Answer on Stackoverflow
Solution 4 - GitigronusView Answer on Stackoverflow
Solution 5 - GitMike MonkiewiczView Answer on Stackoverflow
Solution 6 - GitLemonadeGrenadeView Answer on Stackoverflow
Solution 7 - GitJohan SwanepoelView Answer on Stackoverflow
Solution 8 - GitAnil MView Answer on Stackoverflow