1

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?

Faris Han
  • 525
  • 3
  • 16
  • 2
    If `1ab2cd3` is not the latest commit on *branch-c*, how can it be the single commit (only commit) on this branch? – Matt Jun 23 '21 at 08:05
  • @Matt Commits on remote `branch-c`: `HEAD`, `cmmt1`, `cmmt2`, `1ab2cd3` <<< I only want to merge `1ab2cd3`, not `HEAD`, `cmmt1`, or `cmmt2`. Does it make sense? Or should I change my 'single commit' term? – Faris Han Jun 23 '21 at 08:14
  • 1
    I'm not sure if I understand this properly... 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 `git cherry-pick 1ab2cd3` while on *branch-b*. Note that this will give the commit a different commit-hash. – Matt Jun 23 '21 at 08:21
  • Is `git cherry-pick 1ab2cd3` mean I merge it from the remote _branch-c_? or the local _branch-c_? Is there a way to cherry-pick but not change the hash? @Matt – Faris Han Jun 23 '21 at 08:26
  • 1
    *Merging* is not really the right term to use in this scenario. *Cherry-pick* applies the changes introduced by the commit onto the current branch. This creates a new commit (new hash) and is not a *merge*. 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. You cannot cherry-pick without changing the commit-hash (see [here](https://stackoverflow.com/a/14676852)). The `-x` argument would put a reference to the original hash in the commit message. – Matt Jun 23 '21 at 08:42
  • 1
    I wrote an answer to your question as it seems my comments have solved it. – Matt Jun 23 '21 at 09:42

2 Answers2

1

git cherry-pick 1ab2cd3


Thanks to @Matt for the explanation in question's comment section:

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.

Cherry-pick applies the changes introduced by the commit onto the current branch. This creates a new commit (new hash) and is not a merge. 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.

You cannot cherry-pick without changing the commit-hash (see here). The -x argument would put a reference to the original hash in the commit message.

Faris Han
  • 525
  • 3
  • 16
  • 1
    I actually put my comments into an answer since they seem to have solved your problem. – Matt Jun 23 '21 at 09:44
1

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.

Matt
  • 12,848
  • 2
  • 31
  • 53