Git cherry-pick - How to display changes without actually doing anything
Asked Answered
T

3

8

Is there any way to display changes that would be applied by the 'git cherry-pick ' before actually doing anything?

I would like to see a 'git diff' type of list of changes that would be done by the command without actually doing the changes or modifying anything.

Thermic answered 9/11, 2021 at 8:58 Comment(3)
The obvious suggestion is "git diff", which you've already mentioned, but I'm guessing you're passing more than just a single commit to the cherry-pick command? Perhaps you could give an example.Treatment
Remember also that git show, used on an ordinary (single-parent) commit, shows the same diff that git cherry-pick will attempt to apply to your current commit. So if you are thinking "I might like commit a123456, let me see what it does" you would run git show a123456 to see what it does. Then you'd decide whether to attempt the cherry-pick.Ocrea
Yes I'm aware of git show (that's what I have been using currently) but I would also like to see directly what are the lines the cherry-pick will replace/change.Thermic
B
9

git cherry-pick doesn't have a --dry-run option like some other commands.

However, although not exactly what you asked, you could just test it out by leveraging the -n (--no-commit) option.

The rollback would be trivial :

(assuming you start on a clean working tree)

# cherry-pick without actually committing
git cherry-pick -n <commit>

# inspect changes
git diff --staged

# wipe all these temporary changes to go back to where you started
git reset --hard
Brookebrooker answered 9/11, 2021 at 9:16 Comment(3)
This is the answer I was afraid to get but thanks anyway. I wanted to avoid reset --hard if possibleThermic
After git reset --hard, git status returned Cherry-pick currently in progress. (run "git cherry-pick --continue" to continue) ....Running that --continue command brought back everything as it was before the git cherry-pick -n <commit>.Mitigate
Turns out that (at least with git version 2.39.1) the git reset --hard execution is not required at all; git cherry-pick --abort will bring back the state as before the git cherry-pick -n <commit> execution.Mitigate
N
3

I've done such "experiments" on a (temporary) branch. Original master/main is left unchanged.

git checkout -b cherry-test
git cherry-pick <sha1>
git show/log/whatever
git checkout master
git branch -D cherry-test
Neumeyer answered 14/2, 2023 at 9:2 Comment(0)
P
0

I Believe you can do a git diff between the last commit on your branch and the commit you want to cherry-pick like so :

git diff [last-commit-hash] [cherry-pick-commit-hash]

Penoyer answered 9/11, 2021 at 9:1 Comment(1)
Is this actually the same as doing cherry-pick? this diffs everything in the repo, cherry-pick just merges the changes in single commit not everything in the repo. I'm not going to do a full merge, I'm doing cherry-pick for a reason.Thermic

© 2022 - 2024 — McMap. All rights reserved.