6

I have 3 branches: development, qa, and staging. I have almost the same code in both development and qa, so making a pr from development to qa doesn't yield too many changes (and or conflicts).

But now I have a move all the stuff from qa to staging. And qa code is practically completely different and new from staging, so what's the best option? I searched and found this other answer:

git checkout qa
git merge -s ours staging
git checkout staging
git merge qa

Is this a good idea for my case? Thanks in advance!

EDIT: I don't want to lose all the commits from staging branch.

Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
nick
  • 2,819
  • 5
  • 33
  • 69
  • As qa and staging contains different code changes, you might get conflicts. So you need to resolve and merge them – uday Jun 14 '21 at 19:48
  • yeah, what I want to do is replace all the code in staging with the code in qa, and don't want to solve conflicts, just replace the code as if you manually go file by file changing the code from the files in qa branch – nick Jun 14 '21 at 19:52
  • you can use git merge with force overwrite, you can see this link for similar steps https://stackoverflow.com/questions/40517129/git-merge-with-force-overwrite – uday Jun 14 '21 at 19:54
  • Your requirements are not clear. Please provide a concrete example of commits, current state and what you want in the end. You can also just try the commands and see what happens. Nothing is lost with Git. – Gaël J Jun 14 '21 at 19:54
  • It's should all be a big cycle: `development` branches off `master`; `development` is merged into `qa`; `qa` is merged into `staging`, and `staging` is merged into `master`. The only difference between the merges is the level of testing needed to achieve the desired confidence that you are ready for the merge. – chepner Jun 14 '21 at 20:04
  • 2
    Yes, using `merge -s ours` is your best option here. You will end up with the snapshot of one branch only but keep the history of both. Also see https://stackoverflow.com/q/4624357/112968 for a possible duplicate – knittl Jun 14 '21 at 20:36
  • Does this answer your question? [How do I 'overwrite', rather than 'merge', a branch on another branch in Git?](https://stackoverflow.com/questions/4624357/how-do-i-overwrite-rather-than-merge-a-branch-on-another-branch-in-git) – Marco Luzzara Jun 14 '21 at 22:05

2 Answers2

16

If you want to "completely replace", I'll go with git reset:

git checkout staging
git reset origin/qa --hard

This will make staging have the exact same content as qa but this will "erase" commits that may have been done on staging (the commits won't be deleted yet but they won't be visible in the history of staging anymore).

Gaël J
  • 11,274
  • 4
  • 17
  • 32
  • 1
    Is there any way to do this exact thing but without losing the staging commits? – nick Jun 14 '21 at 19:58
  • 1
    _without losing the commits_ you mean see them in history but still overwrite all files with content from `qa`? – Gaël J Jun 14 '21 at 20:14
1

Gaël J's answer is good, but there is a shorter (i.e. single) command, which doesn't require you to switch branches and doesn't interfere with your current working tree:

git branch -f staging origin/qa

If the branch is already shared with others (= pushed), you have to force-push to the remote repository and inform others that they have to rebase any existing work based on this branch.

If you want to put all staging commits on top of the remote-tracking branch origin/qa, use rebase:

git rebase origin/qa staging

Or, if you want to replace the content of tree referenced by staging, but still keep its old history, use the merge strategy -s ours. See my answer on "How do I 'overwrite', rather than 'merge', a branch on another branch in Git?"

knittl
  • 246,190
  • 53
  • 318
  • 364