Can I 'git cherry-pick <sha>' and have 'git log foo..bar' recognize it?

233 views Asked by At

Background: I have two branches, master and stable, with many commits on master that aren't on stable. I want to cherry-pick a number of those commits to stable, and then be able to use git log --merges-only stable..master to see what's left.

However, if I just cherry-pick, git treats them as two commits and thus the 'git log' command doesn't help. E.g. given this:

# create a repo
mkdir cherry
cd cherry
git init

# add a commit on master
touch foo.txt
git add foo.txt
git commit -a -m 'commit 1'

# create a stable branch
git checkout -b stable

# add two more commits to master
git checkout master
touch bar.txt
git add bar.txt
git commit -m 'commit 2'
touch baz.txt
git add baz.txt
git commit -m 'commit 3'

# cherry-pick just one of those commits to stable
git checkout stable
git cherry-pick master

Then, I'd like to be able to see what commits are candidates for future cherry-picking on master, ideally using git log, but it doesn't really answer my question:

> git log stable..master --pretty=oneline --no-merges
01550adab8993ceb1eec7bbc7a0e3de3550d63fc commit 3
8a3ea27aa50c887b603296bb9d4a36ccbfa35311 commit 2

However, TIL about git cherry:

>  git cherry stable master
+ 8a3ea27aa50c887b603296bb9d4a36ccbfa35311
- 01550adab8993ceb1eec7bbc7a0e3de3550d63fc

Where the entries prefixed with '+' are the candidates for future-cherry picking.

3

There are 3 answers

0
Magnus Bäck On BEST ANSWER

The git cherry command is designed for this purpose.

0
John Szakmeister On

I'm not sure if it will help you or not, but you may want to look at my git-missing script. It mimics Bazaar's bzr missing command. It will show you the extra commits you have on your branch, followed by the commits that are missing from the other branch. It takes cherry picks into account.

For example:

$ git missing master
You have 1 extra revisions
--------------------------
0db6b7c Call SetupSource for .zsh files.

You have 1 missing revisions
----------------------------
70f9f42 Add clojure snippets.
2
user2926055 On

Try rebasing stable against master after you've cherry-picked all the commits you want:

git checkout stable
git rebase master