Git: reset/revert a whole branch to another branches state?
I have a branch A and a branch B (and some other branches).
Lets say A‘s commit history looks like:
- Is there a way to recover from an accidental “svn revert”?
- What is the meaning of revert this commit and roll back this commit in GitHub for Windows?
- Reverting to a previous revision using TortoiseSVN
- How to revert/recover .m file to a previous version? (autosaved blank by mistake and xcode crashed - no Source Control, no Time Machine)
- GitHub - How to revert changes to previous state
- svn: How to revert somebody else's commit?
- commit 5
- commit 4
- commit 3
And B‘s commit history:
- some other commit
- commit 4
- merge of other stuff from branch C (into branch B)
- commit 3
Basically what I want is to “delete” all changes made by the commits some other commit and merge of other stuff from branch C to branch B.
I want the working tree of branch B to be exactly the same like branch A‘s working tree.
How do I achieve this?
2 Solutions collect form web for “Git: reset/revert a whole branch to another branches state?”
One way to achieve this is through
git reset. While on branch
git reset --hard A
B points to the head-commit of
--hard option resets the index and working tree so that all tracked files are reset to the version in branch
A. The old head-commit of
A is copied to
.git/ORIG_HEAD in order to allow undoing the change.
Alternatively – while not on branch
B – you can delete branch
B and re-created it like this:
git branch -d B # delete branch B git branch B A # re-create branch B and let it point to the commit of branch A
Other than the first suggestion, this will leave the index and working tree untouched.
If you want your branch
B to look exactly like branch
A. You could just do a
git checkout branch-B git reset --hard branch-A
Be careful you will lose commits in this case. Your branch-B will look exactly like branch-A, whatever commits were made to branch-B, that were not present in branch-A, will be lost. Also if branch-B is shared with other people, its not recommended to perform this operation.
In that case you could try reverting the commits you don’t want in branch-B
git revert <sha-of-"some other commit"> git revert <sha-of-"merge of other stuff from branch C (into branch B)">
The second commit looks like a merge commit so you might have to pass the parent as well.
git revert <sha-of-"merge of other stuff from branch C (into branch B)"> -m1