128

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.

How to delete my last commit? Or how to fix it?

catherine
  • 22,492
  • 12
  • 61
  • 85

10 Answers10

118

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
asermax
  • 3,053
  • 2
  • 23
  • 28
  • Thanks for the information. And I know the consequence if I delete the commit, I just don't know how to undo my action because it's my first time got a mistake. I need to undo my commit because the system is already live. We are only two who are working with that repository. – catherine Feb 13 '13 at 16:47
  • `git backout `? Or: `hg backout `? – Armando Pérez Marqués Feb 24 '13 at 16:02
  • But what if the commits added after the one to be reverted had made modifications on the line being deleted? – rahs Jun 01 '18 at 09:21
  • @RahulSaha since what you're doing is basically trying to merge a commit into the branch, I'm pretty sure it'll give you a merge conflict you're going have to solve. – asermax Jun 01 '18 at 12:52
105

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.

Community
  • 1
  • 1
naught101
  • 18,687
  • 19
  • 90
  • 138
  • 1
    For those more comfortable with using SourceTree: 1) To change main branch in the web UI for BitBucket, look at the Repo Settings. 2) To delete a branch in the web UI, look on the Branches page. 3) Reset local repo as per above. 4) Push your local changes up to recreate those branches. – Richard Le Mesurier May 10 '15 at 08:10
  • 2
    As someone who accidentally pushed a secret, I very much appreciated the thoroughness of this answer. Thanks! – Torque Dec 17 '16 at 12:51
62

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.

Shub
  • 2,686
  • 17
  • 26
  • Github will allow this, but as of this writing, Bitbucket does not allow force pushes. – JamesQMurphy Sep 02 '15 at 18:12
  • 4
    actually bitbucket does support force pushes just the diff is instead of completely removing the un-tracked commits it marks them as deleted . – Shub Sep 08 '15 at 15:36
  • The recommended way from bitbucket is using revert. But i think in my case this is more appropriate. Since i'm the only one using it, and it feel more clean than using revert. Thanks. – nafsaka Oct 12 '15 at 05:19
  • 1
    Yes, this works on Bitbucket still. You just saved me big time! I did a commit/push from my Eclipse and got mixed up to other project I had. In part due to a bad practice from my part by using both command line and built-in eclipse for working on git. – DarkCygnus Sep 21 '17 at 23:46
  • brilliant, or easier : reset current branch to this commit , then using that force push to server fix my issue – Anthony Kal May 03 '18 at 05:30
  • This one, even when old, still being the best way to solve this kind of issues! – HayrolR Jan 14 '21 at 16:59
  • Dude, please rename "master" branch to something else... I've just overwritten master branch on remote. Thanks god I had a hotfix branch which I created from master just before that. – user6419217 Jul 03 '23 at 10:27
  • @user6419217 I am sorry to hear that, glad you had a backup branch, please take some lessons from these, never execute commands directly from SO without going through it thoroughly, and protect your master branch by disabling force pushes. – Shub Jul 05 '23 at 03:22
17

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.

rbento
  • 9,919
  • 3
  • 61
  • 61
5

This commands worked Well for me!!

In my case, my teammate has done commit with wrong code. So I have to revert that commit in any case, So I tried to

run this command to REVERT the Commit

git revert [LastCommitID]

After this, changes were not reflected in repository, So for that

I executed this command

git push --force origin master

and this command reflected changes by removing that Last commit

Nachiket Gohil
  • 955
  • 11
  • 17
4

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.

Strip commits link in bitbucket settings

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.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
  • Which version is that, and which permissions are required? It looks different on the v6.4.3 I'm looking at, and this item isn't there. I _am_ repository admin. – Raphael Nov 20 '19 at 08:34
3

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

SudoPlz
  • 20,996
  • 12
  • 82
  • 123
  • 1
    Cutting and pasting is really messy. Plus, this wouldn't solve the problem from the question. – Daniel Lee May 27 '13 at 09:06
  • 1
    `git revert` is simple: it creates a new commit that does the reverse of the changes in a previous commit (or multiple commits). It doesn't delete commits, so it's not relevant for this question. Also, the cloning step here isn't really necessary. – naught101 Sep 01 '14 at 07:32
  • 1
    Git revert is the equal mechanism as above. – nafsaka Oct 12 '15 at 05:23
1

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
invzbl3
  • 5,872
  • 9
  • 36
  • 76
0

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)

enter image description here

Delete the last commit row entirely

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

now your branch will updated without your last commit..

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
0

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'.

smriti
  • 11