git 浅克隆后推送到 github
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11375070/
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
Pushing to github after a shallow clone
提问by snowangel
I had a massive git repo because of a huge number of commits, so following advice hereI created a shallow clone. I've made changes to this new local repo, and now I want to push to my origin at Github (and then on to my staging and production remotes on Heroku). Perhaps one day I'll learn to read the documentation:
由于大量的提交,我有一个庞大的 git 仓库,所以按照这里的建议我创建了一个浅克隆。我已经对这个新的本地存储库进行了更改,现在我想推送到我在 Github 的源(然后推送到我在 Heroku 上的暂存和生产远程)。也许有一天我会学会阅读文档:
The git clone --depth command option says
--depth Create a shallow clone with a history truncated to the specified number of revisions. A shallow repository has a number of limitations (you cannot clone or fetch from it, nor push from nor into it)
git clone --depth 命令选项说
--depth 创建一个浅层克隆,其历史记录被截断为指定的修订数量。浅层存储库有许多限制(你不能从它克隆或获取,也不能从它推入或推入)
So... how can I unpick myself from this situation and push my code to Github?
那么......我怎样才能摆脱这种情况并将我的代码推送到 Github?
采纳答案by sj26
Git (since 1.8.3) now has an official way to fetch the full history of a shallow clone:
Git(自 1.8.3 起)现在有一种官方方法来获取浅克隆的完整历史记录:
git fetch --unshallow
From the git fetch documentation:
--unshallow
If the source repository is complete, convert a shallow repository to a complete one, removing all the limitations imposed by shallow repositories.
If the source repository is shallow, fetch as much as possible so that the current repository has the same history as the source repository.
--unshallow
如果源存储库是完整的,则将浅存储库转换为完整存储库,消除浅存储库强加的所有限制。
如果源存储库较浅,则尽可能多地获取,以便当前存储库与源存储库具有相同的历史记录。
回答by Antoine Pelisse
I will not agree with the accepted answer for 2 reasons:
我不会同意接受的答案有两个原因:
- There are many reasons to fail and forget a file
- You lose your commit messages and history
- 失败和忘记文件的原因有很多
- 您丢失了提交消息和历史记录
Here are my suggestions:
以下是我的建议:
Graft point
嫁接点
You should have a $GIT_DIR/.git/shallow file with a graft point. If the history is simple enough, this graft point should allow you to push even though documentation says otherwise.
你应该有一个带有嫁接点的 $GIT_DIR/.git/shallow 文件。如果历史足够简单,这个嫁接点应该允许你推送,即使文档另有说明。
Patches
补丁
This allows you to keep commit history and etc:
这允许您保留提交历史等:
git format-patch origin..master
Then clone the origin and reapply:
然后克隆原点并重新应用:
git clone origin_path
cp shallow_clone/*.patch deep_clone
cd deep_clone
git am *.patch
This time you can push !
这次可以推了!
git push
回答by Gregor
If you are working in a shallow clone and the lack of history is causing a problem, you can fetch more history with the --depth
option.
如果您在浅层克隆中工作并且缺少历史记录导致问题,您可以使用该--depth
选项获取更多历史记录。
git fetch --depth=20
Where 20 is is the amount of commits to fetch. Increase it if that is not enough.
其中 20 是要获取的提交量。如果这还不够,请增加它。
You can also use the --depth
option with git pull
.
您还可以将该--depth
选项与git pull
.
回答by dnaumenko
I had a similar problem with pushing shallow clone repo to Bitbucket servers and I didn't have an access to old history. Finally, I had found a solution. See a sample script with comments below:
我在将浅克隆存储库推送到 Bitbucket 服务器时遇到了类似的问题,但我无法访问旧历史。最后,我找到了解决方案。请参阅带有以下注释的示例脚本:
#!/bin/bash
# Fix shallowness
mv .git/shallow .git/info/grafts
git checkout --orphan temp # create temp empty commit
git reset --hard
git commit -m "Init" --allow-empty
# Replace all shallow commits ids with new commit id. I copy-paste all refs from shallow file
git replace 196cdbdb30e608aae2fd7cbe97cc8c0e6fa66c06 <commit_id_of_empty_init_above>
git replace 4c645849b296aaafc1809a9e1537c0fb305167ad <commit_id_of_empty_init_above>
git replace 50eab8bd8c416c47354331211b1efd8688ad8e97 <commit_id_of_empty_init_above>
git replace 649dc7577b87d1b05dff05bf9adc5e46f6612dfa <commit_id_of_empty_init_above>
git replace 6902148fde7b98ff0d6b6c6ebe929590322c95ff <commit_id_of_empty_init_above>
git remote set-url origin http://<username>:<password>@<example.com:port/repo.git> # reference to a remote repo to push
git push origin 'refs/replace/*' # push replace refs to remote repo first
git push -u origin master # push to master, finally
# Clear some garbage just in case
git filter-branch --tag-name-filter cat -- --all # rewrite history
git push --force origin
git fsck # check that everything is ok
回答by Eli
Option 1) If you still have the original repo, just fetch from it before pushing:
选项 1) 如果您仍然拥有原始存储库,只需在推送之前从中获取:
git fetch --unshallow
Option 2) BEWARE!this is only recommended for new repos, as this WILLresult in loss of history, and also is highly prone to conflicts!!
选项 2) 当心!这仅推荐用于新的存储库,因为这将导致历史丢失,并且很容易发生冲突!!
If you've already removed the repository where you fetched from, you need to discard all history with.
如果您已经删除了从中提取的存储库,则需要丢弃所有历史记录。
git filter-branch -- --all
git push
git filter-branch
: Lets you rewrite Git revision history
git filter-branch
: 让你重写 Git 修订历史
--
: separates filter-branch options from revision options
--
:将过滤器分支选项与修订选项分开
--all
: to rewrite all branches and tags
--all
: 重写所有分支和标签