Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Push shallow clone to a new remote without 'unshallow'?

Tags:

git

I made a shallow clone of a repository with git clone --depth=1. I made some changes... apparently without understanding the complications of shallow clones... and now want to push the new project to a new remote.

On my main dev machine:

git clone --depth=1 file://old_project new_project
cd new_project
# made a handful of commits here....

I now want to push the project to another machine. On that remote machine I did:

git init --bare new_project.git

And then back on my machine:

git remote remove origin
git remote add origin ssh://<remote host>/path/to/repos/new_project.git

Now when I try to push the project, I get:

fatal: protocol error: expected old/new/ref, got 'shallow 7f6a256...'
fatal: The remote end hung up unexpectedly
Counting objects: 49299, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (44533/44533), done.
error: pack-objects died of signal 13
error: failed to push some refs to '<my new remote>'

What I'd like to do is have the new repo contain the history from the initial shallow clone onward, but still retain the changes made after that point. In other words, I don't want to "unshallow" the branch and pull all the previous history.

Is there a way forward without with just deleting .git directory from the project and starting over?

My machine is running git 1.9.1. The remote is running 1.7.11.7. I can probably update my side with no ill effects, but not the remote as it hosts several other projects that I don't want to risk disrupting.

like image 939
Brian McFarland Avatar asked Nov 01 '25 15:11

Brian McFarland


1 Answers

I would do it as follows:

  1. Create a new root branch:

    git checkout --orphan truncated_master
    
  2. Populate its initial commit with the earliest available commit in your shallow repository:

    START_COMMIT=$(git rev-list master|tail -n 1)
    git checkout $START_COMMIT -- .
    git commit -m "Initial commit"
    
  3. Copy all commits from master to the new branch:

    git cherry-pick $START_COMMIT..master
    
  4. Push the new branch to the new remote:

    git push origin truncated_master:master
    
like image 192
Leon Avatar answered Nov 03 '25 06:11

Leon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!