263

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?

rwolst
  • 12,904
  • 16
  • 54
  • 75

10 Answers10

482

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.

Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73
  • 61
    what if i cloned a kick-start project and i don't want/need the whole history? is there a way to avoid it? – itamar Mar 25 '15 at 16:43
  • 10
    @itamar This seems to be a good example for a perfectly valid new question. You could link to this question for reference. – Sascha Wolf Apr 20 '15 at 06:36
  • 17
    Asked as new question http://stackoverflow.com/questions/29748197/how-to-clone-seed-kick-start-project-without-the-whole-history – itamar Apr 20 '15 at 12:41
  • 5
    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/mydeepbranch` – clacke Oct 31 '15 at 14:07
  • 5
    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). – clacke Oct 31 '15 at 14:13
  • 2
    @itamar delete /.git/ (& reference in package.json) and git init – user36388 Jun 13 '17 at 09:50
  • Usually you won't need to add old rep, so just use the git fetch command – user2679290 Aug 22 '21 at 16:43
54

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

git fetch --unshallow upstream
Dorian
  • 22,759
  • 8
  • 120
  • 116
28

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
    
Jayen
  • 5,653
  • 2
  • 44
  • 65
Rene Hamburger
  • 2,003
  • 16
  • 17
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.
Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74
Akhter-uz-zaman
  • 357
  • 3
  • 7
  • I found this a better solution, since it doesn't require a push to the old. Sometimes this could happen with boilerplates. – rnpd Jul 02 '18 at 15:57
  • This is basically the answer from the [related question which you can find here](http://stackoverflow.com/questions/29748197/how-to-clone-seed-kick-start-project-without-the-whole-history). – Sascha Wolf Aug 10 '18 at 10:51
  • 2
    @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. – Sascha Wolf Aug 10 '18 at 10:51
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.

Chayapol
  • 3,718
  • 1
  • 21
  • 12
  • This will help you push a local repo with the history cut by `git clone file:// --depth=`. Such shallow clone will not be pushable, because the history will be only hidden by graft. The `filter-branch` will drop it. – Ferdinand Prantl Apr 29 '22 at 10:36
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.

LUAN.YUAN
  • 11
  • 1
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

Mihir Bhatt
  • 3,019
  • 2
  • 37
  • 41
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
Tom Hale
  • 40,825
  • 36
  • 187
  • 242
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.
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Mohamed Jakkariya
  • 1,257
  • 1
  • 13
  • 16
-1

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

Now it should work.

Mbs Yaswanth
  • 144
  • 2
  • 6
  • !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 https://github.com/ ! [remote rejected] main -> main (failed) error: failed to push some refs to – CS QGB Apr 19 '23 at 11:57
  • git fetch --unshallow https://github.com/ 解决上述问题 – CS QGB Apr 19 '23 at 11:57