如何在几个分支上同时工作 [英] How to work simultaneously on a few branches

查看:88
本文介绍了如何在几个分支上同时工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此问题的后续行动创建分支。



它让我感到奇怪,因为我的本地机器上的文件将是一个奇怪的混合不同的实验。 p>

我会想象最佳实践方法是复制存储库并在我的计算机上为每个分支工作在不同的文件夹中 - 但我不知道如何设置。我在Documents / San / CompProj中拥有当前的存储库,那么我会用什么命令创建一个绑定到另一个本地文件夹上不同分支的新存储库?



Git对我来说是相当新颖的,所以我很乐意在上面提到的问题上对所做的任何更正。

解决方案

<从Git 2.5开始, git-worktree 直接支持这个工作流程。有关详细信息,请参见 VonC对此问题的回答



我的回答如果你不喜欢 git-worktree 出于任何原因可能就足够了。






Git旨在让您在磁盘上的单个文件夹中工作。这是一个包含您关心的所有分支机构的单一存储库。您签出您当时想要处理的分支。



在Git仓库中,一次只能检出一个分支。如果您签出第二个分支,则磁盘上的文件将被删除,并替换为第二个分支中的文件。



如果您有以下分支:

  BRANCH-A BRANCH-B 
alpha.txt alpha.txt
bravo.txt
charlie.txt charlie .txt
delta.txt

当您在 branch-A 结帐 branch-B ,那么 bravo.txt 将被移除, delta.txt 将被添加到你的工作目录中。



然而, git-checkout will not 覆盖您对文件所做的更改,除非您提供 -f 参数。如果您对 alpha.txt 进行了更改,然后尝试切换到 branch-B ,您将收到一条消息,警告您更改会丢失并中止结帐。



例外情况是未跟踪的文件。如果您有 branch-A 签出并且创建了一个名为 echo.txt 的新文件,那么当您签出时,Git将不会触及此文件>分支-B 。通过这种方式,您可以决定是否针对 branch-B 提交 echo.txt ,而不必经历(1)移动的麻烦(2)检出正确的分支,(3)将文件移回回购站。






< h3>脚注

实际上,Git不会强制您使用单个工作目录。如果你愿意,没有什么会阻止你为每个想要在其上工作的分支在磁盘上创建不同的路径。

  / home / me / project 
+ - branch-a /
+ - branch-b /
+ - ...

这些路径中的每一个都是它自己的Git仓库(每个仓库都有一个 .git 文件夹),您可以

  cd〜/ project ##转到我的项目目录
git clone分支-a branch-b ##创建一个新分支-b

cd分支-b
...工作工作...
git commit -a -mMade分支-b上的一些变化

git pull origin ##获取并合并来自分支的更改-a
git push origin ##将我的更改推回到分支-a

这是一些人如果不使用 name d分支:他们将存储库克隆到磁盘上的一个新目录中,然后在它们之间推送并拉取变更集。


This is a follow-up on this question on 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?

Git is fairly new to me so I'd love any corrections you can make on what I'm assuming/asking above.

解决方案

As of Git 2.5, git-worktree directly supports this workflow. See VonC's answer to this question for details.

My answer below may suffice if you don't like git-worktree for whatever reason.


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.

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.

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-A and you checkout branch-B, then bravo.txt will be removed and delta.txt will be added to your working directory.

However, git-checkout will not overwrite 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.

The exceptions are untracked files. If you have branch-A checked 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-B without 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.


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.

/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.

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.

这篇关于如何在几个分支上同时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