Ivan
Ivan

Reputation: 15932

How to reconstruct a Git branch?

I have a master branch and a es branch. es has been reconstructed locally:

git checkout master
git checkout -b es2
git cherry-pick A C E D 
git branch -D es
git checkout master
git branch -m es2 es

where A, C, D and E are some selected commits in the es branch.

After this I have had to force a push:

git push --all -f

From the beginning until this point nobody has pushed or pulled from the repo.

But everything has gone well. The problem appeared when I tried to rebase a branch in another computer. This user has this branch structure:

master - es - dev

dev has only one commit (say X). This is the problematic sequence of git commands:

git checkout master
git pull
git branch -f es origin/es
git checkout dev
git rebase es

The last rebase produce a lot of conflicts with commits that aren't included in the new es branch, but they are included in master branch. After doing some digging we have found that we can reconstruct the structure by doing this instead of the rebase:

git checkout es
git checkout -b dev2
git cherry-pick X
git branch -D dev
git checkout es
git branch -m dev2 dev

This makes me think that the problem is that the "old" dev branch has some reference or link to the old es branch and that's why the rebase tries to add to the dev branch more than the X commit. I'm wondering if there is another cleaner way to make the users dev folder resynchronize with the repository branches.

NOTE: users never change es or master, just dev.

Upvotes: 1

Views: 460

Answers (1)

user456814
user456814

Reputation:

The first time you tried to do the rebase, you needed to use the --onto flag to tell git to rebase the dev branch onto the new es branch. Using the regular rebase would have only worked if the es branch still had the original commits on it, because that's what rebase uses as a reference point to figure out how to redo the dev branch on top of es.

So the simpler way to do what you wanted would have been

git rebase --onto <new-base> <old-base> <branch>
git rebase --onto origin/es es dev

This tells git to take the dev branch and rebase it onto the version of es from the origin, and it says to use the local, original version of es as a reference point to figure out what to exclude in the rebase (i.e., it will take all commits between es and dev, but not including es itself).

Note that if you already have the dev branch checked out, then you can leave it out of the rebase command:

git rebase --onto origin/es es

Upvotes: 2

Related Questions