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

Time:11-17

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

CodePudding user response:

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.

CodePudding user response:

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
  • Related