Sunday, July 11, 2021

Git shallow clone and unshallow

Some git project is huge.  You want to clone but not care about the history, or only interested in the most recent stable version tagged.

Option 1, in github, select specific version tag in drop box, then click 'code' and download source as a zip file.

Option 2, git clone just that particular commit:


    $ git clone --depth 1 --b v3.5.6 {git_url} 


# --depth 1 implies --single-branchjust 1 commit, not all history on that branch.  Or, explicitly get more branches


    $ git fetch --depth 1 --no-single-branch {git_url}

    # 2021 Oct:    above command no longer works in git.

    # new method: 

    # modify .git/config, under [remote "origin"]

    # change fetch to

    #     fetch = +refs/heads/*:refs/remotes/origin/*

    # then,     git fetch --depth 1

    # (for details, see  https://stackoverflow.com/questions/23708231/git-shallow-clone-clone-depth-misses-remote-branches)


Above command gives you one commit, no history, no checkout branch.


    $ git branch       # nothing

    $ git branch -r    # also empty

    $ git branch -a    # all, check both locally & remotely


Now say, you want to get MORE history


    $ git fetch --depth=100


or get ALL history


    $ git fetch --unshallow


What if I want to get checkout another branch, for example, the master code now?


    $ git fetch  {git_url}  master:master

    $ git checkout master


# check out another branch with just one commit


    $ git fetch --depth 1 origin devel:devel



Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home