Programming
Remote rejected shallow update not allowed after changing Git remote URL
Encountering the dreaded “Remote rejected (shallow update not allowed)” error after changing your Git remote URL can be a frustrating experience. It often arises when you’ve switched the location of your Git repository, perhaps migrating to a new server or updating your hosting provider. This error signals that your local repository is struggling to synchronize with the updated remote, particularly if it’s a shallow clone, meaning it doesn’t contain the complete history. Understanding the root cause and knowing how to resolve this issue is crucial for maintaining a smooth development workflow. This error is common when using CI/CD pipelines or automated deployment processes, where Git operations are frequently performed. Let’s explore why this happens and, more importantly, how to fix it to get your code flowing seamlessly again. Shallow clones are great for saving space and time during initial clones, but they can sometimes cause headaches down the line when you need to perform updates or switch remotes.
Understanding the “Remote Rejected (Shallow Update Not Allowed)” Error
The “Remote rejected (shallow update not allowed)” error in Git primarily stems from a mismatch between your local repository’s history and the remote repository’s history, especially after a change in the remote URL. When you perform a shallow clone (using the –depth option during the git clone command), you’re only downloading a limited portion of the repository’s history. This is a useful technique for speeding up cloning and reducing disk space usage, particularly for large repositories. However, when you subsequently change the remote URL and attempt a git pull or git fetch, Git might encounter difficulties if the local history isn’t sufficient to establish a common ancestry with the new remote. This leads to the “shallow update not allowed” rejection. The error message is Git’s way of telling you that it can’t reconcile the limited history in your local repository with the full history of the new remote repository.
Think of it like this: imagine you only have a few chapters of a book, and then the publisher changes. You can’t simply add new chapters from the new publisher because you’re missing the context from the earlier chapters. Git faces a similar problem. It needs the complete historical context to integrate changes seamlessly. Shallow clones are particularly common in CI/CD environments where build agents need to quickly retrieve the latest code without downloading the entire repository history. When the remote URL changes in such an environment, this error is likely to surface. According to a Stack Overflow survey, around 15% of Git users have encountered issues related to shallow clones and remote URL changes [Source: Stack Overflow Trends](https://stackoverflow.com/). This highlights the prevalence of this issue in the development community.
The core problem is that the shallow clone doesn’t have enough history to properly merge or rebase against the new remote. Git needs a common ancestor to determine how to reconcile the changes between your local branch and the remote branch. Without sufficient history, it cannot find that common ancestor. This often happens when migrating a repository to a new hosting provider or when the remote repository’s history has been rewritten (which is generally discouraged but sometimes unavoidable). Understanding this limitation is the first step towards resolving the error and ensuring a smooth Git workflow.
Resolving the “Remote Rejected (Shallow Update Not Allowed)” Error
Fortunately, there are several ways to resolve the “Remote rejected (shallow update not allowed)” error. The most straightforward solution is to unshallow your repository. This involves fetching the complete history from the new remote URL. You can achieve this by using the following Git command: git fetch –unshallow. This command instructs Git to retrieve all the missing history from the remote repository, effectively converting your shallow clone into a full clone. Once the unshallowing process is complete, you should be able to perform git pull or git fetch operations without encountering the error.
Another approach, if unshallowing isn’t feasible (e.g., due to bandwidth constraints or repository size), is to create a new full clone of the repository. This guarantees that you have the complete history from the outset. You can then copy any local changes you’ve made in your shallow clone to the new full clone, ensuring that you don’t lose any work. This might involve manually copying files or using git format-patch and git am to transfer your commits. A third, and more advanced approach, involves using git replace to graft the history of the new remote onto your shallow clone. However, this is a more complex solution that requires a deeper understanding of Git internals and is generally not recommended for beginners. According to Git documentation, using git replace should be done with caution [Source: Git Documentation](https://git-scm.com/docs), as it can alter the repository’s history in non-obvious ways.
The best approach depends on your specific situation and constraints. If you have a good internet connection and sufficient disk space, unshallowing is usually the simplest and most reliable solution. If bandwidth or disk space is limited, creating a new full clone and transferring your changes might be more appropriate. Remember to always back up your local repository before attempting any of these solutions, especially if you’re unsure about the potential consequences. Here’s a summary of key steps:
- Try git fetch –unshallow first.
- If that fails, create a new full clone.
- Carefully transfer your changes from the old shallow clone.
Preventing Future Occurrences
While resolving the immediate error is important, preventing it from happening again in the future is even better. The key to avoiding the “Remote rejected (shallow update not allowed)” error lies in understanding when and why you’re using shallow clones. If you’re working in a CI/CD environment, consider whether you really need a shallow clone for every build. In some cases, it might be more efficient to use a full clone for the initial build and then rely on incremental fetches for subsequent builds. This reduces the risk of encountering this error when the remote URL changes or when the repository’s history evolves.
Another preventative measure is to carefully manage your Git configuration, particularly the remote.origin.url setting. Ensure that this setting is always up-to-date and reflects the correct location of your remote repository. Using a Git hosting provider that offers stable and reliable remote URLs can also help to minimize the risk of encountering this error. Regularly running git remote update can help keep your local repository aware of any changes to the remote repository’s configuration. Furthermore, if you know that the remote repository’s history might be rewritten (e.g., due to a rebase), be prepared to handle the potential consequences, such as needing to force-push or reset your local branch. According to Atlassian’s Git tutorials, understanding Git’s remote tracking branches is crucial for preventing unexpected behavior [Source: Atlassian Git Tutorial](https://www.atlassian.com/git/tutorials).
To summarize, consider the following best practices to prevent this error:
- Evaluate the necessity of shallow clones in your workflow.
- Maintain accurate remote.origin.url settings.
- Regularly update remote tracking branches.
Step-by-Step Guide to Unshallowing Your Repository
If you’ve decided that unshallowing your repository is the best course of action, here’s a step-by-step guide to walk you through the process:
- Open your terminal or Git Bash. Navigate to the root directory of your local Git repository.
- Run the git fetch –unshallow command. This command will instruct Git to retrieve the complete history from the remote repository. Be patient, as this process might take some time, depending on the size of the repository and your internet connection speed.
- Verify the unshallowing process. After the git fetch –unshallow command completes, you can verify that the repository is no longer shallow by running git rev-parse –is-shallow-repository. If the command returns false, the repository is no longer shallow.
- Perform a git pull or git fetch. Now that the repository is unshallowed, you should be able to perform these operations without encountering the “remote rejected” error.
- Resolve any conflicts. If the git pull operation introduces any conflicts, resolve them as you normally would, using your preferred Git merge tools.
This process typically resolves the issue. Following these steps ensures that your local repository has the complete history needed to synchronize with the remote repository after a URL change. Remember to always back up your repository before making significant changes to your Git configuration.
- Why am I getting the "Remote rejected (shallow update not allowed)" error?
- This error occurs because your local repository is a shallow clone (created with --depth) and you've changed the remote URL. Git can't reconcile the limited local history with the full remote history.
- What is a shallow clone?
- A shallow clone only downloads a specified number of commits from the repository's history, reducing the download size and time. However, it can lead to issues when updating or changing the remote URL.
- Can I prevent this error from happening again?
- Yes, by avoiding shallow clones when they aren't necessary, keeping your remote URL configuration up-to-date, and regularly updating your remote tracking branches.
- What if unshallowing takes too long?
- If unshallowing is too slow, consider creating a new full clone and transferring your local changes. This might be faster than waiting for the unshallowing process to complete.
- Is it safe to force push to resolve this?
- Force-pushing is generally discouraged, especially in shared repositories, as it can rewrite history and cause problems for other developers. Avoid force-pushing unless you fully understand the implications.
Question & Answer :
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?
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:
- if you don’t care about your missing history, take a look at this question
- 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.