Data report"State of code review 2024" is now liveRead the full report

Resolving the Git Error: git cherry-pick bad object

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


The git cherry-pick bad object error typically arises when Git cannot find the commit you're trying to cherry-pick.

This guide provides step-by-step instructions to troubleshoot and resolve this issue.

The git cherry-pick command is used to apply the changes introduced by one or more commits from a different branch onto the current branch. The error message fatal: bad object essentially means Git is unable to locate the commit hash you've specified for cherry-picking. This can happen for several reasons:

  • The commit hash is incorrect or mistyped.

  • The commit exists in a remote repository but hasn't been fetched to your local repository.

  • The commit is in a branch that you haven't checked out or fetched into your local repository.

  • Double-check the commit hash: Ensure the commit hash you're using in the git cherry-pick command is correct. A simple typo can lead to the bad object error.
Stop wrestling with Git commands

The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop Googling Git commands.

Learn more
  • Fetch updates from the remote repository: If the commit exists in a remote repository, you may need to fetch the updates:
Terminal
git fetch origin

Replace origin with the name of your remote. This command updates your local copy of the remote repository but doesn't merge any changes into your working branch.

  • Check if the commit is in the fetched data: Use the following command to search for the commit in all branches, including those fetched from the remote:
Terminal
git log --all --grep="commit message"

Replace "commit message" with a unique part of the commit message. If you find the commit, ensure you have the correct hash.

The best engineers use Graphite to simplify Git

Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.

Git started for free

If you know which branch the commit is on, you can fetch that specific branch:

Terminal
git fetch origin branch-name:branch-name

Replace branch-name with the name of the branch containing the commit. This command fetches the branch from the remote named origin and updates (or creates) a local branch with the same name.

Once you've confirmed the commit exists in your local repository, attempt the cherry-pick operation again using the correct commit hash:

Terminal
git cherry-pick commit-hash

Replace commit-hash with the correct hash of the commit you wish to cherry-pick.

While Git is an incredibly useful tool, it has many shortcomings, particularly with rebasing, and managing stacked pull requests.

The Graphite CLI simplifies git, handles rebasing automatically, and allows you to create, submit, and stack pull requests right from the command line.

Under the hood, the CLI runs Git to create branches, commits, and metadata, which means you can still use Git in your scripts, tooling, or whenever you feel like it. Read more about installing the Graphite CLI in our docs.

screenshot of the Graphite CLI

  • Using the correct repository: Verify you're working in the correct local repository that should contain the commit.

  • Ensuring branch existence: If the commit is on a branch that doesn't exist locally, you'll need to fetch or pull that branch from the remote.

If after following these steps you are still unable to resolve the bad object error, please see the official git documentation on git cherry-pick.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2