git 如何同时在几个分支上工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8470360/
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
How to work simultaneously on a few branches
提问by sscirrus
This is a follow-up on this questionon creating branches.
这是关于创建分支的这个问题的后续行动。
It strikes me as odd that I would still work on one repository because the files on my local machine will be a weird mix of different experiments.
让我感到奇怪的是我仍然会在一个存储库上工作,因为我本地机器上的文件将是不同实验的奇怪组合。
I would imagine the best-practice method is to duplicate the repository and work in different folders on my computer for each branch -- but I don't know how to set this up. I have my current repository at Documents/San/CompProj so what are the commands I'd use to create a new repository tied to a different branch on a different local folder?
我想最佳实践方法是复制存储库并在我计算机上的每个分支的不同文件夹中工作 - 但我不知道如何设置。我的当前存储库位于 Documents/San/CompProj,那么我将使用哪些命令来创建与不同本地文件夹上的不同分支相关联的新存储库?
Git is fairly new to me so I'd love any corrections you can make on what I'm assuming/asking above.
Git 对我来说是相当新的,所以我希望你能对我在上面假设/询问的内容进行任何更正。
采纳答案by Stephen Jennings
As of Git 2.5, git-worktree
directly supports this workflow. See VonC's answer to this questionfor details.
从 Git 2.5 开始,git-worktree
直接支持此工作流。有关详细信息,请参阅VonC 对此问题的回答。
My answer below may suffice if you don't like git-worktree
for whatever reason.
如果您git-worktree
出于任何原因不喜欢,我下面的回答可能就足够了。
Git is designed to allow you to work within a single folder on disk. This is a single repository that contains all the branches you care about.†You checkout whichever branch you want to work on at the time.
Git 旨在允许您在磁盘上的单个文件夹中工作。这是一个包含您关心的所有分支的单一存储库。†您可以签出当时想要处理的任何分支。
Within a Git repository, you can only have a single branch checked out at a time. If you check out a second branch, the files on disk are removed and replaced with those from the second branch.
在 Git 存储库中,您一次只能签出一个分支。如果您检出第二个分支,磁盘上的文件将被删除并替换为来自第二个分支的文件。
If you have the following branches:
如果您有以下分支:
BRANCH-A BRANCH-B
alpha.txt alpha.txt
bravo.txt
charlie.txt charlie.txt
delta.txt
When you're on branch-Aand you checkout branch-B, then bravo.txt
will be removed and delta.txt
will be added to your working directory.
当您在branch-A并且您结帐branch-B 时,bravo.txt
则将被删除并delta.txt
添加到您的工作目录中。
However, git-checkout
will notoverwrite changes you've made to files unless you supply the -f
argument. If you make a change to alpha.txt
then try to switch to branch-B, you'll get a message warning you that your changes would be lost and aborts the checkout.
但是,除非您提供参数,git-checkout
否则不会覆盖您对文件所做的更改-f
。如果您进行更改alpha.txt
然后尝试切换到branch-B,您将收到一条消息警告您所做的更改将丢失并中止结帐。
The exceptions are untracked files. If you have branch-Achecked out and you create a new file called echo.txt
, Git will not touch this file when you checkout branch-B. This way, you can decide that you want to commit echo.txt
against branch-Bwithout having to go through the hassle of (1) move the file outside the repo, (2) checkout the correct branch, and (3) move the file back into the repo.
例外是未跟踪的文件。如果您已检出branch-A并创建了一个名为 的新文件echo.txt
,则当您检出branch-B时,Git 将不会触及该文件。这样,您就可以决定要echo.txt
针对分支 B提交,而不必经历以下麻烦:(1) 将文件移出存储库,(2) 签出正确的分支,以及 (3) 将文件移回回购。
Footnote
脚注
†Actually, Git doesn't force you to use a single working directory. If you want, nothing is stopping you from creating different paths on disk for each branch you want to work on.
†实际上,Git 不会强迫您使用单个工作目录。如果您愿意,没有什么可以阻止您为要处理的每个分支在磁盘上创建不同的路径。
/home/me/project
+-- branch-a/
+-- branch-b/
+-- ...
Each of these paths is its own Git repository (each one has a .git
folder inside), and you can push and pull commits between the repos.
这些路径中的每一个都是自己的 Git 存储库(每个路径.git
内部都有一个文件夹),您可以在存储库之间推送和拉取提交。
cd ~/project ## Go to my projects directory
git clone branch-a branch-b ## Create a new branch-b
cd branch-b
... work work work ...
git commit -a -m "Made some changes on branch-b"
git pull origin ## Fetch and merge the changes from branch-a
git push origin ## Push my changes back to branch-a
This is how some people use Mercurial if they aren't using named branches: they clone the repository into a new directory on disk for each branch they want, then push and pull changesets between them.
这就是有些人在不使用命名分支的情况下使用 Mercurial 的方式:他们将存储库克隆到磁盘上的每个他们想要的分支的新目录中,然后在它们之间推送和拉取变更集。
回答by VonC
With Git 2.5+ (Q2 2015), Git is no longer designed to work within a single folder (ie a single working tree)
使用 Git 2.5+(2015 年第二季度),Git 不再设计为在单个文件夹(即单个工作树)中工作
Git will support multiple working trees(for onecloned git repo) with the new command git worktree add <path> [<branch>]
.
Git 将使用新命令支持多个工作树(对于一个克隆的 git repo)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/worktrees
folder (so that work on any OS, including Windows).
这用contrib/workdir/git-new-workdir
更强大的机制替换了旧脚本,其中那些“链接”的工作树实际上记录在主 repo 新$GIT_DIR/worktrees
文件夹中(以便在任何操作系统上工作,包括 Windows)。
You will be able to checkout different branches in different path while being part of the same main cloned repo.
您将能够在不同路径中检出不同分支,同时成为同一个主克隆 repo 的一部分。
See more at "Multiple working directories with Git?"
请参阅“使用 Git 设置多个工作目录?”
回答by I82Much
Your concern about 'the files on my local machine will be a weird mix of different experiments.' is unfounded - if you have branch 2 checked out, you will not see the files of branch 1 at the same time.
您担心“我本地机器上的文件将是不同实验的奇怪组合。” 是没有根据的 - 如果您签出分支 2,您将不会同时看到分支 1 的文件。
I would do something like
我会做类似的事情
# on master branch
git checkout master
# Create a branch for feature 1
git checkout -b feature_1
# work on feature 1
# Start a new feature branch
git checkout master
git checkout -b feature_2
# work on feature 2
# feature 2 finished and committed, time to merge
git checkout master
git merge feature_2
# update feature_1 branch
git checkout feature_1
git merge master
回答by Konstantine Rybnikov
I suggest my solution via small script https://web.archive.org/web/20141114201917/http://www.redhotchilipython.com/en_posts/2013-02-01-clone-per-feature.html
我通过小脚本建议我的解决方案https://web.archive.org/web/20141114201917/http://www.redhotchilipython.com/en_posts/2013-02-01-clone-per-feature.html
While git stash tools are nice, I find having multiple clones much better. You can run tests in one branch, while work on other. Or you don't even have to stop debugger, close editor, clean tempfiles or something else.
虽然 git stash 工具很好,但我发现有多个克隆要好得多。您可以在一个分支上运行测试,而在另一个分支上工作。或者您甚至不必停止调试器、关闭编辑器、清理临时文件或其他东西。