是否可以同时查看同一个项目的多个 git 分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7393274/
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
Is it possible to view multiple git branches at the same time for the same project?
提问by James Raitsev
I have 2 branches, which are not ready to be merged yet, but have some complementary logic, which I'd like to review (before merging)
我有 2 个分支,还没有准备好合并,但有一些互补的逻辑,我想回顾一下(合并前)
Can I check out multiple git branches of the same project? Is it possible?
我可以查看同一个项目的多个 git 分支吗?是否可以?
采纳答案by meagar
You can simply copy the repository to a new location (either by literally copying the directory, or using git clone --shared
) and check out one branch per location.
您可以简单地将存储库复制到新位置(通过逐字复制目录或使用git clone --shared
)并检查每个位置的一个分支。
You can also use git-worktree
for creating multiple working directories from a single instance of a repository.
您还可以git-worktree
用于从存储库的单个实例创建多个工作目录。
Otherwise, the primary means for comparing files between branches prior to merging them is git diff
.
否则,在合并之前在分支之间比较文件的主要方法是git diff
.
回答by VonC
With Git 2.5+ (Q2 2015), a Git repo will support multiple working treeswith git worktree add <path>
(and that will replace contrib/workdir/git-new-workdir
)
使用Git 2.5+(Q2 2015),一个Git回购将支持多个工作树与git worktree add <path>
(这将取代contrib/workdir/git-new-workdir
)
Those "linked" working trees are actually recorded in the main repo new $GIT_DIR/worktrees
folder (so that work on any OS, including Windows).
这些“链接”的工作树实际上记录在主 repo 新$GIT_DIR/worktrees
文件夹中(以便在任何操作系统上工作,包括 Windows)。
See more at "Multiple working directories with Git?"
请参阅“使用 Git 设置多个工作目录?”
回答by Philip Oakley
Yes it is possible with appropriate care. However you are taking one of the copies 'away' from the regular git directory using --work-tree=<path>
option, so changes there won't be seen by git unless you specially tell it. I gave an example here single-working-branch-with-git- see the UPDATED segment.
是的,在适当的照顾下是可能的。但是,您正在使用--work-tree=<path>
选项从常规 git 目录中“远离”一份副本,因此除非您特别告诉它,否则 git 不会看到那里的更改。我在这里举了一个例子single-working-branch-with-git- 请参阅 UPDATED 部分。
Note that the git-new-workdir
doesn't work on Windows XP as it requires Unix style links.
请注意,git-new-workdir
它不适用于 Windows XP,因为它需要 Unix 样式的链接。
回答by Munhitsu
First thing that comes to my mind it to checkout each branch on separate project. So: 1. checkout branch A on primary clone (1) 2. create a new clone (2) 3. checkout branch B in clone 2
我想到的第一件事就是在单独的项目中检查每个分支。所以: 1. 在主克隆 (1) 上签出分支 A 2. 创建一个新克隆 (2) 3. 在克隆 2 中签出分支 B
Second approach could be to create a new branch (aka C) and merge both branch A and B to it. If they are complimentary than this might help with your review.
第二种方法可能是创建一个新分支(又名 C)并将分支 A 和 B 合并到它。如果它们是免费的,那么这可能有助于您的评论。
回答by memeplex
Now git
includes the command worktree
to do exactly that.
现在git
包括执行此操作的命令worktree
。
回答by kto
As already mentioned, you can diff branches with git diff:
如前所述,您可以使用 git diff 区分分支:
git diff [--options] <commit> [--] [<path>…]
This form is to view the changes you have in your working tree relative to the named <commit>. You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch.
Excerpt above is from Git documentation.
以上摘录来自 Git 文档。