Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pushing to github after a shallow clone

I had a massive git repo because of a huge number of commits, so following advice here I created a shallow clone. I've made changes to this new local repo, and now I want to push to my origin at Github (and then on to my staging and production remotes on Heroku). Perhaps one day I'll learn to read the documentation:

The git clone --depth command option says

--depth Create a shallow clone with a history truncated to the specified number of revisions. A shallow repository has a number of limitations (you cannot clone or fetch from it, nor push from nor into it)

So... how can I unpick myself from this situation and push my code to Github?

like image 220
snowangel Avatar asked Jul 07 '12 12:07

snowangel


3 Answers

Git (since 1.8.3) now has an official way to fetch the full history of a shallow clone:

git fetch --unshallow

From the git fetch documentation:

--unshallow

If the source repository is complete, convert a shallow repository to a complete one, removing all the limitations imposed by shallow repositories.

If the source repository is shallow, fetch as much as possible so that the current repository has the same history as the source repository.

like image 117
sj26 Avatar answered Oct 31 '22 20:10

sj26


I will not agree with the accepted answer for 2 reasons:

  1. There are many reasons to fail and forget a file
  2. You lose your commit messages and history

Here are my suggestions:

Graft point

You should have a $GIT_DIR/.git/shallow file with a graft point. If the history is simple enough, this graft point should allow you to push even though documentation says otherwise.

Patches

This allows you to keep commit history and etc:

git format-patch origin..master

Then clone the origin and reapply:

git clone origin_path
cp shallow_clone/*.patch deep_clone
cd deep_clone
git am *.patch

This time you can push !

git push
like image 14
Antoine Pelisse Avatar answered Oct 31 '22 19:10

Antoine Pelisse


If you are working in a shallow clone and the lack of history is causing a problem, you can fetch more history with the --depth option.

git fetch --depth=20

Where 20 is is the amount of commits to fetch. Increase it if that is not enough.

You can also use the --depth option with git pull.

like image 8
Gregor Avatar answered Oct 31 '22 19:10

Gregor