Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Cherry-Pick to working copy without commit

Tags:

git

I have several branches where I keep certain commits that I want to apply to my working copy every now and then. Initially I tried cherry-picking but I do not want to have the commit in the target branch later.

So I did cherry-pick + reset HEAD~1 --soft

Is there something simpler like cherry-picking to working copy only?

like image 725
schoetbi Avatar asked Sep 01 '15 13:09

schoetbi


People also ask

How do you do cherry pick without commit?

The --no-commit option will execute the cherry pick but instead of making a new commit it will move the contents of the target commit into the working directory of the current branch.

Does git cherry pick change commit?

The git cherry-pick is a very useful command. It takes changes from a specific commit and applies them to your current branch in a new commit. As a consequence, git cherry pick does not alter your current Git history : instead it adds commits to it.

Is a merge but no option was given in cherry pick?

Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline. This option specifies the parent number (starting from 1) of the mainline and allows cherry-pick to replay the change relative to the specified parent.


7 Answers

Use '-n' flag with the cherry-picking which is "no commit"

See here: http://git-scm.com/docs/git-cherry-pick

git cherry-pick -n <HASH>

To then unstage the staged changes

git reset
like image 161
Igal S. Avatar answered Oct 06 '22 00:10

Igal S.


If you are using Terminal

git cherry-pick -n <HASH>

If you are using Intellij Idea

Settings -> Version Control -> git
untick commit automatically on cherry-pick 
like image 23
Ravinda Lakshan Avatar answered Oct 06 '22 02:10

Ravinda Lakshan


You can also use apply instead of cherry-pick if you're just trying to apply all the changes you made in a commit to your working directory:

git show <commit> | git apply

This will apply the changes made in but will not add them to staging or create a commit.

like image 42
adamgy Avatar answered Oct 06 '22 00:10

adamgy


For IntelliJ users

Settings > Version Control > Git: Uncheck Commit automatically on cherry-pick

enter image description here

Update

This feature was removed and set to true by default. There are many complains and maybe they will return it back.

Meanwhile you can achieve the same result with "Cherry-Pick Selected Changes" from commit view (as mentioned in the thread shared by Neo):

enter image description here

like image 35
Trayan Momkov Avatar answered Oct 06 '22 00:10

Trayan Momkov


Simply run:

$ git cherry-pick --no-commit <HASH>
like image 30
moriarty007 Avatar answered Oct 06 '22 01:10

moriarty007


I was getting errors that error: server/metadata/metadata-preui-prod.xml: No such file or directory

So now I do this

git checkout SHA -- server/metadata && git reset -- server/met adata

Old

git show SHA -- file1.txt file2.txt | git apply -

or all files from SHA

git show SHA | git apply -

How to git-cherry-pick only changes to certain files?

like image 36
rofrol Avatar answered Oct 06 '22 02:10

rofrol


To solve this in Visual Studio 2019 without opening a console I went to the "Git Repository" window and right clicked on the Git Commit history and did a cherry pick on the commit I wanted. This commits the cherry pick. But then I did a right click on the commit below and did a "Reset -> Keep changes (--mixed)" which reset the commit but kept the changes uncommitted.

like image 26
Janspeed Avatar answered Oct 06 '22 00:10

Janspeed