216

I'm having trouble performing a cherry-pick. On my local machine, I'm currently on my "master" branch. I want to cherry-pick in a commit from another branch, named "zebra". The "zebra" branch is a remote branch.

So git status:

# On branch master
nothing to commit (working directory clean)

Ok, now I try to cherry-pick the commit I want:

git cherry-pick xyz
fatal: bad object xyz

where "xyz" is the signature of the commit I'm interested in, that happened on branch "zebra".

So the first obvious question is, why can't git find the commit I'm referencing? I don't really understand how this is working in the first place to be honest. Does git store something like a database of commits locally in my working directory, for all other branches? When executing the cherry-pick command, does it go and search that local database to find the commit I'm talking about?

Since "zebra" is a remote branch, I was thinking I don't have its data locally. So I switched branches:

git checkout zebra
Switched to branch 'zebra'

So now here on my local machine, I can see that the files in the directory reflect zebra's state correctly. I switch back to master, try to cherry-pick again (hoping the commit data is available now), but I get the same problem.

I've got a fundamental misunderstanding of what's going on here, any help would be great.

user291701
  • 38,411
  • 72
  • 187
  • 285
  • 4
    conceptually everything seems right. are you sure you are using correct hash (signature as you call it) of the commit? try 'git show ' to verify. – 0xc0de Dec 09 '12 at 15:52
  • 1
    Hi, yes positive - both my branches are up on github, and I can use them to find the commit pages that way. If I understand, the state of my machine locally is such that git cannot find the hash from 'zebra' while in the context of 'master'. Do I need to somehow tell it that 'zebra' exists locally, too? – user291701 Dec 09 '12 at 15:58
  • 1
    oh and doing 'git show xyz' gives the same error "fatal: bad object". (and i'm replacing xyz with the correct hash). – user291701 Dec 09 '12 at 15:59
  • And to clarify, I can use my 'xyz' hash to look at the commit on github without issue, like: "https://github.com/me/test/commit/xyz". – user291701 Dec 09 '12 at 16:01
  • `$git fetch` and try again – Mauricio Gracia Gutierrez Sep 14 '22 at 16:54
  • In this case `git fetch -all` and `git pull` will help. Sometimes our local branch was not up to date with remote and one should check it using `git status`. Doing `git pull` can fix it and then apply cherry pick. – akshay_sushir Apr 11 '23 at 11:28

12 Answers12

290

Since "zebra" is a remote branch, I was thinking I don't have its data locally.

You are correct that you don't have the right data, but tried to resolve it in the wrong way. To collect data locally from a remote source, you need to use git fetch. When you did git checkout zebra you switched to whatever the state of that branch was the last time you fetched. So fetch from the remote first:

# fetch just the one remote
git fetch <remote>
# or fetch from all remotes
git fetch --all
# make sure you're back on the branch you want to cherry-pick to
git cherry-pick xyz
Peter Lundgren
  • 8,787
  • 2
  • 26
  • 21
  • 1
    Archived link: http://archive.miek.nl/blog/archives/2011/12/13/cherry-picking_remote_branches/index.html – brianpeiris Oct 01 '14 at 15:09
  • 1
    Tried this approach to cherry-pick an upstream fix on Github and it turned out that the checked-out hash was different from the one on Github. Therefore I had to check out, get the hash and cherry-pick that. – DustWolf Apr 25 '18 at 12:24
  • 5
    Really missing here an option to pass remote in cherry-pick like: git cherry-pick – Jared Oct 29 '18 at 11:50
  • worked well after fetching the remote branch. Thanks :) – Adeel Apr 23 '19 at 09:54
  • yes worked after fetching for me too. More about git cherry-pick is available in https://www.atlassian.com/git/tutorials/cherry-pick – Satheesh M Nov 14 '19 at 11:41
  • good explains and thanks for the answer here. working for me. – AMIC MING May 05 '20 at 20:23
33

Just as an addendum to OP accepted answer:

If you having issues with

fatal: bad object xxxxx

that's because you don't have access to that commit. Which means you don't have that repo stored locally. Then:

git remote add LABEL_FOR_THE_REPO REPO_YOU_WANT_THE_COMMIT_FROM
git fetch LABEL_FOR_THE_REPO
git cherry-pick xxxxxxx

Where xxxxxxx is the commit hash you want.

