Revert committed changes using Git

Sometimes, I am fixing bugs or adding functionality that is committed and pushed to the remote repository. Then someone else should that should test or make some decisions do not want the change or want to test it before after some time. Then it is good to keep the changes in a local branch and revert the change in the main branch. Doing this is pretty easy and straight forward.

$ git log --oneline
d200b72330 Bugfix
450c8586e7 Other changes
96cfa71fe3 More earlier changes

When checking the git log, the commit that I want to revert is d200b72330. First thing I want to do is make sure I have the changes later when the change is wanted.

$ git checkout d200b72330
$ git switch -c BugfixIWantLater
$ git checkout master

When checking out the commit, it is checked out in a detached head state and the next thing is to make a git switch -c <branch name> to make a local branch of it. If I just want to go back and remove changed made in detach head state git switch -. I create a local branch and then go back to the main branch again.

$ git revert d200b72330
$ git push origin master

Now I have saved the changes that I had in the commit in a separate branch, I can just revert the commit and push to the central repository.

Teis Lindemark

Read more posts by this author.