Search code examples
git

How can I recall a pushed commit one week before?


My local commit history is:

commit 5
commit 4
commit 3
commit 2
commit 1

The remote GitLab server commit history is:

commit 3
commit 2
commit 1

commit 2 was pushed to the GitLab server one week ago. Now, I know commit 2 should not be committed, because the content is all Apache temporary files.

How can I delete or cancel commit 2?


Solution

  • The safest thing to do here would probably be to git revert the 2nd commit. First type git log on your branch, which should bring up something like this:

    f83j2l8f commit 5 comment
    439gm93f commit 4 comment
    lsm382fs commit 3 comment
    lm93nd82 commit 2 comment
    9rnj1iu3 commit 1 comment
    

    Find the SHA-1 hash of the second commit, which is lm93nd82 in the example I gave above. A SHA-1 hash will generally look like some really long unintelligible string. Now all you have to do is this:

    git revert lm93nd82
    

    This will create a new commit on top of your branch, which will functionally undo/erase whatever is was that the 2nd commit introduced. Now just push your branch as you normally would:

    git push origin your_branch
    

    Now the remote should show a new commit at the HEAD, and the changes which the 2nd commit introduced should be gone.