person github
If you have two branches (for example, one is called old-branch
and the other is called new-branch
) and you want to compare the two branches to remove changes that have been made new-branch
in them , you can use the following methods:old-branch
Method 1: Use git diff
andgit apply
-
Generate the difference between two branches:
git diff old-branch..new-branch > changes.patch
-
Switch to
new-branch
:git checkout new-branch
-
Apply the difference in reverse:
git apply -R changes.patch
This will undo new-branch
all old-branch
changes that have been made.
Method 2: Usegit rebase -i
-
Switch to
new-branch
:git checkout new-branch
-
Interactively rebase to
old-branch
:git rebase -i old-branch
This will open a text editor listing all commits new-branch
relative to . old-branch
You can delete commit lines that you don't want to keep.
Method 3: Usegit cherry-pick
If you know exactly which commits are new-branch
unique and you want to keep them, you can create a completely new branch and select the commits you want one by one.
-
old-branch
Create a new branch from :git checkout old-branch git checkout -b new-clean-branch
-
Cherry-pick the commits you want:
git cherry-pick <commit_hash>
This way, new-clean-branch
only the commits you explicitly selected will be included, without old-branch
any changes in them.
Choose the method that works for you, and proceed with caution. Hope this helps you! Have other questions?