1

I am trying to get used to the git repository.

I had pushed a commit with edited new branch to the wrong one (which was added to the one I need). What should i do to cancel the commit?

I work on Windows 7, using TortoiseGit.

1 Answers1

2

To delete the commit you could checkout the branch where you mistakenly created it and reset the last commit (as pointed out on nafas' link):

git reset HEAD^

But if you added the commit to the wrong branch you might want to move it before deleting it. To do so you first need to know its id, which you can get running the rev-parse command when you are checked out in the commit you want to move:

git rev-parse HEAD

This will return the sha, something like 8a011a056ae70bcdd58dfb576552c2d0d2e80047. Now with the cherry-pick command you can bring that commit to the right branch. Checkout the branch where you initially meant to create the commit run the command with the previously obtained id:

git cherry-pick 8a011a056ae70bcdd58dfb576552c2d0d2e80047

That would copy the commit to your current branch. Now you can reset the wrong commit and delete the staged files, or update the wrong branch to one commit before, for instance:

git checkout wrongBranch^
git branch -f wrongBranch

Finally, to update the remote branch with a previous commit you would need to force the push:

git push -f origin wrongBranch
Juan
  • 1,754
  • 1
  • 14
  • 22