How do I clean my GIT branch and sync it up with another branch


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


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.

Answered By – eftshift0

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published