0

I have the following situation which is different than this one.

Here's my local repo:

$ git branch
  branch1
  ...
  branchk
  master
* this-branch-where-fix-should-happen

What I want is to apply a commit that only exists in the upstream:staging branch.

I've tried to fetch upstream in a certain branch

$ git fetch upstream staging:staging

From github.com:<org>/<repo>
 * [new branch]              staging    -> staging

New output from branch.

$ git branch
  branch1
  ...
  branchk
  master
  staging
* this-branch-where-fix-should-happen

Now trying to cherry pick into fix-branch:

$ git cherry-pick -x <sha-1>
    fatal: bad object <sha-1>

Is there a correct way to cherry pick in this situation? Or am I doing it the completely wrong way?


Solution: Finding the problem and fixing it

As instructed by Tim Biegeleisen

Fetching upstream

$ git fetch upstream

This is where my mistake was, I was getting the commit number via the github site, however that was actually a <sha-1> for the merge. To get the correct commit number:

$ git log --pretty=oneline --grep="<some-info-about-the-commit>" upstream/staging

I've used --grep="#pull-request-number" and got the correct commit id.

<correct-sha-1> PR description (#PR-number)

Now trying to cherry pick into fix-branch:

$ git cherry-pick -x <correct-sha-1>

And it works!

Aristu
  • 761
  • 3
  • 17
  • 30

1 Answers1

1

As soon as you git fetch, the staging remote branch should actually exist locally, as a tracking branch called upstream/staging, or something similar to that. If you want to target a specific commit from this tracking branch, which mirrors the remote, just type

git log upstream/staging

to see the commits from this branch, and pick the one you want. Now cherry picking should work:

# from the this-branch-where-fix-should-happen branch
git cherry-pick -x <sha-1 from staging>
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360