Delete last commit in bitbucket

后端 未结 9 1307
隐瞒了意图╮
隐瞒了意图╮ 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 <commit_hash>
    

    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.

    0 讨论(0)
  • 2020-12-12 10:57

    As others have said, usually you want to use hg backout or git revert. However, sometimes you really want to get rid of a commit.

    First, you'll want to go to your repository's settings. Click on the Strip commits link.

    Enter the changeset ID for the changeset you want to destroy, and click Preview strip. That will let you see what kind of damage you're about to do before you do it. Then just click Confirm and your commit is no longer history. Make sure you tell all your collaborators what you've done, so they don't accidentally push the offending commit back.

    0 讨论(0)
  • 2020-12-12 11:00

    You can write the command also for Bitbucket as mentioned by Dustin:

    git push -f origin HEAD^:master
    

    Note: instead of master you can use any branch. And it deletes just push on Bitbucket.

    To remove last commit locally in git use:

    git reset --hard HEAD~1
    
    0 讨论(0)
  • 2020-12-12 11:01

    I've had trouble with git revert in the past (mainly because I'm not quite certain how it works.) I've had trouble reverting because of merge problems..

    My simple solution is this.

    Step 1.

     git clone <your repos URL> .
    

    your project in another folder, then:

    Step 2.

    git reset --hard <the commit you wanna go to>
    

    then Step 3.

    in your latest (and main) project dir (the one that has the problematic last commit) paste the files of step 2

    Step 4.

    git commit -m "Fixing the previous messy commit" 
    

    Step 5.

    Enjoy

    0 讨论(0)
  • 2020-12-12 11:07

    In the first place, if you are working with other people on the same code repository, you should not delete a commit since when you force the update on the repository it will leave the local repositories of your coworkers in an illegal state (e.g. if they made commits after the one you deleted, those commits will be invalid since they were based on a now non-existent commit).

    Said that, what you can do is revert the commit. This procedure is done differently (different commands) depending on the CVS you're using:

    On git:

    git revert <commit>
    

    On mercurial:

    hg backout <REV>
    

    EDIT: The revert operation creates a new commit that does the opposite than the reverted commit (e.g. if the original commit added a line, the revert commit deletes that line), effectively removing the changes of the undesired commit without rewriting the repository history.

    0 讨论(0)
  • 2020-12-12 11:10

    you can reset to HEAD^ then force push it.

    git reset HEAD^
    git push -u origin master --force
    

    It will delete your last commit and will reflect on bitbucket as commit deleted but will still remain on their server.

    0 讨论(0)
提交回复
热议问题