0

Is it possible to undo one or two faulty commits I have made to Github. I'm not very good at coding either so if there's a way to do it without coding, that would be great.

Thank you very much

0

1 Answer 1

2

You can do this via doing the following command in your local workspace:

git reset --merge <hash>

Where <hash> is the commit prior to the commits that you want to revert.

The above command is enough if you haven't pushed to GitHub. If you have pushed to GitHub, you then run the following to force an update of the remote branch:

git push --force origin <branch>

(Note: This is assuming that your remote is named origin, but rename as appropriate.)

Note that this re-writes history on the remote branch, and is not recommended if other developers are using the same branch. If you know who the developers are, you can communicate with them to delete their local copies of the modified branch and ask them to run git fetch to get a new copy of the branch.

3
  • Don't use the --hard flag, use --merge in order to avoid losing changes in working directory (either staged or unstaged). Jul 13, 2016 at 21:56
  • Huh? When do you 'git push' to GitHub... before or after 'git reset'?
    – zipzit
    Jul 13, 2016 at 21:57
  • @zipzit - After git reset. git reset updates your local branch pointer to the <hash> commit. git push --force updates the remote branch on GitHub. Jul 13, 2016 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.