x-yuri
x-yuri

Reputation: 18983

git: how to fix commit in both branches

Being in branch B, I realized I need to fix commit that exists in both branches A and B. Here what I tried to do, but with no result (only fourth commit's hash changes):

#!/bin/bash -eu
rm -rf 1
mkdir 1
cd 1
git init

echo 1 >1
git add .
git commit -am 1
echo 2 >2
git add .
git commit -am 2
echo 3 >3
git add .
git commit -am 3

git checkout -b B

echo 22 >2
git add .
git commit -am 2

git rebase -i HEAD~3   # fix second commit with fourth one, like this:
                       # pick 485dacc 2
                       # f 976dc2a 2
                       # pick d899817 3

git rebase master || true
echo 22 >2
git add .
git rebase --continue

Upvotes: 1

Views: 62

Answers (1)

Peter Lundgren
Peter Lundgren

Reputation: 9217

Just before your first rebase, your history looks like this:

* ab5f408 (HEAD, B) 2
* ef59c1a (master) 3
* 0a3437c 2
* f62884f 1

Then after:

git rebase -i HEAD~3   # fix second commit with fourth one, like this:
# pick 485dacc 2
# f 976dc2a 2
# pick d899817 3

It now looks like:

* 33211f1 (HEAD, B) 3
* af315cd 2
| * ef59c1a (master) 3
| * 0a3437c 2
|/  
* f62884f 1

To get master where I think you want it:

git checkout master
git reset --hard B

Which leaves you with:

* 33211f1 (HEAD, master, B) 3
* af315cd 2
* f62884f 1

Upvotes: 2

Related Questions