0

I had a branch that

A---->my commit B--->other commit C-->current D

I want to revert "my commit B", but other commits stay the same.

how can I achieve this?

what I want is we will still develop on this branch. the git history would be: A---->my commit B--->other commit C-->current D --> revert my commit B

change to another branch can't accept, cause this branch is shared with others

yang yang
  • 159
  • 1
  • 12
  • 4
    `git revert ` – Ry- Apr 10 '17 at 01:55
  • Well.... this also works.... if you want to keep "commit B" in the history of the branch. If you want to make it look like as if commit B never happened, cherry-picking is your friend. – eftshift0 Apr 10 '17 at 01:57

3 Answers3

6

The correct way to do exactly what you describe is to use the git revert command.

You would find the offending commit(s), checkout the tip of your branch and execute this command if it is only one commit you want to revert:

git revert SHA-OF-COMMIT

If there is more than one commit, in sequence, that you want to revert you would do this:

git revert SHA-OF-OLDEST-COMMIT..SHA-OF-NEWEST-COMMIT

If you want to revert multiple commits that aren't sequential (there are other commits inbetween), do the revert operation in multiple steps.

In either case new commit(s) will be created that reverse the changes introduced by the commit(s) you wanted to revert. You do not lose the original commits, they're still part of history, but the changes they introduced has been removed further down the history.

This is the safest route if your repository is a shared one as it only creates new commits. git revert is not the command to use if you need to completely eradicate all traces of the commit, including historically, then you would need to do history rewriting but this does not seem to fit with your question.


Tip: Make a complete copy of your entire working folder and git repository locally before you start to ensure you don't do anything that requires extensive cleanup.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • thanks a lot. this would be helpful, but can you write more details about "If you want to revert multiple commits that aren't sequential (there are other commits inbetween), do the revert operation in multiple steps." – yang yang Apr 13 '17 at 02:34
-1
git checkout -b corrected-branch A
git cherry-pick C
git cherry-pick D

Or

git checkout -b corrected-branch A
git cherry-pick B..D

Or

git checkout -b corrected-branch A
git cherry-pick D~2..D
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • Sorry , but I think what you do is create a new branch? my intends is to to use the branch still, cause other guy are all develop on this branch. – yang yang Apr 10 '17 at 02:01
  • That doesn't matter. Branches are just "pointers to revisions". They can be created/deleted at will. If you are comfortable working on detached HEAD, just try ```git checkout A```, then do your cherry-picking and repoint your original branch to the resulting revision. Potato/Potato. – eftshift0 Apr 10 '17 at 02:04
-2

EDIT (Question changed): This is not a good solution on a shared branch, since changing the history will be painful to everyone else sharing the branch. Go with "revert" in this case.

Interactive Rebasing: git rebase A -i (-i for "interactive") will let you pick and choose which commits to keep and which to discard.

More info: How to run git rebase interactive mode to remove duplicate commits

willoller
  • 7,106
  • 1
  • 35
  • 63
  • 1
    This answer might benefit from a warning about rewriting shared history, it sounds from the question like this is definitely a shared repo... – Dan Lowe Apr 10 '17 at 03:00