rwolst
rwolst

Reputation: 13702

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

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?

Upvotes: 275

Views: 133019

Answers (10)

Mihir Bhatt
Mihir Bhatt

Reputation: 3165

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

Upvotes: 1

Akhter-uz-zaman
Akhter-uz-zaman

Reputation: 367

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.

Upvotes: 17

Alex Wolf
Alex Wolf

Reputation: 20178

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.

Upvotes: 497

Mbs Yaswanth
Mbs Yaswanth

Reputation: 150

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

Now it should work.

Upvotes: -3

LUAN.YUAN
LUAN.YUAN

Reputation: 31

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.

Upvotes: 3

Mohamed Jakkariya
Mohamed Jakkariya

Reputation: 1657

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.

Upvotes: 0

Tom Hale
Tom Hale

Reputation: 47043

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

Upvotes: 0

Chayapol
Chayapol

Reputation: 3944

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.

Upvotes: 4

Rene Hamburger
Rene Hamburger

Reputation: 2253

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
    

Upvotes: 33

Dorian
Dorian

Reputation: 24009

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

git fetch --unshallow upstream

Upvotes: 57

Related Questions