1

I have a few branches that mimic environments.

  • development
  • preview
  • main

My 'preview' branch is where we stage our changes for review. Anyways, it is way out of sync with development so I want to literally replace all the code in 'preview' with 'development'.

Meaning, 'preview' still exists, but it now has the latest code from development. It is NOT merged, its literally a copy from development.

I've seen various solutions. I don't "need" to keep the history and there are many developers workings off the branches, so I need something that they can just pull and get the updates, if possible.

Replace "preview" branch code with "development" code, while still retaining autonomy. Like throw away "preview" branch code and update it with "development" code. no merge, completely replace. All branches keep their independence. It is ok for preview to lose its history since I am starting over for it.

EDIT: preview has become a dumping ground, so I don't want to merge and have all that crust in there. I want to start fresh with the latest greatest code from development.

I'm thinking of something like:

git checkout preview 
git reset --hard origin/development
git push -f

But not sure how this will play with multiple developers.

Also considering,

git checkout development
git merge -s ours preview
git checkout preview
git merge development

But I am worried its diverged too much.

james emanon
  • 11,185
  • 11
  • 56
  • 97

2 Answers2

2

Delete the branch and recreate it:

git branch -D preview
git branch preview development

Or, force (re-)creation of the branch at a specified ref:

git branch -f preview development

The end-result is identical.

Don't forget to (force) push and inform others of the effectively rewritten history. Any commits based on the old preview branch now need to be rebased, otherwise you will bring in the old history of "preview" again into your new history.

knittl
  • 246,190
  • 53
  • 318
  • 364
  • Is there a way to do this w/o having the developers delete their local copy and pull again? Basically, is there a solution in which they just pull? – james emanon Aug 17 '21 at 05:14
  • 1
    @jamesemanon each Git repository is independent. If they already have their local version of `preview`, they have to delete it manually. `git fetch` (and subsequently `git pull`) will only update remote-tracking branches (e.g. `origin/preview`). – knittl Aug 17 '21 at 05:22
  • ok. I was hoping to avoid something like a global 'hey all, you need to delete your local copy and git fetch/pull the preview branch". Right now when these branches are updated, we just 'git pull" and get the latest. hmmm. Will this be a viable option in which devs can just "git pull"? https://stackoverflow.com/a/42454737/1054992 – james emanon Aug 17 '21 at 05:31
  • @jamesemanon: I argue that nobody should use `git pull` (at least, not until they've learned by heart the various commands that `git pull` runs, and understand how, when, and why it's OK to use the shortcut `git pull` instead of the two separate commands it will run)—but in the end the answer is *mostly* "no, they cannot use `git pull` here". There are some cases, using the `--fork-point` mode of `git rebase`, where they *can* just use `git pull`. The fork point stuff doesn't work in every case though, so they *really need to understand Git deeply* before depending on this. – torek Aug 17 '21 at 08:32
  • 1
    @jamesemanon no, `git pull` (nor `git fetch` + `git merge`) can be used here by other devs. If you really need this, see [my answer for how to 'overwrite' a branch](https://stackoverflow.com/a/4624383/112968). It keeps the (old review) history and creates a new merge commit which throws away all changes from the review branch. History shaped like this allows your peers to (fast-forward) merge the "new" review branch. – knittl Aug 17 '21 at 18:02
1

git checkout development && git branch -D preview && git checkout development && git checkout -b preview

I just checked this and it worked for me -- version 2.30.1

hd1
  • 33,938
  • 5
  • 80
  • 91
  • will this have issues with other developers pulling from preview even if they have their "old" version? Also, are you just "git push -u" or doing any force push? – james emanon Aug 17 '21 at 05:07
  • They'll have to delete their copy of preview and pull again. – hd1 Aug 17 '21 at 05:09
  • cool, how effective is this way? https://stackoverflow.com/questions/67976340/replace-branch-completely-with-another-branch – james emanon Aug 17 '21 at 05:11