Retrieve specific commit from a remote Git repository

后端 未结 10 1611
谎友^
谎友^ 2020-11-22 06:32

Is there any way to retrieve only one specific commit from a remote Git repo without cloning it on my PC? The structure of remote repo is absolutely same as that of mine and

相关标签:
10条回答
  • 2020-11-22 06:39

    Finally i found a way to clone specific commit using git cherry-pick. Assuming you don't have any repository in local and you are pulling specific commit from remote,

    1) create empty repository in local and git init

    2) git remote add origin "url-of-repository"

    3) git fetch origin [this will not move your files to your local workspace unless you merge]

    4) git cherry-pick "Enter-long-commit-hash-that-you-need"

    Done.This way, you will only have the files from that specific commit in your local.

    Enter-long-commit-hash:

    You can get this using -> git log --pretty=oneline

    0 讨论(0)
  • 2020-11-22 06:41

    You only clone once, so if you already have a clone of the remote repository, pulling from it won't download everything again. Just indicate what branch you want to pull, or fetch the changes and checkout the commit you want.

    Fetching from a new repository is very cheap in bandwidth, as it will only download the changes you don't have. Think in terms of Git making the right thing, with minimum load.

    Git stores everything in .git folder. A commit can't be fetched and stored in isolation, it needs all its ancestors. They are interrelated.


    To reduce download size you can however ask git to fetch only objects related to a specific branch or commit:

    git fetch origin refs/heads/branch:refs/remotes/origin/branch
    

    This will download only commits contained in remote branch branch (and only the ones that you miss), and store it in origin/branch. You can then merge or checkout.

    You can also specify only a SHA1 commit:

    git fetch origin 96de5297df870:refs/remotes/origin/foo-commit
    

    This will download only the commit of the specified SHA-1 96de5297df870 (and its ancestors that you miss), and store it as (non-existing) remote branch origin/foo-commit.

    0 讨论(0)
  • 2020-11-22 06:41

    I think 'git ls-remote' ( http://git-scm.com/docs/git-ls-remote ) should do what you want. Without force fetch or pull.

    0 讨论(0)
  • 2020-11-22 06:43

    If the requested commit is in the pull requests of the remote repo, you can get it by its ID:

    # Add the remote repo path, let's call it 'upstream':
    git remote add upstream https://github.com/repo/project.git
    
    # checkout the pull ID, for example ID '60':
    git fetch upstream pull/60/head && git checkout FETCH_HEAD
    
    0 讨论(0)
  • 2020-11-22 06:46

    This works best:

    git fetch origin specific_commit
    git checkout -b temp FETCH_HEAD
    

    name "temp" whatever you want...this branch might be orphaned though

    0 讨论(0)
  • 2020-11-22 06:49

    I did a pull on my git repo:

    git pull --rebase <repo> <branch>
    

    Allowing git to pull in all the code for the branch and then I went to do a reset over to the commit that interested me.

    git reset --hard <commit-hash>

    Hope this helps.

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