porton
porton

Reputation: 5827

Reverting some changes in Git

In my Git repository I forked branch devel from branch master.

After this I did some changes in devel and committed a few times.

Now I realize that some of these changes were wrong.

What is the best method to revert (that is patch in the reverse direction) some (but not all) of changes (of devel relatively to master)? I need to review each change (whether to revert it) and then how to revert the given changeset?

Upvotes: 0

Views: 254

Answers (2)

David Deutsch
David Deutsch

Reputation: 19045

For each commit (in reverse chronological order) that has mistakes, do:

git revert <sha of commit> --no-commit
git reset head

This will make your working copy reflect the repository, minus all the changes introduced in the specified commit. You just want to revert some of the changes; to do this, you will want to add just those reversions to the index. You can do this with the command line, but that can be quite cumbersome. I would recommend using a tool like Source Tree instead; with that, you can just visually select which hunks in which files you want to stage.

Once you are satisfied that the index contains all the reversions you want, go ahead and git commit them, then do a git checkout . to clean up your working copy. Then move on to the next commit you want to (partially) revert.

Note that if your mistakes are spread over a bunch of commits and you do not want to revert each commit individually, you can pass a range of commits to git revert as well.

Upvotes: 2

CodeWizard
CodeWizard

Reputation: 142652

Now I realize that some of these changes were wrong.... What is the best method to revert

Read it all here: How to move HEAD back to a previous location? (Detached head)

It will explain tpo you how to revert, checkout and some other useful information as well.

for example:

git reflog

You can always use the reflog as well

git reflog
git checkout HEAD@{...}

This will get you back to your desired commit

enter image description here

Upvotes: -1

Related Questions