Delete last commit in bitbucket

后端 未结 9 1330
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 10:17

I made a mistake and I don\'t know how to delete my latest push in the repository. I pull the latest updates of the app but it has conflicts and I push it to repository.

9条回答
  •  无人及你
    2020-12-12 10:53

    Here is a simple approach in up to 4 steps:

    0 - Advise the team you are going to fix the repository

    Connect with the team and let them know of the upcoming changes.

    1 - Remove the last commit

    Assuming your target branch is master:

    $ git checkout master              # move to the target branch
    $ git reset --hard HEAD^           # remove the last commit
    $ git push -f                      # push to fix the remote
    

    At this point you are done if you are working alone.

    2 - Fix your teammate's local repositories

    On your teammate's:

    $ git checkout master              # move to the target branch
    $ git fetch                        # update the local references but do not merge  
    $ git reset --hard origin/master   # match the newly fetched remote state
    

    If your teammate had no new commits, you are done at this point and you should be in sync.

    3 - Bringing back lost commits

    Let's say a teammate had a new and unpublished commit that were lost in this process.

    $ git reflog                       # find the new commit hash
    $ git cherry-pick 
    

    Do this for as many commits as necessary.

    I have successfully used this approach many times. It requires a team effort to make sure everything is synchronized.

提交回复
热议问题