Delete last commit in bitbucket

后端 未结 9 1308
隐瞒了意图╮
隐瞒了意图╮ 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 11:12

    By now, cloud bitbucket (I'm not sure which version) allows to revert a commit from the file system as follows (I do not see how to revert from the Bitbucket interface in the Chrome browser).

    -backup your entire directory to secure the changes you inadvertently committed

    -select checked out directory

    -right mouse button: tortoise git menu

    -repo-browser (the menu option 'revert' only undoes the uncommited changes)

    -press the HEAD button

    -select the uppermost line (the last commit)

    -right mouse button: revert change by this commit

    -after it undid the changes on the file system, press commit

    -this updates GIT with a message 'Revert (your previous message). This reverts commit so-and-so'

    -select 'commit and push'.

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

    If you are not working with others (or are happy to cause them significant annoyance), then it is possible to remove commits from bitbucket branches.

    If you're trying to change a non-master branch:

    git reset HEAD^               # remove the last commit from the branch history
    git push origin :branch_name  # delete the branch from bitbucket
    git push origin branch_name   # push the branch back up again, without the last commit
    

    if you're trying to change the master branch

    In git generally, the master branch is not special - it's just a convention. However, bitbucket and github and similar sites usually require there to be a main branch (presumably because it's easier than writing more code to handle the event that a repository has no branches - not sure). So you need to create a new branch, and make that the main branch:

    # on master:
    git checkout -b master_temp  
    git reset HEAD^              # undo the bad commit on master_temp
    git push origin master_temp  # push the new master to Bitbucket
    

    On Bitbucket, go to the repository settings, and change the "Main branch" to master_temp (on Github, change the "Default branch").

    git push origin :master     # delete the original master branch from Bitbucket
    git checkout master
    git reset master_temp       # reset master to master_temp (removing the bad commit)
    git push origin master      # re-upload master to bitbucket
    

    Now go to Bitbucket, and you should see the history that you want. You can now go to the settings page and change the Main branch back to master.

    This process will also work with any other history changes (e.g. git filter-branch). You just have to make sure to reset to appropriate commits, before the new history split off from the old.

    edit: apparently you don't need to go to all this hassle on github, as you can force-push a reset branch.

    Dealing with annoyed collaborators

    Next time anyone tries to pull from your repository, (if they've already pulled the bad commit), the pull will fail. They will manually have to reset to a commit before the changed history, and then pull again.

    git reset HEAD^
    git pull
    

    If they have pulled the bad commit, and committed on top of it, then they will have to reset, and then git cherry-pick the good commits that they want to create, effectively re-creating the whole branch without the bad commit.

    If they never pulled the bad commit, then this whole process won't affect them, and they can pull as normal.

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

    Once changes has been committed it will not be able to delete. because commit's basic nature is not to delete.

    Thing you can do (easy and safe method),

    Interactive Rebase:

    1) git rebase -i HEAD~2 #will show your recent 2 commits

    2) Your commit will list like , Recent will appear at the bottom of the page LILO(last in Last Out)

    Delete the last commit row entirely

    3) save it by ctrl+X or ESC:wq

    now your branch will updated without your last commit..

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