Mit*_*ril 18 git version-control github
我有一个大项目(让我们说A repo),它有一个来自的子文件夹B repo.当我承诺时,我会像下面那样遇到警告A repo
warning: adding embedded git repository: extractor/annotator-server
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint: git submodule add <url> extractor/annotator-server
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint: git rm --cached extractor/annotator-server
hint:
hint: See "git help submodule" for more information.
Run Code Online (Sandbox Code Playgroud)
我见过git-submodule并且git-subtree:
https://www.atlassian.com/blog/git/alternatives-to-git-submodule-git-subtree
但我不喜欢它们,因为它们需要额外的配置.
我想要的是,例如:
结构如:
A/
--- a.py
--- B/
--- B/b.py
Run Code Online (Sandbox Code Playgroud)
当我改变B/b.py.
如果我在路径上A/, git add可以检测到已B/b.py更改,git push只能将其提交给A repo.
git add . (would add changes under A/ )
git push (would push changes under A/ )
git pull (would pull changes under A/ )
git clone XXX:A (would clone all files under A/ , A/B/ is just looks like plain folder with all files, not a repo )
Run Code Online (Sandbox Code Playgroud)如果我在路径上A/B/,git add只添加B/b.py对B仓库的更改,并且git push只将其提交给B仓库.
git add . (would add changes under B/ , but not add changes to A repo)
git push (would push changes under B/ , but not push changes to A repo)
git pull (would clone changes under B/ , )
git clone XXX:B (would clone all files under B/ )
Run Code Online (Sandbox Code Playgroud)一旦我想在另一台机器上使用A和B,就行了
git clone A
rm -rf A/B/
git clone B ./B
git add . && git commit 'sync with B'
Run Code Online (Sandbox Code Playgroud)换句话说,A和B充当独立的回购.
但事实是,A repo将B repo视为子模块:
回购 https://github.com/eromoe/test
B repo https://github.com/eromoe/test2
如何强制A repo跟踪所有文件A/,B repo跟踪所有文件A/B/?我希望A和B充当自包含的回购,没有任何其他配置.
Mar*_*Liu 16
您可以使用以下命令将test2 repo中的文件添加到test repo,如下所示:
# In local test repo
rm -rf test2
git clone https://github.com/eromoe/test2
git add test2/
git commit -am 'add files from test2 repo to test repo'
git push
Run Code Online (Sandbox Code Playgroud)
注意:
你应该使用git add test2/(斜杠,而不是git add test2).
git add test2/将test2文件夹及其文件视为普通文件夹和文件以进行测试回购(创建模式100644).
git add test2将test2文件夹视为测试仓库(创建模式160000)的子模块.
小智 9
git可能提醒了存储库。它对我有帮助:
git rm-缓存your_folder_with_repo
git commit -m“删除缓存的仓库”
git添加your_folder_with_repo /
git commit -m“添加文件夹”
git推
对于登陆此页面的任何人,其目标只是将一堆 git 存储库归档到更大的父存储库或其他东西中,最简单的暴力解决方案是将所有嵌套.git文件夹重命名为其他任何内容 - 例如: to ..git. 现在,git add -A将像父 git 项目中的任何其他普通文件夹一样将它们全部添加,并且您可以git commit轻松地在父存储库中添加所有内容。完毕。
(https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles 的一部分)。
我刚刚在周末写了这个脚本,并且已经在许多项目中使用了它。它运作良好!有关详细信息和安装,请参阅文件顶部的注释,并运行git disable-repos -h以获取帮助菜单。
安装:
git clone https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.git
cd eRCaGuy_dotfiles/useful_scripts
mkdir -p ~/bin
ln -si "${PWD}/git-disable-repos.sh" ~/bin/git-disable-repos
# If this is the first time using your ~/bin dir, log out and
# log back in now. Otherwise, just re-source your .bashrc file:
. ~/.bashrc
Run Code Online (Sandbox Code Playgroud)
这是标准的使用模式:
cd path/to/parent/repo
# Do a dry-run to see which repos will be temporarily disabled
git disable-repos --true_dryrun
# Now actually disable them: disable all git repos in this dir and below
git disable-repos --true
# re-enable just the parent repo
mv ..git .git
# quit tracking the subrepo as a single file (required
# if you previously tried to add it to your main repo before
# disabling it as a git repo)
git rm --cached path/to/subrepo
# add all files, including the now-disabled sub-repos, to the parent repo
git add -A
# commit all files
git commit
Run Code Online (Sandbox Code Playgroud)
这会将所有子存储库,包括它们的(现在..git).git 文件夹和所有 git 工件,作为常规文件提交到父 git 存储库。您拥有 100% 的控制权!只想更新 1 个子仓库?然后 cd 进入它并手动将其一个..git文件夹重命名回.git,然后像往常一样使用该子存储库,然后在完成后git disable-repos --true再次运行(或手动从.gitback重命名为..git),并将其提交到父存储库中。我的git disable-repos脚本的美妙之处在于,它可以在必要时快速且无缝地禁用或启用 100 个子存储库,而手动执行此操作是不切实际的。
也许我的用例很奇怪:我只需要将大量内容提交到一个 repo 中,直到我可以在以后单独清理和分离每个 subrepo,但它可以完成我需要它做的事情。
这里是完整的帮助菜单输出的git disable-repos -h:
$ git disable-repos -h
'git disable-repos' version 0.3.0
- Rename all ".git" subdirectories in the current directory to "..git" to temporarily
"disable" them so that they can be easily added to a parent git repo as if they weren't
git repos themselves (".git" <--> "..git").
- Why? See my StackOverflow answer here: /sf/answers/4365789081/
- See also the "Long Description" below.
- NB: if your sub-repo's dir is already being tracked in your git repo, accidentally, stop
tracking it with this cmd: 'git rm --cached path/to/subrepo' in order to be able to
start tracking it again fully, as a normal directory, after disabling it as a sub-repo
with this script. To view all tracked files in your repo, use 'git ls-files'.
- References:
1. /sf/ask/89184021/#1274447
2. /sf/ask/1918229491/#27416839
3. /sf/ask/597324171/#14406253
Usage: 'git disable-repos [positional_parameters]'
Positional Parameters:
'-h' OR '-?' = print this help menu, piped to the 'less' page viewer
'-v' OR '--version' = print the author and version
'--true' = Disable all repos by renaming all ".git" subdirectories --> "..git"
So, once you do 'git disable-repos --true' **from within the parent repo's root directory,**
you can then do 'mv ..git .git && git add -A' to re-enable the parent repo ONLY and
stage all files and folders to be added to it. Then, run 'git commit' to commit them.
Prior to running 'git disable-repos --true', git would not have allowed adding all
subdirectories since it won't normally let you add sub-repos to a repo, and it recognizes
sub-repos by the existence of their ".git" directories.
'--true_dryrun' = dry run of the above
'--false' = Re-enable all repos by renaming all "..git" subdirectories --> ".git"
'--false_dryrun' = dry run of the above
'--list' = list all ".git" and "..git" subdirectories
Common Usage Examples:
1. To rename all '.git' subdirectories to '..git' **except for** the one immediately in the current
directory, so as to not disable the parent repo's .git dir (assuming you are in the parent
repo's root dir when running this command), run this:
git disable-repos --true # disable all git repos in this dir and below
mv ..git .git # re-enable just the parent repo
Be sure to do a dry run first for safety, to ensure it will do what you expect:
git disable-repos --true_dryrun
2. To recursively list all git repos within a given folder, run this command from within the
folder of interest:
git disable-repos --list
3. Assuming you tried to add a sub-repo to your main git repo previously, BEFORE you deleted or
renamed the sub-repo's .git dir to disable the sub-repo, this is the process to disable
the sub-repo, remove it from your main repo's tracking index, and now re-add it to your
main repo as a regular directory, including all of its sub-files and things:
Description: remove sub-repo as a sub-repo, add it as a normal directory, and commit
all of its files to your main repo:
Minimum Set of Commands (just gets the job done without printing extra info.):
git disable-repos --true # disable all repos in this dir and below
mv ..git .git # re-enable just the main repo
# quit tracking the subrepo as a single file
git rm --cached path/to/subrepo
# start tracking the subrepo as a normal folder
git add -A
git commit
Full Set of Commands (let's you see more info. during the process):
git disable-repos --true # disable all repos in this dir and below
mv ..git .git # re-enable just the main repo
git ls-files path/to/subrepo # see what is currently tracked in the subrepo dir
# quit tracking the subrepo as a single file
git rm --cached path/to/subrepo
git status
# start tracking the subrepo as a normal folder
git add -A
git status
git commit
Long Description:
I want to archive a bunch of small git repos inside a single, larger repo, which I will back up on
GitHub until I have time to manually pull out each small, nested repo into its own stand-alone
GitHub repo. To do this, however, 'git' in the outer, parent repo must NOT KNOW that the inner
git repos are git repos! The easiest way to do this is to just rename all inner, nested '.git'
folders to anything else, such as to '..git', so that git won't recognize them as stand-alone
repositories, and so that it will just treat their contents like any other normal directory
and allow you to back it all up! Thus, this project is born. It will allow you to quickly
toggle the naming of any folder from '.git' to '..git', or vice versa. Hence the name of this
project: git-disable-repos.
See my answer here:
/sf/ask/3290580331/#62368415
This program is part of: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
Run Code Online (Sandbox Code Playgroud)
对于任何寻求更“专业”解决方案的人来说,这些似乎是最流行的解决方案,按照最流行的(因此似乎是最受支持的?)
git submodule- https://git-scm.com/docs/git-submodule - 内置于git.git subtree- https://www.atlassian.com/git/tutorials/git-subtreegit subrepo- https://github.com/ingydotnet/git-subrepo其中哪一个是最好的?我不能说,但它们看起来都让我感到困惑,所以我选择了我上面描述的手动、蛮力选项,因为在这种情况下它最符合我的预期目的,直到我能找到时间打破每个子-repos 到他们自己在 GitHub 上单独维护的存储库。
git submodule:2020 年 9 月 21 日更新: Martin Owen 于 2016 年 5 月发表的这篇文章(“Git Submodules vs Git Subtrees”)包含了对git submodulevs的很好的比较git subtree,并且普遍支持git submodule. 然而,作者当时甚至不知道,git subrepo除了在评论中提到的时候,也没有提到它。
git submodule似乎是内置于git. 虽然看起来它确实有一个学习曲线,但我计划在我的下一个项目中使用它,现在我准备打开该项目并再次开始工作,它取决于 sub-git repos。我计划从这里开始学习:
git submodule文档在这里:https : //git-scm.com/book/en/v2/Git-Tools-Submodules关键词:git add subrepo;git 添加子仓库;git 添加嵌套存储库;git add .git 文件夹和文件
我删除了该特定文件夹中的 .git 。然后我运行命令后
git add folder_which_has_deleted_dot_git
git commit -m "Changed to standalone repo"
Run Code Online (Sandbox Code Playgroud)
然后我就能够跟踪该文件夹并将其转换为公共/独立存储库。
如果您不关心 BA 使用的确切版本,您可以保留当前设置(嵌套 git 存储库)。
您将收到“嵌入式存储库”警告,但除此之外,两个存储库都会按照您的预期运行,每个存储库仅添加、提交和推送其存储库。
注意:您可以使用以下命令将该警告短路/清空git config advice.addEmbeddedRepo
| 归档时间: |
|
| 查看次数: |
25202 次 |
| 最近记录: |