Retrieve specific commit from a remote Git repository

后端 未结 10 1630
谎友^
谎友^ 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: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.

提交回复
热议问题