Search code examples
gitcommitpullgit-fetch

Pull a commit from a different repository


I've cloned a project for me to work on, but a friend made a change on the original project. I would like to pull the changes from that commit into my project.

Using

git fetch https://github.com/cvandermeer/wisemonkeys.git f70bcfd75a498ed9159b6e5313e306166fc3df62

throws the following error:

error: no such remote ref f70bcfd75a498ed9159b6e5313e306166fc3df62

git remote -v gives me the following,

origin  https://github.com/alucardu/appsynth.git (fetch)
origin  https://github.com/alucardu/appsynth.git (push)

Is it not possible to pull a commit from a different project?


Solution

  • (For convenience, let's call your friend "Alice" in the following.)

    You fetch references (branches or tags), not commits

    I would like to pull the changes from that commit into my project.

    Is it not possible to pull a commit from a different project?

    Strictly speaking, you don't fetch commits; you fetch references (branches or tags). If you're interested in a particular commit that exists in Alice's repo but not in yours, you'll need to fetch a reference whose ancestry contains the commit in question from Alice's repo.

    Use a valid git fetch syntax

    Using [...] throws the following error [...]

    The command you're using,

    git fetch <remote-url> <commit>
    

    is not a valid git fetch syntax, hence the error you're getting. The syntax you want to use is

    git fetch <repository> 
    

    where <repository> is a remote, i.e. a nickname under which your local repo knows Alice's repo. You may want to be more specific and also add a refspec at the end of this command.

    Edit: As pointed out by torek in his comment, you can also use a naked URL, without configuring the remote in your local repository, but that's probably not what you want to do, here.

    Add Alice's repo as a remote first

    git remote -v gives me the following [...]

    The output of git remote -v indicates that you haven't added Alice's repo as a remote of your local repository yet. You need to to that before you can fetch from it:

    git remote add alice https://github.com/cvandermeer/wisemonkeys.git
    

    Fetch and find out more about the commit of interest

    Then run

    git fetch alice
    

    to fetch everything you don't already have from Alice's repo. You can then run

    git name-rev f70bcfd75a498ed9159b6e5313e306166fc3df62  
    

    to identify a reference from which the commit is accessible, and

    git show f70bcfd75a498ed9159b6e5313e306166fc3df62
    

    to print information about the commit and decide what to do with it (cherry-pick it, merge the branch it's on into one of your local branches, etc.).

    Additional resources