How to compare two old and new branches and remove the modifications in the new branch from the modifications in the other old branch?

person github

If you have two branches (for example, one is called old-branchand the other is called new-branch) and you want to compare the two branches to remove changes that have been made new-branchin them , you can use the following methods:old-branch

Method 1: Use git diffandgit apply

  1. Generate the difference between two branches:

    git diff old-branch..new-branch > changes.patch
    
  2. Switch to new-branch:

    git checkout new-branch
    
  3. Apply the difference in reverse:

    git apply -R changes.patch
    

This will undo new-branchall old-branchchanges that have been made.

Method 2: Usegit rebase -i

  1. Switch to new-branch:

    git checkout new-branch
    
  2. Interactively rebase to old-branch:

    git rebase -i old-branch
    

This will open a text editor listing all commits new-branchrelative to . old-branchYou can delete commit lines that you don't want to keep.

Method 3: Usegit cherry-pick

If you know exactly which commits are new-branchunique and you want to keep them, you can create a completely new branch and select the commits you want one by one.

  1. old-branchCreate a new branch from :

    git checkout old-branch
    git checkout -b new-clean-branch
    
  2. Cherry-pick the commits you want:

    git cherry-pick <commit_hash>
    

This way, new-clean-branchonly the commits you explicitly selected will be included, without old-branchany changes in them.

Choose the method that works for you, and proceed with caution. Hope this helps you! Have other questions?

Guess you like

Origin blog.csdn.net/m0_57236802/article/details/132898733