SIMULATAN
SIMULATAN

Reputation: 959

Git copy contents of one branch into another

So I know there are multiple answers to this problem but they all don't work. I simply want the git commands to copy the files from one branch into another. Unfortunately, when I want to merge the two branches, it either gives me the error that there isn't a relation (which makes sense but still I don't care I just want to have the current state of the files of the one branch to the other) or it says nothing to commit tree clean.

It can't be that hard to just copy the current state of the files of branch a to branch b right?

EDIT: Yes, the history doesn't matter, I just want the current code state to be on the new branch, maybe with a new commit

Upvotes: 1

Views: 1757

Answers (1)

mousetail
mousetail

Reputation: 8010

Option One

So what you want to do is delete B and create a new branch with the same name:

git branch -d B

Note: All data on B will be permanently destroyed. It might be better to rename B like this:

git branch -m B B_old

Now, you can create a new branch B branching of of A:

git checkout A
git switch -c B

Now the branch B will be exactly the same as A (including history)

Option Two

You can also use the theirs option to resolve all merge conflicts from the remote branch:

git switch B
git merge A -s theirs

This will use the changes from A whenever there is a conflict, but keep changes from B if there is not. This only works if branches A and B have a common ancestor somewhere.

Upvotes: 1

Related Questions