How to cherry-pick from a remote branch?

后端 未结 10 549
长发绾君心
长发绾君心 2020-12-07 11:17

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 \"zeb

相关标签:
10条回答
  • 2020-12-07 11:51

    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.

    0 讨论(0)
  • 2020-12-07 11:53

    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
    
    0 讨论(0)
  • 2020-12-07 11:55

    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 :)

    0 讨论(0)
  • 2020-12-07 11:55

    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.

    0 讨论(0)
  • 2020-12-07 11:57

    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(-)

    0 讨论(0)
  • 2020-12-07 11:58

    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.

    0 讨论(0)
提交回复
热议问题