277

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?

6
  • 4
    How did you clone your local version? git clone --depth? Commented Mar 11, 2015 at 10:54
  • It was a while ago and I can't remember. Is there a way to find out? Commented Mar 11, 2015 at 11:01
  • 3
    There should be a file named shallow in you .git folder. Commented Mar 11, 2015 at 11:05
  • Yes, I can see a shallow file. Commented Mar 11, 2015 at 11:06
  • See stackoverflow.com/a/50996201 for a solution that just discards (or rewrites) the missing history Commented Mar 30, 2019 at 17:16

10 Answers 10

501

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
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).
@itamar delete /.git/ (& reference in package.json) and git init
Usually you won't need to add old rep, so just use the git fetch command
57

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

git fetch --unshallow upstream

Comments

33

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
    

2 Comments

fatal: No rebase in progress?
error: src refspec main does not match any
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.

3 Comments

I found this a better solution, since it doesn't require a push to the old. Sometimes this could happen with boilerplates.
This is basically the answer from the related question which you can find here.
@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.
5

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.

1 Comment

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.
3

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.

1 Comment

PERFECT! basically, these are my steps: 1. create a repo using git init on the server, 2. cat .git/shallow on a client machine that has a large git history, 3. add .git/shallow on the server with the content from step 2, 4. git push from the client to the server.
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

Comments

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

Comments

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.

1 Comment

fatal: Invalid branch name: 'HEAD'
-3

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

Now it should work.

1 Comment

!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 to

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.