Git - 将分支存储在单独的本地目录中

发布于 2024-10-19 00:06:51 字数 514 浏览 2 评论 0原文

我对 git 相当陌生,正在尝试以正确的方式设置我的存储库。

基本上我的应用程序是一个平台,因此该平台的实现基于主分支,但对这些文件以及一些附加文件进行了一些小的修改。

我尝试将其设置为分支,因此我有一个 master 分支、implementation_1implementation_2

但据我所知,这意味着本地所有分支都存储在一个目录中,它们的分离只能通过 git 进行。

我想要的是有 3 个本地目录,masterimp_1imp_2。如果我对 imp_1 目录中的核心文件之一进行更改,我希望能够将该更改合并到 master 分支中,并从那里合并到 imp_2

我开始认为这些需要是 3 个不同的存储库(实现是核心的分支)。这是要走的路吗?那么,我该如何处理上述情况呢?

I'm rather new to git, and am trying to set up my repository the right way.

Basically my app is a platform of sorts, so implementations of this platform are based on the master branch, but have some small modifications to those files as well as some additional files.

I tried setting it up as branches, so I have a master branch, implementation_1 and implementation_2.

But as far as I can tell, that would mean that locally all the branches are stored in one directory, with their separation being only through git.

What I would like is to have 3 local directories, master,imp_1, and imp_2. If I make a change to one of the core files in the imp_1 directory, I want to be able to merge that change into the master branch and from there into imp_2.

I'm beginning to think these need to be 3 different repositories (the implementations being forks of the core). Is that the way to go? In that case, how would I go about handling the above scenario?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朱染 2024-10-26 00:06:51

分支是 Git 中的一等公民。它们不会像旧版 VCS(例如 SVN、CVS 等)那样“模拟”为分支。

如果您确实需要三个不同的目录,因为您想要拥有三个不同的开发环境,请创建 3 个克隆:

  • 一个位于名为 master 的目录中
  • 一个位于名为 imp_1 的目录中
  • 一个位于名为 imp_2 的目录中

但要真正理解分支,您可以阅读“DVCS 中不同分支模型的优缺点”。


或者,从 Git 2.5 开始(2015 年 7 月,OP 提出问题 4 年后),正如我在 Git 的多个工作目录?,使用 git worktree

这将是一个克隆,多个文件夹(每个分支一个)。
使用 Git 2.7+,您可以列出这些文件夹:

$ git worktree list
/path/to/bare-source            (bare)
/path/to/linked-worktree        abcd1234 [master]
/path/to/other-linked-worktree  1234abc  (detached HEAD)

Branches are first class citizens in Git. They are not "emulated" as branches like in older VCS such SVN, CVS, etc.

If you really need three different directories, because you want to have three distinct development environment, make 3 clones:

  • one in a directory called master
  • one in a directory called imp_1
  • one in a directory called imp_2

But to really understand branches, you can read "Pros and cons of different branching models in DVCS".


Or, since Git 2.5 (July 2015, 4 years after the OP's question), as I detailed in Multiple working directories with Git?, use git worktree.

That will be one clone, multiple folders (one per branch).
With Git 2.7+, you can then list those folders:

$ git worktree list
/path/to/bare-source            (bare)
/path/to/linked-worktree        abcd1234 [master]
/path/to/other-linked-worktree  1234abc  (detached HEAD)
美煞众生 2024-10-26 00:06:51

您可以创建存储库的三个克隆并在它们之间进行合并。 git 中的分叉与分支的合并方式等没有什么不同。你可以轻松做到:

git merge ../master
git pull ../imp_2

本地克隆时,git 会硬链接你的对象,甚至节省磁盘空间

you can create three clones of your repository and merge between those. forks in git are no different from branches the way they are merged and such. you can easily do:

git merge ../master
git pull ../imp_2

when cloning locally git will hardlink your objects and even save disk space

睫毛溺水了 2024-10-26 00:06:51

您可以使用 git worktree 来完成此操作,该工作树是 于 2015 年 7 月左右在 Git 2.5 中引入

git clone -b master <repo> master
cd master

# checking out implementation_1 in ../imp_1
git worktree add ../imp_1 implementation_1

# creating branch implementation_2 at master~2 as you check it out in ../imp2:
git worktree add -b implementation_2 ../imp_2 master~2

瞧!你完成了。

通过此设置,您将只有一个 .git 文件夹(位于 master/.git 中)。请注意,一次可以在单个工作树中签出任何分支。

You can do this using git worktree that was introduced in Git 2.5, circa July 2015.

git clone -b master <repo> master
cd master

# checking out implementation_1 in ../imp_1
git worktree add ../imp_1 implementation_1

# creating branch implementation_2 at master~2 as you check it out in ../imp2:
git worktree add -b implementation_2 ../imp_2 master~2

And voilà! You're done.

With this setup, you'll have only one .git folder (in master/.git). Note that any branch can be checked out in a single worktree at a time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文