Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Branch Off A Branch

Tags:

git

github

I'm working off of master and create Branch 'A'.

Branch 'A' contains HTML/CSS/JS to create a 'widget'.

While this code is being reviewed I also want to work on creating tests for this 'widget'.

I can't work off of master yet because Branch 'A' hasn't been merged. But I need a way to work off of Branch 'A' without making updates to it while the code is being reviewed to push to master.

I figure I need to make Branch 'B' off of Branch 'A' so that I can continue working off of the code I had already created.

Question 1. How can I do this in git?

Question 2. Once I merge Branch 'A' to master, will that also include Branch 'B' even though i'm not done yet?

Question 3. Can I merge Branch 'B' independent of Branch 'A'?

like image 769
sjmartin Avatar asked Dec 15 '15 23:12

sjmartin


People also ask

Can you branch off a branch in GitHub?

On GitHub.com, navigate to the main page of the repository. Optionally, if you want to create the new branch from a branch other than the default branch of the repository, click Branches then choose another branch.

What does branch off mean in git?

It's trivial - you can create a branch off any branch in git. If you're on branch A, simply do git checkout -b B and you'll have a new branch starting at A. It will be separate from A, and changes from A will not be reflected in B. When A is merged to master, the merge will not bring in the commits on B.

How do I disconnect from a branch in git?

Remove remote Git branch from GitHub or GitLab To do this, you must issue the following Git command to remove a branch from a remote repository: git push origin --delete name-of-branch-to-remove.


1 Answers

Question 1. How can I do this in git?

git checkout branch_a
git pull
git checkout -b branch_b

You'll then have the commits from branch_a in your new branch_b.

Question 2. Once I merge Branch 'A' to master, will that also include Branch 'B' even though i'm not done yet?

No, they're entirely separate branches so only the commits from branch_a will exist in master.

Question 3. Can I merge Branch 'B' independent of Branch 'A'?

Sort of a two-pronged approach here. If you want to take branch_a commits with you, then you can merge branch_b into master at any time, although that sort of voids the purpose.

Given that branch_b's tests presumably depend on branch_a it would seem silly to merge B before A.

Once you have merged A into master, you will probably need to rebase branch_b onto the new master, and get rid of all of the original commits you took over from branch_a, since they all now exist in master:

# Assume A merged into master
git fetch
# Rebase interactively
git rebase -i origin/master
# Exclude (skip or comment out) the commits that were from A, leaving only B
# Rebasing (n/n).....
git push origin branch_b --force

This will give you a fresh branch_b from master, where the commits from A now exist, and you won't have merge conflicts when you merge B into master.

The force push will only be required if you've already pushed branch_b to origin. If you haven't, no problem.

like image 74
scrowler Avatar answered Oct 01 '22 05:10

scrowler