0

I have 4 branches. main, release, master, preview.

Preview is used for doing experiments. I want to clean the preview branch and sync it with release branch.

If I just merge release branch into preview, I still have the old experiment code. I am not sure how to go about it

Johansrk
  • 5,160
  • 3
  • 38
  • 37
  • 1
    You would like to keep the history of the experiment in the branch or you don't care taking it back (removing it from history.... which means rewriting history of the branch)? Also consider that if multiple people are using that branch, the second option will be a pain to carry out if developers are not careful. – eftshift0 Nov 16 '22 at 16:46
  • Do you care about the history of the branch? Is it publically shared? Can you delete + recreate it (with the correct/new start point)? Might also be of relevance: https://stackoverflow.com/questions/4624357/how-do-i-overwrite-rather-than-merge-a-branch-on-another-branch-in-git; it keeps the "experimental" (preview) history – knittl Nov 16 '22 at 16:49
  • no. The history of the preview branch can be wiped – Johansrk Nov 16 '22 at 16:53

2 Answers2

2

If you would like to keep the experiment in the branch, you could do this:

git checkout preview
git merge --no-commit release
# let's get the contents of files just like in release:
git restore --staged --worktree --source=release -- .
git merge --continue

Now you have a merge of release into preview and the contents of the merge (files) is just like release.

Another way, without the merge, but having the result of having contents of files just like in release would be:

git checkout preview
git restore --staged --worktree --source=release -- .
git commit -m "Making this branch look like release, but without merging nor modifying history of this branch".

If you want the branch to be just like release also in terms of history:

git checkout preview
git reset --hard release
# and now feel free to force-push into whatever remove branch you need to.
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • could not make any of them work. When I did I ended up with pending 820 pulls and 92 push – Johansrk Nov 16 '22 at 20:58
  • @Johansrk based on your comments to the question the last option of this answer (reset hard) is probably what you want, and then you would have to force push the `preview` branch to blow away the old `preview` branch on the server and replace it with your new re-written version (which is identical to `release`). After the reset, the 820 missing commits on the remote branch and 92 new commits on the new branch is because you haven't force pushed yet. – TTT Nov 16 '22 at 22:44
1

Since you noted in the comments that you are not interested in keeping the history, you can simply drop the branch and recreate it from whatever commit you want:

git branch -D preview
git branch preview release

or as a single command:

git branch -f preview release

Both commands from above will only work if the branch is currently not checked out. If it is checked out, checkout can be used:

git checkout -B preview release
knittl
  • 246,190
  • 53
  • 318
  • 364