Search code examples
gitcommitremote-accessgit-mergegit-branch

How to merge current local branch with specific single commit from different branch in remote repository?


Lets say I have this in my local:

master
branch-a
branch-b

and I have this in my remote repository:

master
branch-a
branch-b
branch-c

Conditions:

  1. I own the remote repository. I have all needed accesses.
  2. My local branch-b and remote branch-b already in sync.
  3. Remote branch-c have a single commit with hash: 1ab2cd3
  4. Commit 1ab2cd3 is NOT the latest commit on remote branch-c.

Question: How can I merge my local branch-b with that 1ab2cd3 commit from remote branch-c?


Solution

  • Since my comments seem to have answered your question, I will add them as an answer:

    Merging is not really the right term to use in this scenario. The term that most closely describes a merge of a single commit is cherry-picking. If you want to have the contents of commit 1ab2cd3 on branch-b, you could use the following command while on branch-b:

    git cherry-pick 1ab2cd3
    

    Cherry-pick applies the changes introduced by the commit onto the current branch. This creates a new commit and is not a merge in terms of Git. Note that this will give the commit a different commit-hash. You cannot cherry-pick without changing the commit-hash (see this answer). However, the -x argument can be used to put a reference to the original hash in the commit message of the commit produced by cherry-pick:

    git cherry-pick -x 1ab2cd3
    

    Addressing your question concerning which branch the commit will be taken from: A commit is identified by its hash which is identical in your local repository and on the remote. When you cherry-pick this commit, it takes the commit no matter on what branch(es) it is part of.