git 同时在两个分支上工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2048470/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:39:39  来源:igfitidea点击:

git working on two branches simultaneously

gitbranch

提问by name

I have a project with many branches.

我有一个有很多分支的项目。

I would like to work on severalbranches simultaneously without switching back and forth with git checkout.

我想同时在几个分支上工作,而不用来回切换git checkout

Is there any way I can do that besides copying the whole repository somewhere else?

除了将整个存储库复制到其他地方之外,我还有什么办法可以做到这一点?

采纳答案by VonC

Git 2.5+ (Q2 2015) will support that feature: Once you have cloned a git repo, you will be able to checkout multiple branches in different path with the new command git worktree add <path> [<branch>].

Git 2.5+(2015 年第二季度)将支持该功能:一旦您克隆了一个 git 存储库,您将能够使用新命令检出不同路径中的多个分支git worktree add <path> [<branch>]

That replaces an older script contrib/workdir/git-new-workdir, with a more robust mechanism where those "linked" working trees are actually recorded in the main repo new $GIT_DIR/worktreesfolder (so that work on any OS, including Windows).

这用contrib/workdir/git-new-workdir更强大的机制替换了旧脚本,其中那些“链接”的工作树实际上记录在主 repo 新$GIT_DIR/worktrees文件夹中(以便在任何操作系统上工作,包括 Windows)。

Again, once you have cloned a repo (in a folder like /path/to/myrepo), you can add worktrees for different branches in differentindependent paths (/path/to/br1, /path/to/br2), while having those working trees linked to the main repo history (no need to use a --git-diroption anymore)

同样,一旦你克隆了一个/path/to/myrepo仓库(在一个文件夹中),你可以在不同的独立路径(/path/to/br1/path/to/br2)中为不同的分支添加工作树,同时将这些工作树链接到主仓库历史(不再需要使用--git-dir选项)

See more at "Multiple working directories with Git?".

请参阅“使用 Git 设置多个工作目录?”。

And once you have created a worktree, you can move or remove it(with Git 2.17+, Q2 2018).

创建工作树后,您可以移动或删除它(使用 Git 2.17+,2018 年第二季度)。

回答by Stefan N?we

Take a look at $GIT_SRC_DIR/contrib/workdir/git-new-workdir.

看看$GIT_SRC_DIR/contrib/workdir/git-new-workdir

a simple script to create a working directory that uses symlinks to point at an exisiting repository. This allows having different branches in different working directories but all from the same repository.

一个简单的脚本来创建一个使用符号链接指向现有存储库的工作目录。这允许在不同的工作目录中有不同的分支,但都来自同一个存储库。

回答by Konstantine Rybnikov

I suggest my small script http://www.redhotchilipython.com/en_posts/2013-02-01-clone-per-feature.html

我建议我的小脚本http://www.redhotchilipython.com/en_posts/2013-02-01-clone-per-feature.html

It will do git clone and replace the config (to "look" at original repo, so pull/push will go into "main" repo) basically, but it's simple enough to serve an abstraction from actual bootstrapping.

它将执行 git clone 并替换配置(以“查看”原始存储库,因此拉/推将进入“主”存储库),但它足够简单,可以从实际引导中提供抽象。

回答by Mostafa

Git supports multiple worktree at the same time. For more information see:

Git 同时支持多个工作树。有关更多信息,请参阅:

How ever it is very hard to support multiple worktree with IDs. For example this is an enhancement requestin JGet (eclipse ID) to support worktree.

如何使用 ID 支持多个工作树是非常困难的。例如,这是JGet(eclipse ID)中支持工作树的增强请求

So, you have to manage project manually (command line) with lots of problems or work with a single worktree in an IDE.

因此,您必须手动(命令行)管理项目并遇到很多问题,或者在 IDE 中使用单个工作树。

回答by Mostafa

Like our friend VonCsaid five months ago, now there is a new feature since version 2.5.x that does the job. git worktree.

就像我们的朋友VonC五个月前所说的那样,现在有一个自 2.5.x 版以来的新功能可以完成这项工作。git 工作树

回答by BastiBen

Not really as Git only supports to have oneworking copy of the repository data within the repository directory.

并不是真的,因为 Git 只支持在存储库目录中拥有一个存储库数据的工作副本。

If you want to commit/pull to the same repository with two different working copies, you could create a barerepository and clone it to two working copies.

如果您想使用两个不同的工作副本提交/拉取到同一个存储库,您可以创建一个存储库并将其克隆到两个工作副本

Whenever you have finished something, you simply push to the "main"bare repository.

每当您完成某事时,您只需推送到“主”裸存储库即可。

Some hints:

一些提示:

man git-clone

man git-clone

git clone --bare

git clone --bare