LucasW
LucasW

Reputation: 65

GITLAB: How to push ENTIRE branch to another one

My goal is pretty simple to explain, but yet I could not find any way to do this. Let's say I have 3 branches. A, B and C

A -------.
          \--------.----------- B
                     \
                      .-------------- C

I simply want to push the entire branch "C" to the branch "B".Let me explain: I don't want to keep any stuff that I did in B after I created C from it. I simply want B to have the exact same code which have the branch C.

How can I safely do that ?

There's more than 10 000 files of difference between the two of them so manually is not an option.

I tried several stuff including a git merge with "theirs" strategy but I still had a lot of conflict somehow.

Upvotes: 2

Views: 1887

Answers (4)

Romain Valeri
Romain Valeri

Reputation: 22065

Regardless of the number of potentially conflicting files or the differences in code, either of these three commands

# if you don't need to checkout the branch immediately
git branch -f B C

# if you DO want to checkout the branch immediately
git checkout -B B C

# if you branch B is ALREADY checked out
git reset --hard C

would make B point at the same commit than C, making them exactly identical, code and history. No conflicts resolution, no merges, it's instantaneous.

Note that this is considered a rewrite of B's recent history, so this is not to use if you're sharing branch B with anyone.

It'll also dereference any commit made on B after C was branched off it, so if you have any doubt, beforehand make a backup of B with

git branch backup-B B

Lastly... you've forgotten to make a backup and have a regret? git reflog will list previous positions of HEAD, you'll find B there, recover it with git reset --hard <old position hash> or put a handle on it for inspection/reuse with git branch recovered-B <old position hash>

Upvotes: 5

torek
torek

Reputation: 490118

There is no theirs strategy. There's a -X theirs option, but that is not a strategy. Git calls it a "strategy option", which is a bad name: it sounds like an option that provides a strategy, but in fact, it's an option to some other strategy, the one from the -s option, which defaults to -s recursive here. I call -X an "eXtended option" to help avoid confusing it with the -s option.

For a description of why there is no -s theirs, even though there was at one time, see this answer by VonC.

The various reset methods (git checkout -B, git reset --hard, and the like) in the other answers here are usually the way to go. However, if you want to produce the effect of a -s theirs strategy easily, see jthill's answer to Is there a "theirs" version of "git merge -s ours"?

Upvotes: 3

EncryptedWatermelon
EncryptedWatermelon

Reputation: 5618

Checkout B then reset HEAD

git checkout B
git reset --hard C

Upvotes: 4

Calum Halpin
Calum Halpin

Reputation: 2105

If you just want to discard all changes unique to B then delete it:

git branch -d B

Checkout C and create a new B from it:

git checkout C
git branch B

B is now exactly the same as C.

Upvotes: 2

Related Questions