Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make branch B exactly like branch A?

Tags:

git

Say I have an existing branch B (if it makes a difference, it's remote).

I also have a branch A (either local or remote). I would like branch B to be exactly like A. If I understand correctly, merge doesn't make the branches identical, there still can be something in branch B that wasn't on branch A and it will remain there after doing git checkout B followed by git merge A. Do I understand correctly?

Of course, I can just delete branch B and create a new branch B from A. Is that the best way?

note just to clarify it: I want to keep branch B alive because of deployment configuration. If I delete and recreate I'll need to do another thing manually for deployment.

like image 974
Moshe Shaham Avatar asked Nov 09 '15 13:11

Moshe Shaham


People also ask

How do you make a branch similar to another branch?

To make a merge, you check out some branch ( A in these cases) and then run git merge and give it at least one argument, typically another branch name like B . The merge command starts by turning the name into a commit ID. A branch name turns into the ID of the tip-most commit on the branch.

How do you replicate a branch?

branch : add a --copy ( -c ) option to go with --move ( -m ) Add the ability to --copy a branch and its reflog and configuration, this uses the same underlying machinery as the --move ( -m ) option except the reflog and configuration is copied instead of being moved.

How do you force change branches?

Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.


2 Answers

git keeps track of the history of development. Thus, to make a branch exactly like another branch, you just need to:

git checkout <branch B>
git reset --hard <branch A>
git push --force origin <branch B>

Of course, doing this you will lose the development history that was on branch B.

like image 130
houtanb Avatar answered Oct 29 '22 11:10

houtanb


Sounds like you just want to rename branchB.

git branch -m branchB new_name_for_branchB
git branch branchA branchB

Now new_name_for_branchB refers to the same commit that branchB used to, and branchB now refers to the same commit as branchA. The two branches A and B are still distinct, though.

like image 25
chepner Avatar answered Oct 29 '22 13:10

chepner