Search code examples
gitbitbucketgit-commitrecover

Recover code from older commits of a particular branch


I have a branch A. In that branch I have done several commits for eg. Commit 5 Commit 4 Commit 3 Commit 2 Commit 1 Where commit 1 is the oldest one. All the commits were pushed successfully and i can even see them in bit bucket. The changes i pushed on commit 3 and commit 4 I can't see those changes in my commit 5. How do i recover that data?


Solution

  • Sounds like your 'commit 5' is not what you intended. You have two possible options.
    One:
    Use

    git revert <commit5-id>
    

    which will create a new commit undoing your 'commit 5'. This is the recommended way forward.

    Two:
    If you have full permissions on the server, and everyone working on the project understands the consequences and agrees,

    git push -f <remote> <commit4-id>:<branch-name>
    

    which will remove 'commit 5' all together.