Search code examples
gitgithubgit-pushgit-commitgit-reset

Revert commits back after the head was changed and pushed


I've committed some bad code a few days ago and today I changed and pushed an older head to master.

Before changing head:

commit 4

commit 3 - bad code starts here

commit 2 - i set the head here and pushed

commit 1

After changing head:

commit 2 - current head

commit 1

The problem is that now I want those 'bad commits' back.

It is possible to get them back?


Solution

  • You can use git reflog to solve your problem:

    git reflog
    a8U3Ild... HEAD@{10}: commit 3
    e3Noj1q... HEAD@{11}: commit 4
    

    Find the SHA-1 hashes of the commits which you deleted. The commit you want is the one for "commit 4" which was the head of the branch before you rolled things back. Then you can reset your current branch to "commit 4" like this:

    git checkout master
    git reset --hard e3Noj1q