How to convert a Git shallow clone to a full clone?
Asked Answered
J

7

401

Follow-up of this so-question: if I have a shallow clone, how to fetch all older commits to make it a full clone?

Joplin answered 23/7, 2011 at 17:39 Comment(1)
J
239

EDIT: git fetch --unshallow now is an option (thanks Jack O'Connor).

You can run git fetch --depth=2147483647

From the docs on shallow:

The special depth 2147483647 (or 0x7fffffff, the largest positive number a signed 32-bit integer can contain) means infinite depth.

Jellyfish answered 23/7, 2011 at 17:55 Comment(8)
Now that git fetch --unshallow exists (as in @sdram's answer), this answer is no longer the best one.Spherule
@sdram's answer did not work for me (git version 2.1.1), but this answer did.Postmark
Neither answer worked for me. Both commands succeeded in fetching all the missing commits, but when I try to push new commits, I get an error about the server not knowing about 'shallow' refsBrew
git fetch --depth=2147483647 is the largest possible depth to provide to the command.Wilkison
I used git fetch --unshallow, but it still does not show all the branches.Caw
@Sid, stackoverflow.com/questions/11623862/… fixed that for me.Ravishing
Tip: on an unstable internet connection, we can incrementally git fetch --depth=1000 then again git fetch --depth=10000 and so on.Mancy
Use of git fetch --depth=2147483647 is sufficient within Azure DevOps Pipeline.Mnemonic
B
908

The below command (git version 1.8.3) will convert the shallow clone to regular one

git fetch --unshallow

Then, to get access to all the branches on origin (thanks @Peter in the comments)

git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin
Bear answered 30/7, 2013 at 3:36 Comment(11)
This doesn't undo the --single-branch side effect. To do that, edit .git/config and change fetch = +refs/heads/BRANCHNAME:refs/remotes/origin/BRANCHNAME to fetch = +refs/heads/*:refs/remotes/origin/*Raskin
This doesn't create local branches tracking the remote branches, so you still need to checkout -b BRNAME origin/BRNAME to get that set up.Raskin
See also stackoverflow.com/questions/17714159/…: git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"; git fetch origin from an answer there should be the same as editting .git/config by handRaskin
This only works if the repo is marked as shallow. I can't remember how, but there are situations where you can end up with an incomplete repo without having explicitly done a shallow clone. @svick's https://mcmap.net/q/13343/-how-to-convert-a-git-shallow-clone-to-a-full-clone is the answer that works every time.Wilkison
git fetch --unshallow --update-head-ok origin '+refs/heads/*:refs/heads/*' worked for meJaconet
@gzaripov's one-liner did not work for me. It resulted in a local snapshot of remote branches that could not be configured with tracking. The three-lines given in this answer, however, do work.Photomechanical
This is the only solution that @Peter_Cordes posted, that worked for meReeding
See also https://mcmap.net/q/12258/-git-shallow-clone-clone-depth-misses-remote-branches: git remote set-branches origin '*' does similar thingQuiberon
@Jaconet under what circumstances is using the porcelain --update-head-ok useful?Hae
fatal: --unshallow on a complete repository does not make sense If 799 people agree, something must be wrong at my end. I've yet to determine what.Fornax
Note: this option is available to git pull, as well, i.e., git pull --unshallow.Tapestry
J
239

EDIT: git fetch --unshallow now is an option (thanks Jack O'Connor).

You can run git fetch --depth=2147483647

From the docs on shallow:

The special depth 2147483647 (or 0x7fffffff, the largest positive number a signed 32-bit integer can contain) means infinite depth.

Jellyfish answered 23/7, 2011 at 17:55 Comment(8)
Now that git fetch --unshallow exists (as in @sdram's answer), this answer is no longer the best one.Spherule
@sdram's answer did not work for me (git version 2.1.1), but this answer did.Postmark
Neither answer worked for me. Both commands succeeded in fetching all the missing commits, but when I try to push new commits, I get an error about the server not knowing about 'shallow' refsBrew
git fetch --depth=2147483647 is the largest possible depth to provide to the command.Wilkison
I used git fetch --unshallow, but it still does not show all the branches.Caw
@Sid, stackoverflow.com/questions/11623862/… fixed that for me.Ravishing
Tip: on an unstable internet connection, we can incrementally git fetch --depth=1000 then again git fetch --depth=10000 and so on.Mancy
Use of git fetch --depth=2147483647 is sufficient within Azure DevOps Pipeline.Mnemonic
B
32

I needed to deepen a repo only down to a particular commit.

After reading man git-fetch, I found out that one cannot specify a commit, but can specify a date:

git fetch --shallow-since=15/11/2012

For those who need incremental deepening, another man quote:

--deepen=<depth>

Similar to --depth, except it specifies the number of commits from the current shallow boundary instead of from the tip of each remote branch history.

Bastian answered 11/5, 2017 at 22:30 Comment(0)
S
17

Two ways to achieve Shallow Clone to Deep Clone. :

  1. Used the following steps to download the branch: (This downloads the shallow copy of the branch and then converts it into a Full Clone i.e bring complete branch and its history).

    a. git clone -b branch http://git.repository/customSP01.git --depth 1

This does a shallow clone (with the depth-option) only fetches only one single branch (at your requested depth).

b. cd customSP01
c. git fetch --depth=100
d. get fetch --depth=500
....
e. git fetch --unshallow    

//The above command will convert the shallow clone to regular one. However, this doesn’t bring all the branches:

Then, to get access to all the branches.

f. git remote set-branches origin '*'

[This Step can also be done manually by editing following line in .git/config.

fetch = +refs/heads/master:refs/remotes/origin/master

to (replace master with *):

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

g. git fetch -v

This converts the Shallow Clone into Deep Clone with all the History and Branch details.


  1. You can avoid steps f and g, if you use the below instead of command present in step a. to do the shallow clone:

    git clone -b branch --no-single-branch http://git.repository/customSP01.git --depth 1

Satsuma answered 3/7, 2020 at 11:6 Comment(1)
I only needed step F. I did git clone --depth=1 <url>, but then git fetch --unshallow did not fix it, nor did git fetch --all: remote branch list still just had master & HEAD. Step F fixed it.Tycho
T
2

You can try this:

git fetch --update-shallow
Tumulus answered 25/8, 2017 at 2:39 Comment(1)
--update-shallow doesnt work if shallow clone is performed with depth value.Blowout
B
1

None of the above messages did the trick. I'm trying to work with git tags starting from a shallow clone.

First I tried

git fetch --update-shallow

which kind of worked half-way through. Yet, no tags available!

git fetch --depth=1000000

This last command really fetched the tags and I could finally execute

git checkout -b master-v1.1.0 tags/v1.1.0

and be done with it.

HTH

Badmouth answered 1/3, 2019 at 18:50 Comment(2)
What's the downvote for? Please explain so I can improve upon this. Thank you.Badmouth
I didn't downvote, but I think it might be because 'git checkout -b' is used to create a new local branch. So, I don't think it does what might be expected in the context of your answer.Naughton
N
-2

Configurations that helped with the error is (In GitLab) For each project :

  1. On the top bar, select Main menu > Projects and find your project.
  2. On the left sidebar, select Settings > CI/CD. Expand General pipelines.
  3. Under Git strategy, choose git fetch, under Git shallow clone, enter a value, 1000, the maximum value for GIT_DEPTH Read More - https://gitlab.yourcompany.com/help/ci/pipelines/settings#limit-the-number-of-changes-fetched-during-clone{}

In the .gitlab-ci-yml (this should be done before any command that calls GitVersion.exe)

  before_script:
    - git fetch --prune --tags --unshallow  
Newhall answered 7/6, 2023 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.