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

10

268

I have a project under Git version control that I worked on both a server and my local computer. I originally had the remote origin set as my local computer but I would now like to change that to BitBucket.

On the server I used the command

git remote set-url origin bitbucket_address

But now when I try to push my project I get the error

 ! [remote rejected] master -> master (shallow update not allowed)

What is causing this and how do I fix it?

Pluri answered 11/3, 2015 at 10:16 Comment(6)
How did you clone your local version? git clone --depth?Dig
It was a while ago and I can't remember. Is there a way to find out?Pluri
There should be a file named shallow in you .git folder.Dig
Yes, I can see a shallow file.Pluri
See stackoverflow.com/a/50996201 for a solution that just discards (or rewrites) the missing historyEugenides
ideally enable receive.shallowUpdate on the git serverMalkin
D
487

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 your 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. If you already removed or replaced your old remote then you'll need to add it 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 remove the old remote.

Dig answered 11/3, 2015 at 11:23 Comment(7)
what if i cloned a kick-start project and i don't want/need the whole history? is there a way to avoid it?Elfont
@Elfont This seems to be a good example for a perfectly valid new question. You could link to this question for reference.Dig
Asked as new question stackoverflow.com/questions/29748197/…Elfont
Note that git fetch --unshallow can take a refspec to only unshallow a certain branch rather than the whole repo. E.g.: git fetch --unshallow origin refs/heads/mydeepbranch:refs/remotes/origin/mydeepbranchPresbyterate
If you are pushing to a repo that is a bit behind whatever repo you cloned from, not creating an all-new repo, it is enough that your local reference is deep enough to contain the remote reference. So if your origin/master was 20 commits ahead of your oldrepo/master when you clone --depth 1'ed it, and you have made 17 local commits since, it is enough for you to do git fetch --depth 37 origin refs/heads/master:refs/remotes/origin/master (apologies for any off-by-one error), and then you can do git push oldrepo master without incident (may require git 1.9.0 or newer).Presbyterate
@Elfont delete /.git/ (& reference in package.json) and git initZenger
Usually you won't need to add old rep, so just use the git fetch commandGeostrophic
R
55

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

git fetch --unshallow upstream
Repertory answered 4/4, 2017 at 21:31 Comment(1)
this works for me, and quite easy then the top voted answer.Shoup
T
29

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
    
Tanta answered 30/8, 2018 at 22:54 Comment(3)
Thank you! Worked like a charm, when the original repository was deleted and you have only a shallow copy of it.Bracket
fatal: No rebase in progress?Reflexive
error: src refspec main does not match anyReflexive
V
17

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 origin your-new-repo
  • Then Push it.
Volkan answered 14/5, 2018 at 3:51 Comment(3)
I found this a better solution, since it doesn't require a push to the old. Sometimes this could happen with boilerplates.Incitement
This is basically the answer from the related question which you can find here.Dig
@NachPD I'm not sure what you mean, when you say that the other solution requires "a push to the old". Do you mean a fetch instead of a push? Because it does not require a push.Dig
B
4

If fetching --unshallow doesn't work. There must be some problems with your branch. Fix it with the following command before pushing it.

git filter-branch -- --all

Do this only with --unshallow doesn't work since there's a SAFETY concern.

Benita answered 6/3, 2020 at 13:19 Comment(1)
This will help you push a local repo with the history cut by git clone file://<path> <dirname> --depth=<N>. Such shallow clone will not be pushable, because the history will be only hidden by graft. The filter-branch will drop it.Unproductive
W
1

I fixed this issue. but maybe you can not fixed it. the solution as follows.

  1. get the shallow file from the git, like common/.git/shallow
  2. push this file to the .git directory in git server.
  3. push your branch to the git server.

In my company, I need the IT admin to add the file, and I have not permissions.

Wootten answered 24/2, 2022 at 5:39 Comment(0)
L
1

In case of bitbucket pipeline

It might be due to limited depth of commit (default 50 commit)

You can increase limit

clone:
  depth: 500       # include the last five hundred commits
  
pipelines:
  default:
    - step:
        name: Cloning
        script:
          - echo "Clone all the things!"

Note : Use depth: full for no limit

Lymphoblast answered 20/9, 2022 at 13:45 Comment(0)
B
0

Based on the most upvoted answer, I created an alias to automate things:

Add to your .gitconfig:

[alias]
    unshallow = !"git fetch --unshallow \"${1:-origin}\" # Unshallow from remote $1 (defaults to origin)"

Usage:

  • git unshallow # unshallow current branch based on the origin remote
  • git unshallow other-remote # unshallow current branch from remote other-remote
Beanfeast answered 6/9, 2020 at 14:41 Comment(0)
T
0

For simply resolving this issue if you don't care about the remote existing changes then do it the following way.

  1. Remove your local .git folder from your repository.
  2. Type git init.
  3. Add your remote again git remote add origin <REMOTE_URL>.
  4. Set main branch by git branch -M main.
  5. Push your changes then git push --set-upstream origin main.
Tacye answered 6/10, 2021 at 5:58 Comment(1)
fatal: Invalid branch name: 'HEAD'Reflexive
E
-1

Just delete the shallow file in your /.git/shallow

Now it should work.

Easter answered 6/6, 2022 at 13:2 Comment(2)
!git push -f https://{repo} main fatal: unable to get credential storage lock: File exists Counting objects: 347, done. Delta compression using up to 4 threads. Compressing objects: 100% (245/245), done. Writing objects: 100% (347/347), 56.28 MiB | 1.56 MiB/s, done. Total 347 (delta 153), reused 250 (delta 99) remote: Resolving deltas: 100% (153/153), done. remote: fatal: did not receive expected object 2aaa07546a89ead5a106b6ed48beb3ce3a485c8a error: unpack failed: index-pack failed To github.com ! [remote rejected] main -> main (failed) error: failed to push some refs toReflexive
git fetch --unshallow github.com 解决上述问题Reflexive

© 2022 - 2024 — McMap. All rights reserved.