3

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")
  • This is doing something similar, but only for one commit, https://stackoverflow.com/questions/3710192/how-do-i-move-a-commit-between-branches-in-git –  Oct 16 '19 at 08:12

2 Answers2

1

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

mimikrija
  • 113
  • 7
  • I am little confused, about the `branchA` and `branchB` you use. I have `branch0`, `branch1` and `branch2`. How do they correspond to those? I have those 3 branches, which each have some commits. –  Oct 16 '19 at 09:15
  • When I try `git branch -mv -f branch1`, I get the following: `fatal: Invalid branch name: 'HEAD'`. –  Oct 16 '19 at 09:19
  • @user2569831 which branch does `commit3` belong to? I was confused with your diagrams. – mimikrija Oct 16 '19 at 09:22
  • Sorry, if it's not too clear. I added an UPDATE to the question with the error I am getting. `commit3` belongs to `branch2`. `branch1` and `branch2` was created off `branch0`. –  Oct 16 '19 at 09:25
  • @user2569831 thanks for the update. Obviously you are still in a rebase (status output) which means not all the conflicts were fixed. once you have a clean `status` you may continue with renaming branches. – mimikrija Oct 16 '19 at 09:50
  • @user2569831 please have a look at the edited question. I think you've mixed up the branches in your rebase. – mimikrija Oct 16 '19 at 09:53
  • thanks for the update. Just a question, what about the `commit1` and `commit2`? I don't think they will be moved to the the `branch1`. –  Oct 16 '19 at 10:41
  • @user2569831 they should be there, unless the changes contained in `commit4` and `commit5` already include the same changes `commit` and `commit2` introduced. In that case,during a rebase, `commit1` and `commit2` become obsolete (they don't introduce a change) therefore they are gone. BTW to get a better understanding of this please read: https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase – mimikrija Oct 16 '19 at 10:58
  • I cannot seem to get it to progress on the rebase. I have resolved conflicts by doing a `git commit -am "fix conflicts from rebase"` then `git rebase --continue`, but `git status` always says `rebase in progress; onto 2636118 You are currently rebasing branch 'temp3' on '2636118'. (all conflicts fixed: run "git rebase --continue") nothing to commit, working tree clean`. Why can't I proceed with the `rebase`? –  Oct 16 '19 at 11:26
  • First of all I think you have your branches mixed up. You should be on branch `temp` and while there, run `git rebase branch1` . I am confused with the commit hash you get in the output. – mimikrija Oct 16 '19 at 12:21
0

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
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400