Showing posts with label git-push. Show all posts
Showing posts with label git-push. Show all posts

Friday, January 26, 2018

How to revert a merge commit that's already pushed to remote branch | Revert a merge after being pushed | Undo a git merge that has been pushed to the server | Git HowTo: revert a commit already pushed to a remote repository

You could follow these steps to revert the incorrect commit(s) or to reset your remote branch back to correct HEAD/state
1. git checkout development
2. Use git log -5 to show latest 5 commits, which will result as below:
$ git log -5
commit 6e10182080307215e70d5d843d29ddfb302d776f (HEAD -> development, origin/development, origin/HEAD)
Author: Pritom K Mondal <pritomkucse@gmail.com>
Date:   Thu Jan 25 20:53:19 2018 +0600

    Test

commit f262e20f51171d2d24d9f15cd2abcf73a641ac43
Author: Pritom K Mondal <pritomkucse@gmail.com>
Date:   Thu Jan 25 20:52:34 2018 +0600

    Test

commit f808058d419e9d71b9f4bc27bf272241c0bf9971
Author: Pritom K Mondal Kumar <pritomkucse@gmail.com>
Date:   Thu Jan 25 14:45:54 2018 +0000

    Repository created
3. git reset f262e20f51171d2d24d9f15cd2abcf73a641ac43 (i.e. your commit number where you want to go)
4. run the git status to show all the changes that were part of the wrong commit, if you don't want to see untracked files run git status --untracked-files=no or as shortcut run git status -uno
5. simply run git reset --hard to revert all those changes
7. force-push your local branch to remote and notice that your commit history is clean as it was before it got polluted.
git push -f origin development
8. But by any chance if you need to abort reset you need to revert above reset option as git reset 'HEAD@{1}' as short answer.
9. Long answer is type git reflog and go to desired location.
$ git reflog
1123175 (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: reset: moving to HEAD
1123175 (HEAD -> master, origin/master, origin/HEAD) HEAD@{1}: reset: moving to HEAD
1123175 (HEAD -> master, origin/master, origin/HEAD) HEAD@{2}: reset: moving to 1123175d5b42a8f459aff1cd7bb59082e0087775
b1110db HEAD@{3}: merge remotes/origin/branch1: Merge made by the 'recursive' strategy.
1123175 (HEAD -> master, origin/master, origin/HEAD) HEAD@{4}: commit: Test from master
d81337f HEAD@{5}: commit: Test from master