13

A local branch:-

'feature/100'

And Remote branches:-

'master'

'Version2'

Accidently,

  1. I have merged my feature branch 'feature/100' to the master
  2. Also pushed it to the remote repository.

But in real 'feature/100' should have been merged into remote branch 'Version2'

How I fixed it (partially):-

i have merged the feature branch 'feature/100' to the remote branch 'Version2' and pushed it to the server.

git checkout Version2
git merge --squash feature/100
git add .
git commit -m 'New message'

But I want to delete the last push I have merged and commit to the master branch. How?

Side Note I am only one working on this project.. So even if pushed commit is deleted it won't harm anyone else

Community
  • 1
  • 1
user1327064
  • 4,187
  • 5
  • 20
  • 30
  • 1
    @knittl: coz i wanted to merged my feature branch in one commit – user1327064 Sep 06 '12 at 17:53
  • [How can I undo a `git commit` locally and on a remote after `git push` - Stack Overflow](https://stackoverflow.com/questions/6459080/how-can-i-undo-a-git-commit-locally-and-on-a-remote-after-git-push) ? (although this one has a merge?) – user202729 Feb 22 '21 at 11:07

3 Answers3

25

You can either:

Revert your change

git revert HEAD

This will create a new commit that reverts the changes that you just pushed up to master. This is the safest option because other people may have already pulled down the change that you have pushed up.

Change your commit history and force push the change

You can remove the commit that you just pushed up with:

git reset --hard HEAD~1

git push origin master --force

You don't want to do this unless you're absolutely sure that no one has pulled down your changes from master.

For more info, see Delete commits from a branch in Git

Community
  • 1
  • 1
austen
  • 3,314
  • 3
  • 25
  • 26
2

It's probably too late, but if you want to rewind your pushed branch (master?) by one commit, issue the following command:

git push origin +master^:master

+ makes the push forced, master^ describes the previous-last commit. :master pushes to the remote master branch.

knittl
  • 246,190
  • 53
  • 318
  • 364
0

You can revert the merge commit on master. When you later really want to merge the branch, you will have to revert the revert first. For a detailed explanation, see here:

http://www.kernel.org/pub/software/scm/git/docs/howto/revert-a-faulty-merge.txt

robinst
  • 30,027
  • 10
  • 102
  • 108