更改 Git 远程 URL 后远程被拒绝(不允许浅更新)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28983842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remote rejected (shallow update not allowed) after changing Git remote URL
提问by rwolst
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.
我有一个在 Git 版本控制下的项目,我在服务器和本地计算机上工作。我最初将远程源设置为我的本地计算机,但现在我想将其更改为 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?
这是什么原因造成的,我该如何解决?
回答by Sascha Wolf
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 newrepository.
看来您曾经git clone --depth <number>
克隆过本地版本。这导致浅克隆。这种克隆的一个限制是你不能从它推送到一个新的存储库。
You now have two options:
您现在有两个选择:
- if you don't care about you're current or 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 unshallowyour repository. To do so you will need to add your old remote 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
从旧遥控器获取剩余历史记录(如本答案中所建议)。
git fetch --unshallow old
And now you should be able to push into your new remote repository.
现在您应该能够推送到您的新远程存储库。
Note: After unshallowingyour clone you can obviously remove the old remote again.
注意:在取消您的克隆后,您显然可以再次删除旧遥控器。
回答by Dorian
In case your repo is origin
, and the original repo is upstream
:
如果您的回购是origin
,而原始回购是upstream
:
git fetch --unshallow upstream
回答by Akhter-uz-zaman
If you want to push the new repo as it is, you can try this:
如果你想按原样推送新的 repo,你可以试试这个:
- 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 your-new-repo
- Then Push it.
- 首先
old git folder
从您当前的回购中删除,sudo rm -rf .git
- 然后再次初始化git
git init
- 然后添加新的远程仓库
git remote add your-new-repo
- 然后推一下。
回答by Rene Hamburger
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.
如果您想将 repo 与自浅的初始提交以来添加的新提交保持原样,另一种选择是:使用交互式 rebase 修改此提交。
Start an interactive rebase including the first (root) commit with
git rebase --interactive --root
Change the
pick
of the initial commit(s) toedit
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
启动交互式变基,包括第一个(根)提交
git rebase --interactive --root
pick
将初始提交更改为edit
并保存并关闭文件。如果您克隆了深度大于 1 的存储库,您可能需要对所有这些提交执行相同的操作。或者,
fixup
在交互式变基期间执行所有这些。将此提交转换为常规的非浅层提交
git commit --amend --no-edit
这也将更改提交 ID,并将您添加为此初始提交的共同作者。
不要忘记完成你的rebase
git rebase --continue
回答by Chayapol
If fetching --unshallow doesn't work. There must be some problems with your branch. Fix it with the following command before pushing it.
如果获取 --unshallow 不起作用。你的分支肯定有问题。在推送之前使用以下命令修复它。
git filter-branch -- --all
Do this only with --unshallow doesn't work since there's a SAFETYconcern.
仅使用 --unshallow 执行此操作不起作用,因为存在安全问题。