Cezar Augusto
  • 8,794
  • 5
  • 31
  • 36
28

Adding remote repo (as "foo") from which we want to cherry-pick

$ git remote add foo git://github.com/foo/bar.git

Fetch their branches

$ git fetch foo

List their commits (this should list all commits in the fetched foo)

$ git log foo/master

Cherry-pick the commit you need

$ git cherry-pick 97fedac
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
13

After merging a development branch to master, I usually delete the development branch. However, if I want to cherry pick the commits in the development branch, I have to use the merge commit hash to avoid "bad object" error.

Juuso Ohtonen
  • 8,826
  • 9
  • 65
  • 98
9

Need to pull both branch data on your local drive first.

What is happening is your trying to cherry-pick from branch-a to branch-b, where in you are currently on branch-b, but the local copy of branch-a is not updated yet (you need to perform a git pull on both branches first).

steps:
- git checkout branch-a
- git pull origin branch-a
- git checkout branch-b
- git pull origin branch-b
- git cherry-pick <hash>

output:
[branch-b <hash>] log data
Author: Author <Author
1 file changed, 1 insertion(+), 3 deletions(-)

temyong
  • 91
  • 1
  • 1
9

Just 2 small steps to do:

  1. git fetch
  2. git cherry-pick ur-commit-hash
Ikbel
  • 1,817
  • 1
  • 17
  • 30
4

The commit should be present in your local, check by using git log.

If the commit is not present then try git fetch to update the local with the latest remote.

Kuldeep Saxena
  • 1,803
  • 1
  • 14
  • 17
3

I had this error returned after using the commit id from a pull request commit id tab. That commit was subsequently squashed and merged. In the github pull request, look for this text: "merged commit xxxxxxx into..." instead of attempting to use the commit ids from the commits tab.

Mary
  • 31
  • 2
2

If you have fetched, yet this still happens, the following might be a reason.

It can happen that the commit you are trying to pick, is no longer belonging to any branch. This may happen when you rebase.

In such case, at the remote repo:

  1. git checkout xxxxx
  2. git checkout -b temp-branch

Then in your repo, fetch again. The new branch will be fetched, including that commit.

lulalala
  • 17,572
  • 15
  • 110
  • 169
2

I solved this issue by going on the branch with the commit I want to cherry pick.

git checkout <branch With Commit To Cherry-Pick>

use log to find commit hash

git log

when you've found your hash cut and paste on note pad. if using command just scroll up to get the hash then checkout the branch you want to place the commit in.

git checkout < branch I Want To Place My Cherry-Picked-Hash In>

finally call cherry-pick from git (note) -x is to append your cherry-pick message to the original. "When recording the commit, append a line that says "(cherry picked from commit …​)" to the original commit message in order to indicate which commit this change was cherry-picked from. "

git cherry-pick -x <your hash commit to add to the current branch>
RVscript
  • 49
  • 3
1

This can also be easily achieved with SourceTree:

  • checkout your master branch
  • open the "Log / History" tab
  • locate the xyz commit and right click on it
  • click on "Merge..."

done :)

Giona
  • 20,734
  • 4
  • 56
  • 68
1

Steps are :

  • git fetch

  • git checkout [remote-branch] , example: git checkout origin/payroll

  • git cherry-pick db8d7526f2218829a96de72c0cf27e3636893f36

  • git branch [new-branch] , new-branch must be same with remote-branch , example: git branch payroll

  • git checkout [new-branch]

  • git push origin

  • Please provide more information. What is xxxxxx in `git cherry-pick` comment? Also, 3rd step - there is no remote branch at that step, it will be created when local branch will be pushed. – kosist Jun 08 '22 at 06:42
  • remote-branch can be created at web repository – Vicky Ibrahim Jun 09 '22 at 08:00
  • But you write `git branch` - this particular command will create local branch. If one already has remote branch, then he needs to `git fetch`, and then `git checkout branch-name`. – kosist Jun 09 '22 at 08:12
  • may be it can. Because i had tried "git fetch", then "git checkout branch-name". it's not success – Vicky Ibrahim Jun 10 '22 at 12:59
  • then `git-fetch` has to be mentioned in the answer. Because now it is not clear, why branch name should be the same as remote (and there is no step to create remote branch). – kosist Jun 11 '22 at 14:04
  • OK, git-fetch has been added – Vicky Ibrahim Jun 13 '22 at 06:14