Reputation: 1129
A-->B
I have branch B
which was branched out from branch A
. I then had some changes to be done in branch A but accidentally committed the changes to branch B on Github (remote). I found solutions that i can do git reset --hard <commit_id>
or git revert
.
So this was what I did on Branch B:
git revert HEAD
git reset --hard <commit_id>
git checkout A
and did the corrections neededgit push origin A
However my Branch B still has the wrong commits and i wanna update my branch B since it's branched out from Branch A. How can i do that ? Can I do
git pull --rebase origin A
Upvotes: 1
Views: 4426
Reputation: 18577
The easiest way to do this is by specifying --onto
in a rebase.
I'm assuming that you have two local branches, A
and B
. Right now A
has what you want, and you want to update B
so that it's a branch off of A
, and you also want to remove some commits.
Your best friend in dealing with these issue is git log
. To make it easier to visualize what's going on, use git log --oneline --graph
.
The first step is to find the first commit that's unique to branch B.
git checkout B
git log --oneline --graph
Find the first commit that is unique to branch B (i.e., isn't part of branch A) and copy its commit ID. For illustration, I'm going to assume the commit ID is 4b84d26
.
To rebase branch B
onto the current version of branch A
, do
git rebase --onto A 4b84d26
where 4b84d26
would be whatever commit ID you copied from before. This should make B
based on A
again.
Next, do git log --oneline --graph
again and find the commit ID that points to the commit you want to return to (i.e., the commits you accidentally made). This commit ID will have changed from before due to the rebase. Again, you should do git revert --hard <commit_id>
and your branch B should have the correct history.
Note that in doing this, you're rewriting history, so when you push you will have to do a --force
push, which could make things difficult for anyone you're collaborating with. Best to let them know you're doing a force push before doing it!
Upvotes: 2