Pushing to github after a shallow clone
Asked Answered
P

6

28

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?

Pellicle answered 7/7, 2012 at 12:38 Comment(2)
What's the problem? What did you try?Moonier
For future reference: git now supports pushing from shallow clones (since version 1.9). It will still fail when pushing to an outdated fork that's older than your history.Livestock
F
2

From @nschum

For future reference: git now supports pushing from shallow clones (since version 1.9). It will still fail when pushing to an outdated fork that's older than your history.

(Aside: Unfortunately copy/paste is the recommended way to promote a comment to an answer)

Factotum answered 7/7, 2012 at 12:39 Comment(1)
Do I have git 1.9? Yes, git 1.9 came out in 2014 github.com/msysgit/msysgit/releases/tag/…Fitton
Z
19

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.

Zeldazelde answered 21/3, 2016 at 5:18 Comment(0)
S
14

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
Soelch answered 8/7, 2012 at 8:50 Comment(4)
That is very lame though to copy paste my answer into yoursSoelch
In my case, it gave the error "<some-file> does not exist in the index"Macron
I have also applied this solution to the angular-seed project -hosted in github- which was cloned using --depth=1. When trying to push to a different remote target repo, I got the error "! [remote rejected] master -> master (shallow update not allowed)". Using the target repo as origin worked just well.Physicochemical
`"You should have a $GIT_DIR/.git/shallow file with a graft point." -- How to create that?Refreshment
W
13

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.

Wording answered 6/8, 2012 at 19:32 Comment(0)
Q
5

Option 1) If you still have the original repo, just fetch from it before pushing:

git fetch --unshallow

Option 2) BEWARE! this is only recommended for new repos, as this WILL result in loss of history, and also is highly prone to conflicts!!

If you've already removed the repository where you fetched from, you need to discard all history with.

 git filter-branch -- --all
 git push

git filter-branch : Lets you rewrite Git revision history

-- : separates filter-branch options from revision options

--all : to rewrite all branches and tags

Quarterphase answered 21/8, 2017 at 10:13 Comment(2)
Please edit your answer and explain why this is an answer to the question. Code-only answers are not very helpful.Defend
ahhh, THANK YOU bro! this is the best answer, no need to mess with origina fetch what i already deleted!Chlorite
R
4

I had a similar problem with pushing shallow clone repo to Bitbucket servers and I didn't have an access to old history. Finally, I had found a solution. See a sample script with comments below:

#!/bin/bash

# Fix shallowness
mv .git/shallow .git/info/grafts

git checkout --orphan temp # create temp empty commit
git reset --hard
git commit -m "Init" --allow-empty

# Replace all shallow commits ids with new commit id. I copy-paste all refs from shallow file 
git replace 196cdbdb30e608aae2fd7cbe97cc8c0e6fa66c06 <commit_id_of_empty_init_above>
git replace 4c645849b296aaafc1809a9e1537c0fb305167ad <commit_id_of_empty_init_above>
git replace 50eab8bd8c416c47354331211b1efd8688ad8e97 <commit_id_of_empty_init_above>
git replace 649dc7577b87d1b05dff05bf9adc5e46f6612dfa <commit_id_of_empty_init_above>
git replace 6902148fde7b98ff0d6b6c6ebe929590322c95ff <commit_id_of_empty_init_above>
git remote set-url origin http://<username>:<password>@<example.com:port/repo.git> # reference to a remote repo to push
git push origin 'refs/replace/*' # push replace refs to remote repo first
git push -u origin master # push to master, finally

# Clear some garbage just in case
git filter-branch --tag-name-filter cat -- --all # rewrite history
git push --force origin
git fsck # check that everything is ok
Richierichlad answered 26/10, 2015 at 13:34 Comment(1)
<GIT_DIR>/info/grafts is deprecatedSclerosis
F
2

From @nschum

For future reference: git now supports pushing from shallow clones (since version 1.9). It will still fail when pushing to an outdated fork that's older than your history.

(Aside: Unfortunately copy/paste is the recommended way to promote a comment to an answer)

Factotum answered 7/7, 2012 at 12:39 Comment(1)
Do I have git 1.9? Yes, git 1.9 came out in 2014 github.com/msysgit/msysgit/releases/tag/…Fitton

© 2022 - 2024 — McMap. All rights reserved.