How Can I Selectively Merge or Pick Changes from Another Branch in Git?

Better Stack Team
Updated on July 25, 2024

In Git, you can selectively merge or pick changes from another branch using various techniques depending on your specific needs. Here are several methods to achieve this:

Method 1: Cherry-pick

Cherry-picking allows you to select a specific commit from another branch and apply it to your current branch.

  1. Identify the commit to cherry-pick: First, find the commit hash or reference of the commit you want to pick from the other branch.

     
    git log other-branch-name
    
  2. Cherry-pick the commit: Switch to your target branch where you want to apply the changes and execute:

     
    git cherry-pick <commit-hash>
    

    Replace <commit-hash> with the hash of the commit you want to pick from other-branch-name.

  3. Resolve conflicts (if any): If Git encounters conflicts during the cherry-pick, resolve them as usual using git add and git commit.

Method 2: Merge with -no-ff and -no-commit

This method allows you to merge changes from another branch while controlling how Git records the merge.

  1. Merge with -no-ff and -no-commit: Switch to your target branch and merge other-branch-name, preserving the merge commit:

     
    git checkout your-branch-name
    git merge --no-ff --no-commit other-branch-name
    

    This command prepares Git for a merge but does not automatically commit the merge.

  2. Selectively add changes: Use git add to stage changes from other-branch-name selectively:

     
    git add -p
    

    This command will interactively allow you to add changes, one hunk at a time.

  3. Commit the merge: After adding the desired changes, commit the merge:

     
    git commit
    

Method 3: git diff and git apply

If you want to apply changes from another branch without creating a merge commit, you can use git diff and git apply.

  1. Generate a patch file: From other-branch-name, generate a patch file containing the changes:

     
    git diff other-branch-name > my-changes.patch
    
  2. Apply the patch: Switch to your target branch and apply the patch:

     
    git checkout your-branch-name
    git apply my-changes.patch
    
  3. Resolve conflicts (if any): If there are conflicts during the patch application, resolve them manually and use git apply --continue to proceed.

Method 4: git checkout specific files

If you only want to bring specific files or directories from another branch into your current branch, you can use git checkout with the --patch option.

  1. Check out specific files: Check out specific files or directories from other-branch-name into your current branch:

     
    git checkout other-branch-name -- path/to/file1 path/to/dir1
    

    Replace path/to/file1 path/to/dir1 with the paths to the files or directories you want to bring in.

Notes:

  • Commit Hash: Always verify the commit hash or reference of the changes you want to pick or merge.
  • Conflicts: Be prepared to resolve conflicts when merging or applying changes.
  • Branch Switching: Use git checkout to switch between branches as necessary before executing commands.

These methods provide flexibility in selectively merging or picking changes from another branch in Git, depending on whether you need to apply specific commits, merge with control over the commit message, apply patches, or selectively choose files. Choose the method that best fits your workflow and the nature of the changes you want to incorporate into your branch.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github