Reputation: 39691
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.
Upvotes: 231
Views: 355053
Reputation: 91
This happened due to your local git does not have remote commit. so you just need to fetch remote commit using git fetch command and then try to cherry pick commit, it will work.
git fetch
Upvotes: 3
Reputation: 21
To cherry-pick a commit from another repository you have to first add the other repository as a remote (a remote is a repository whose branches you are tracking). You have to give the remote a name.
git remote add <name> https://github.com/example.git
Then, you have to fetch the branches of the remote.
git fetch <name>
Now that you are tracking the other repository and have fetched the changes you can list the commits of a specific branch and cherry-pick a commit.
git log otherRepo/<branch>
git cherry-pick <commit>
In case that you don't want to track the remote anymore you can remove it with:
git remote remove <name>
Upvotes: 1
Reputation: 11
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
Upvotes: 1
Reputation: 49
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>
Upvotes: 2
Reputation: 17981
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:
git checkout xxxxx
git checkout -b temp-branch
Then in your repo, fetch again. The new branch will be fetched, including that commit.
Upvotes: 2
Reputation: 9892
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.
Upvotes: 36
Reputation: 23796
$ git remote add foo git://github.com/foo/bar.git
$ git fetch foo
foo
)$ git log foo/master
$ git cherry-pick 97fedac
Upvotes: 29
Reputation: 2003
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.
Upvotes: 5
Reputation: 31
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.
Upvotes: 3
Reputation: 21114
This can also be easily achieved with SourceTree:
done :)
Upvotes: 1
Reputation: 9672
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.
Upvotes: 15
Reputation: 101
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(-)
Upvotes: 10
Reputation: 9197
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
Upvotes: 311