Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merge/cherry-pick avoiding staging

I am trying to make a cherry-pick with some changes, but I do not want to commit them in this branch, I just want to have them locally.

For this purpose, I am using

git cherry-pick <hash> --no-commit

However, this adds them automatically to the stage, and then, I have to manually reset them using

git reset HEAD <files>

Is there no option with git to make cherry pick not commit, and not add to index?

something like:

git cherry-pick <hash> --no-commit --no-stage

I know this trick would do what I want, but I would have to specify all the files in the commit:

Edit

Tried also this, but it is also added to the index.

git checkout hash -- <list of files>

And this files, might not share the same common path. I am trying to automate it

like image 804
Mayday Avatar asked Apr 24 '18 07:04

Mayday


People also ask

How do you skip cherry pick?

cherry-pick/revert: add --skip option During a cherry-pick or revert a user could likewise skip a commit, but needs to use ' git reset ' (or in the case of conflicts ' git reset --merge '), followed by ' gi t ( cherry-pick | revert ) --continue ' to skip the commit.

Should you cherry pick a merge commit?

Whenever you can use a traditional Merge or Rebase to integrate, you should do so. Cherry-pick should be reserved for cases where this is not possible, e.g. when a Hotfix has to be created or when you want to save just one/few commits from an otherwise abandoned branch.

Can you cherry pick a merge?

With the cherry-pick command, Git lets you incorporate selected individual commits from any branch into your current Git HEAD branch. When performing a git merge or git rebase , all the commits from a branch are combined. The cherry-pick command allows you to select individual commits for integration.

Is cherry picking good practice?

Cherry picking has a negative connotation as the practice neglects, overlooks or directly suppresses evidence that could lead to a complete picture. Cherry picking can be found in many logical fallacies.


3 Answers

An alternative would be to use apply instead of cherry-pick:

git show <commit> | git apply

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

like image 191
adamgy Avatar answered Oct 13 '22 09:10

adamgy


Cherry-pick is, behind the scenes, a merge operation. Git performs merges in the index—so no, there is no way to do this other than using the index / staging-area. (But see below.)

If you don't want to commit the result in the current branch, just use some other branch (git checkout -b <newbranch>) or a detached HEAD (no branch at all: git checkout --detach HEAD). You can also use a mixed reset, as lucas-reineke suggested in a comment, to make the commit in the current branch and then move the current branch back one commit. This has the side effect of resetting the index to match the adjusted (post-cherry-pick) HEAD, leaving you with the state you originally asked for.

You can use git cherry-pick -n HEAD followed by git reset --mixed HEAD (with no path names listed) to get the same result: the cherry-pick takes place in the index, updating the work-tree as a side effect, without committing, and then the reset copies the files from the HEAD commit back into the index, leaving the work-tree undisturbed. Note that --mixed is the default, so git reset without --soft or --hard does --mixed even if you don't include an explicit --mixed (it's useful to make sure you did not make any typos in the command, though).

These are the two ways you would normally do this. There is one more possibility, though: while Git has the index—the one distinguished index that goes with the primary work-tree for the repository—you can redirect Git to your own alternative index, using the GIT_INDEX_FILE variable. You could set this to the name of a temporary file that does not yet exist, run git reset to create that temporary index file and fill it in, then run git cherry-pick -n <commit> to update the temporary index. You can then unset GIT_INDEX_FILE and remove the temporary file. The result is a cherry-pick that uses your temporary index, instead of the normal index, that alters the work-tree files as a side effect. Note that this is the same amount of work as doing the cherry-pick in the normal index and then resetting the normal index. It may be useful for some sort of scripted work-flow, however. Beware of cherry-picks that fail and therefore leave merge conflicts in the index!

like image 25
torek Avatar answered Oct 13 '22 08:10

torek


I was able to achieve this by

  1. Cherry-pick the commit into your branch
  2. git reset --soft
like image 25
AlignedDev Avatar answered Oct 13 '22 09:10

AlignedDev