欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

git 初始化git存储库_处理Git子存储库的简单方法

程序员文章站 2022-05-27 12:48:30
...

git 初始化git存储库

Warning: this post is old and might not reflect the current state of the art

警告:此帖子过时,可能无法反映当前的最新状态

Suppose in your project you have a folder that you want to manage on a separate Git repository.

假设您的项目中有一个要在单独的Git存储库中管理的文件夹。

Or, suppose you want to open-source part of your application, but you don’t want the burden of managing a separate folder, symlinks and separate Git pushes.

或者,假设您想开源应用程序的一部分,但是您不希望管理单独的文件夹,符号链接和单独的Git推送。

What can you do?

你能做什么?

输入子树 (Enter subtrees)

First, create a new repository that will host the files you need. If using GitHub, initialize it with a README.md file so it will automatically create the master branch.

首先,创建一个新的存储库,该存储库将托管您需要的文件。 如果使用GitHub,请使用README.md文件对其进行初始化,以便它将自动创建master分支。

Next, add it as a remote inside your project.

接下来,将其添加为项目中的远程对象。

$ git remote add repository-name [email protected]:yourname/repository-name.git
$ git subtree add --prefix=subfolder/path/you/want repository-name master

Change repository-name, subfolder/path/you/want and yourname with your chosen names.

更改repository-namesubfolder/path/you/wantyourname与您所选择的名称。

You can add --squash to the last command if the project want to incorporate has already many commits, and you want to pull all changes in one single commit.

如果要合并的项目已经有很多提交,并且您想一次提交所有更改,则可以在最后一个命令中添加--squash

You’re done!

你完成了!

推拉 (Pushing and pulling)

Now, suppose someone pushes a PR on that subrepository, or you edit it from another project.

现在,假设有人在该子存储库上推送了PR,或者您从另一个项目中对其进行了编辑。

To incorporate the changes inside your project, do:

要将更改合并到项目中,请执行以下操作:

git subtree pull --prefix=subfolder/path/you/want repository-name master

You can add --squash if you want to incorporate all changes in one single commit.

如果要将所有更改合并到一个提交中,则可以添加--squash

Now you’ll push to your own project, and the changes made in the subrepository are stored in the Git history of it.

现在,您将进入自己的项目,并且在子存储库中所做的更改将存储在该项目的Git历史记录中。

To also push those changes to the subrepository Git history, and to the remote, just do

要将这些更改推送到子存储库Git历史记录和远程设备上,只需执行

git subtree push --prefix=subfolder/path/you/want repository-name master

Again, use --squash if you feel it appropriate. Git will cherry-pick the commits that involve the subrepository subdirectory, so you’ll just see those changes in the subrepository history.

同样,如果您认为合适,请使用--squash 。 Git会挑选涉及子存储库子目录的提交,因此您只会在子存储库历史记录中看到这些更改。

You might want to be careful and differentiate the commits that involve the subrepository. In this way you’ll keep things separated.

您可能需要小心并区分涉及子存储库的提交。 这样,您就可以将事情分开。

翻译自: https://flaviocopes.com/how-to-handle-git-subrepositories/

git 初始化git存储库