325

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

Mot
  • 26,308
  • 23
  • 82
  • 120

6 Answers6

802

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
jxmallett
  • 3,957
  • 1
  • 27
  • 34
Ramkumar D
  • 8,400
  • 2
  • 16
  • 17
  • 51
    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/* – Peter Cordes Jul 29 '14 at 21:36
  • 3
    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. – Peter Cordes Jul 29 '14 at 21:45
  • 31
    See also http://stackoverflow.com/questions/17714159/how-do-i-undo-a-single-branch-clone: `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 hand – Peter Cordes Dec 08 '14 at 23:50
  • 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://stackoverflow.com/a/6802238/260122 is the answer that works every time. – clacke Apr 27 '17 at 05:45
  • 3
    `git fetch --unshallow --update-head-ok origin '+refs/heads/*:refs/heads/*'` worked for me – gzaripov May 25 '20 at 09:32
  • @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. – M. Anthony Aiello Sep 17 '20 at 15:27
  • This is the only solution that @Peter_Cordes posted, that worked for me – Prometheus Oct 28 '20 at 10:24
  • See also https://stackoverflow.com/a/27393574/475821: `git remote set-branches origin '*'` does similar thing – esmirnov Sep 10 '21 at 07:13
  • @gzaripov under what circumstances is using the porcelain `--update-head-ok` useful? – Tom Hale Feb 08 '22 at 09:06
  • `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. – Sridhar Sarnobat May 25 '22 at 04:05
199

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.

Tom Hale
  • 33,125
  • 23
  • 155
  • 216
svick
  • 225,720
  • 49
  • 378
  • 501
  • 261
    Now that `git fetch --unshallow` exists (as in @sdram's answer), this answer is no longer the best one. – Jack O'Connor Apr 14 '14 at 08:41
  • 1
    @sdram's answer did not work for me (git version 2.1.1), but this answer did. – kay Nov 07 '14 at 14:48
  • 2
    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' refs – Tyguy7 Sep 19 '15 at 00:08
  • 5
    `git fetch --depth=2147483647` is the largest possible depth to provide to the command. – clacke Apr 27 '17 at 05:47
  • 9
    I used `git fetch --unshallow`, but it still does not show all the branches. – Sid Oct 03 '17 at 14:29
  • 3
    @Sid, https://stackoverflow.com/questions/11623862/git-fetch-doesnt-fetch-all-branches fixed that for me. – Bryan Larsen Oct 10 '18 at 13:25
  • Tip: on an unstable internet connection, we can incrementally `git fetch --depth=1000` then again `git fetch --depth=10000` and so on. – Zzz0_o Apr 07 '21 at 11:19
29

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.

Victor Sergienko
  • 12,476
  • 3
  • 53
  • 81
13

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

Jeff Hammond
  • 4,945
  • 3
  • 26
  • 45
Rajeev Ranjan
  • 131
  • 1
  • 3
  • I only needed step F. I did `git clone --depth=1 `, 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. – Tom Aug 20 '21 at 21:26
2

You can try this:

git fetch --update-shallow
Nathan Tuggy
  • 2,230
  • 27
  • 29
  • 38
Altynai
  • 21
  • 1
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

Gen.Stack
  • 203
  • 2
  • 11
  • What's the downvote for? Please explain so I can improve upon this. Thank you. – Gen.Stack Dec 15 '20 at 12:10
  • 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. – Kevin Buchs Jan 29 '22 at 19:02