199

So I have a commit that has a helpful code change but it's on another branch.

I would like to apply this commit in the other branch to my working copy on my current branch (not as another commit).

Is this possible? How would I do this?

Thought I'd share this is related to my previous question but specific to working copy: git cherry picking one commit to another branch

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
Oliver Williams
  • 5,966
  • 7
  • 36
  • 78
  • Possible duplicate of [Git: Cherry-Pick to working copy without commit](https://stackoverflow.com/questions/32333383/git-cherry-pick-to-working-copy-without-commit) – Mad Physicist Dec 18 '18 at 23:49

2 Answers2

323

How to add the desired commit(s) into different branches.

git cherry-pick <SHA-1>...<SHA-1> --no-commit

Apply the change introduced by the commit(s) at the tip of the master branch and create a new commit(s) with this change.

The syntax of the ... is a commit range. grab all commits from start (exclude) to the last one. If you want a single commit to use a single SHA-1

enter image description here


cherry-pick without commiting

By default git cherry-pick commit your changes, so if you wish to cherry-pick without committing all the changes simply add the -n flag

This will allow you to review the changes and commit them manually if you wish or abort it if you run into too many conflicts.

git cherry-pick -n <hash>

cherry-pick a merge commit

In case you needed to cherry-pick a merge instead of a commit, use the -m flag

#
# In this case, we select the [1] first parent in the commit
# Use git show <hash> to see a list of available parents
# 
git cherry-pick -m 1 <hash> 

Read out the full git cherry-pick documentation for all the options you can use

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 9
    Thanks! For both the clear & well-formatted text, and the charming diagram. If you write a book, I'll buy it ;-) – Spike0xff Mar 07 '17 at 15:58
  • 2
    The question explicitly mentions "not as another commit" while your answer explicitly mentions "create a new commit with this change". What am I missing? – Armen Michaeli Sep 25 '19 at 08:55
  • 6
    you are missing the no commit flag – CodeWizard Sep 25 '19 at 12:11
  • That even works for remote commits as long as your local repository knows about them (perform a `git fetch remote` to refresh) – sberder Jan 15 '20 at 07:52
  • Thanks for your answer, but it is somewhat misleading that you quote "[...] creates a new commit [...]", just below a command containing `--no-commit`, which prevents the default commit from happening. – m7913d Mar 06 '21 at 22:55
  • 1
    This is better than the official documentation. Thanks! – TBirkulosis Jun 23 '21 at 14:20
  • Note that `one...three` will grab commits `two` and `three`, since the first one is excluded. So we must look at its parent commit to do something like `zero...three`. – JCarlosR Feb 23 '23 at 17:35
47

You can still use the git cherry-pick command. See git cherry-pick --help:

   -n, --no-commit
       Usually the command automatically creates a sequence of
       commits. This flag applies the changes necessary to
       cherry-pick each named commit to your working tree and the
       index, without making any commit. In addition, when this
       option is used, your index does not have to match the HEAD
       commit. The cherry-pick is done against the beginning state
       of your index.

So you can just git cherry-pick -n <commitid>, and the changes will be applied to your working directory and staged in the index (like git -a), but will not be committed.

larsks
  • 277,717
  • 41
  • 399
  • 399