user2569831
user2569831

Reputation:

Git - how to move commits between branches

I have the following:

branch0  ---  commit1 --- commit2 --- branch2 --- commit3
   |
branch1
   |
commit4
   |
commit5

I would like to change it to the following (i.e. move everything from commit1):

branch0
   |
branch1
   |
commit4
   |
commit5  ---  commit1 --- commit2 --- branch2 --- commit3

I have tried using rebase and cherry-pick, but I am not knowledgeable enough, so any suggestions welcome please.

Thanks

UPDATE

From mimikrija's answer below, I try the following:

git checkout branch0
git checkout -b temp
git rebase branch1
## fix conflicts
git add .
git commit -am "rebase applied"
git rebase --continue
git branch -mv -f branch1

I get the following error:

fatal: Invalid branch name: 'HEAD'

git status
rebase in progress; onto 89844e6
You are currently rebasing branch 'temp' on '89844e6'.
  (all conflicts fixed: run "git rebase --continue")

Upvotes: 3

Views: 340

Answers (2)

mimikrija
mimikrija

Reputation: 113

Assuming these are all local branches (not published yet, or published but not used by anyone else) these would be the steps to take.

git checkout branch2

git checkout -b temp create a temporary branch based on branch2

git rebase branch1 now you have what you drew in your diagram, but it is called temp

git branch -mv -f branch1 force rename the branch

and finally, remove the commits from branch2:

git checkout branch2

git reset --hard branch0

Upvotes: 1

Shubham Khatri
Shubham Khatri

Reputation: 282030

All you would need to do is to take a pull from branch 1 into branch 2

Step:

git checkout branch2
git pull origin branch1

Upvotes: 0

Related Questions