4

I made changes to several files, committed and pushed them to the master only to realize I was on the wrong branch for the commits and push. How can I undo this? I am not sure if git reset will give me what I want.

2
  • for easier convenienve. let's say you have a branch A. Did you commit stuff on master and pushed to master on the remote repo? So your branch A actually doesn't have anything and you want your stuff on that branch and revert master to what it was? Commented Sep 10, 2015 at 15:07
  • Since branches are just pointers to commits, you can change these pointers to point to whatever commits you want. You might need to use reset and merge --ff-only to keep your tree intact. Commented Sep 10, 2015 at 18:34

1 Answer 1

4

To avoid having to rewrite the history you can use git revert.

This will create a new commit that is the inverse of the commit you accidentally pushed.

Find the sha's of the commits you pushed and run this command for each sha:

git revert <sha> 

Then push your revert commits.

Sign up to request clarification or add additional context in comments.

1 Comment

I did 'git revert' just now and it seems to be exactly what I was looking for. Thanks for posting this! I've had a lot of trouble keeping my files going to the right branches and this was just what I've needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.