28

I accidently pushed a feature branch to the remote master. Now I want to revert the master to the previous commit. When I select 'Revert current branch to this commit' only the local branch is reverted, and because the remote master is 55 ahead (accidental push) I cannot push the just reverted local master to the remote.

When looking into this issue on SO and Google, I found that many people use force push using the command line. But since I'm using Source Tree I'd like to come up with a way that actually uses Source Tree.

I also found the rebase option, but I can't seem to find a step-by-step tutorial..

Any ideas?

KrekkieD
  • 967
  • 9
  • 23
Michael Kloet
  • 281
  • 1
  • 3
  • 3

3 Answers3

30

When you push a commit, the safest way to revert it (rather than forcing the push with -f) is to use the revert function, so a new commit is created on top of your previous commit.

This is possible to do using Sourcetree, right clicking in the commit that you want to revert, and selecting "Reverse commit...".

enter image description here

You would need to do this for each commit you want to revert, in reverse order.

Jesús Carrera
  • 11,275
  • 4
  • 63
  • 55
4

The best way to do this is with Reset <branch> to this commit option shown in the image below.

Reset to previous commit

A pop up will be displayed to you to choose what mode of code reset you would like to choose. [See below] Choose the mode of reset

Note: This is not exactly code reversal, but it removes your commit entirely from the local and makes it look cleaner.

  • 10
    This only affects the local branch. If you've already pushed your commit, this just erases the data from your local repo - the remote will will have all the changes though. – Mage Xy Jan 23 '18 at 23:00
  • 1
    If you read Atlasssian git best practice, hard reset is only allowed on local level. If u already pushed branch to remote, then just click 'revert this commit', you push to remote after watching code been reverted. The previous `bad` commit still sit there meanwhile latest push did a revert effect on it. :) source:https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting – CodeFarmer May 23 '18 at 04:52
  • 1
    This is the answer i need for my scenario. Because i already pushed in my local machine alone. But i want to delete that pushed commits in local branch. This one helps me perfectly. Thanks to @LearnerAllTheWay – Arun Prasad May 28 '18 at 10:35
1

Sourcetree doesn't expose the -f flag to you: https://answers.atlassian.com/questions/54469/how-do-i-perform-a-forced-push-push-f-from-sourcetree

So, the only way to bring the remote back to it's original state is to git revert each commit you introduced in the reverse order.

Assuming the remote was on commit A and you pushed your feature branch holding commits B, C and D, you have to

git revert D
git revert C
git revert B
git push

The push will work fine since you're not modifying the pushed history but instead push a new set of changes.

If you want to shorten your series of reverts to one commit, you could do a

git rebase -i origin/master

and select each of your revert commits to be squashed. Then, your push will only contain one commit that reverts D, C and B in one go.

eckes
  • 64,417
  • 29
  • 168
  • 201
  • Most of the sourcetree related answers I found say that `-f` is not required, and that it can be done with `rebase`. I'm kind of assuming that there is a sourcetree way to make this happen, but your answer sort of says it can't be done using just sourcetree? Most other answers on SO also show the actual GIT commands, and not the sourcetree steps. I'm not GIT-savvy enough to make that translation myself. – KrekkieD Oct 27 '14 at 15:15
  • this cannot be done with rebase. If you're willing to use the console, specify the `-f` flag there. – eckes Oct 27 '14 at 15:24
  • So it's not possible using only SourceTree? – Michael Kloet Oct 29 '14 at 12:13
  • @MichaelKloet, yes it's possible using only Sourcetree, see my answer – Jesús Carrera Sep 01 '15 at 09:30