Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote rejected (shallow update not allowed) after changing Git remote URL

Tags:

git

People also ask

How do I change my git remote URL?

Update the URL for Git repositories Run git remote -v to see the current remote URL. Update the remote URL with git remote set-url using the current and new remote URLs.

What is remote URL in git?

Git remote is an important part of Git, a tool that provides an easy-to-use system for tracking changes in source code during software development. With Git, you can save the state of your code at regular intervals (determined by you).

What is shallow clone git?

Git shallow clone lets you pull down just the latest commits, not the entire repo history. So if your project has years of history, or history from thousands of commits, you can select a particular depth to pull.


As it seems you have used git clone --depth <number> to clone your local version. This results in a shallow clone. One limitation of such a clone is that you can't push from it into a new repository.

You now have two options:

  1. if you don't care about you're missing history, take a look at this question
  2. if you want to keep your full history, then continue reading:

So, you want to keep your history, eh? This means that you have to unshallow your repository. To do so you will need to add your old remote again.

git remote add old <path-to-old-remote>

After that we use git fetch to fetch the remaining history from the old remote (as suggested in this answer).

git fetch --unshallow old

And now you should be able to push into your new remote repository.


Note: After unshallowing your clone you can obviously remove the old remote again.


In case your repo is origin, and the original repo is upstream:

git fetch --unshallow upstream

Another option if you want to keep the repo as is with the new commits you have added since the shallow, initial commit is this: Amend this commit with an interactive rebase.

  • Start an interactive rebase including the first (root) commit with

    git rebase --interactive --root
    
  • Change the pick of the initial commit(s) to edit and save & close the file.

    If you've cloned the repo with greater depth than 1, you may need to do the same for all of those commits. Or, alternatively, execute fixup for all of these during the interactive rebase.

  • Convert this commit to a regular, unshallow commit with

    git commit --amend --no-edit
    

    This will also change the commit ID and add you as co-author to this initial commit.

  • Don't forget to finish your rebase

    git rebase --continue
    

If you want to push the new repo as it is, you can try this:

  • First remove the old git folder from your current repo,sudo rm -rf .git
  • Then initialize the git again git init
  • Then add the new remote repo git remote add your-new-repo
  • Then Push it.