git 如何管理一个项目的多个版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12153405/
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 manage multiple versions of a project?
提问by Justin
I have a project that is open sourced using git
for revision control. Soon, I am going to release a premium hosted version of the project as well, but there will be changes made to the code specifically for the premium hosted version (performance, etc.), that should not exist in the open source version.
我有一个开源项目,git
用于版本控制。很快,我也将发布该项目的高级托管版本,但将对代码进行专门针对高级托管版本(性能等)的更改,这在开源版本中不应该存在。
I want to avoid having two separate directories/projects, because if I fix a bug in the open source version, that bug is most likely in the premium hosted version as well. I don't want to make changes in two places.
我想避免有两个单独的目录/项目,因为如果我修复开源版本中的错误,那么该错误很可能也出现在高级托管版本中。我不想在两个地方进行更改。
What is the best way to manage this? Simply branching in git
won't work right, because some files need to have 2 versions: an open source version and a premium hosted version.
管理这种情况的最佳方法是什么?简单地分支是git
行不通的,因为有些文件需要有 2 个版本:一个开源版本和一个高级托管版本。
采纳答案by VonC
And yet, branching should work, and will allow you to maintain two separate versions.
然而,分支应该有效,并且允许您维护两个单独的版本。
If you have a bug which applies to the premium version, fix it on master
, and merge it on premium
branch.
Git will only merge what has changed since you branched between master
and premium
, ie your bug fix.
On another way to publish an hotfix both in master
and premium
would be to do it from the common ancestor: see "Git merging hotfix to multiple branches".
如果您有适用于高级版本的错误,请在 上修复它master
,然后将其合并到premium
分支上。
Git 只会合并自从你在master
和之间分支后发生的变化premium
,即你的错误修复。
在另一种方式来发布的修补程序都在master
和premium
将是从共同的祖先做:看“混帐合并修补程序,以多个分支”。
Update 2015: git 2.5 (July 2015) has replaced git-new-workdir
presented below by the new command git worktree add <path> [<branch>]
.
2015 年更新:git 2.5(2015 年 7 月)已用git-new-workdir
新命令取代了下面的内容git worktree add <path> [<branch>]
。
For more, see "Multiple working directories with Git?".
有关更多信息,请参阅“使用 Git 设置多个工作目录?”。
Original answer 2012:
2012 年的原始答案:
me-andmentions in the commentsthe command git-new-workdir.
See:
me-and在评论中提到命令 git-new-workdir。
看:
One solution to this is to simply create another local clone of your repository. Git automatically uses hard links when you clone locally, so cloning is very fast.
But there is one problem with this: You now have another, separate repository you need to keep up to date.This is where
git-new-workdir
comes in.
Instead of doing a full-blown clone of your repository, it simply sets up a new working directory (with its own index) for you.
The actual repository itself is shared between the original and the new working directory. This means:
- If you update one repository, the new commits are instantly visible in all other working directories as well.
- Create a new commit or branch in one of your working directories, they're instantly available in all working directories.
Note: Even though the commits are automatically there, Git won't update the working copy if you've got the same branch checked out. You'll have to do that for yourself.
对此的一种解决方案是简单地创建存储库的另一个本地克隆。Git 在本地克隆时会自动使用硬链接,因此克隆速度非常快。
但这有一个问题:您现在有另一个独立的存储库,您需要保持最新状态。这就是
git-new-workdir
切入点。它
不是对您的存储库进行完整的克隆,而是简单地为您设置一个新的工作目录(带有自己的索引)。
实际存储库本身在原始工作目录和新工作目录之间共享。这意味着:
- 如果您更新一个存储库,新的提交也会立即在所有其他工作目录中可见。
- 在您的工作目录之一中创建新的提交或分支,它们在所有工作目录中立即可用。
注意:即使提交是自动存在的,如果您检出相同的分支,Git 也不会更新工作副本。你必须为自己做这件事。
回答by vaheeds
Use of worktree
is best for this purpose.
使用worktree
最适合此目的。
In my case, I have two version of the same software that the basics are the same but each version has some different features.
就我而言,我有两个版本的相同软件,它们的基础知识相同,但每个版本都有一些不同的功能。
So I create two worktree
that means, create two relevant long-running branches beside the master.
所以我创建了两个worktree
,这意味着在 master 旁边创建两个相关的长期运行的分支。
$git worktree add -b version-silver ..\version-silver master
$git worktree add -b version-gold ..\version-gold master
Then I have:
然后我有:
$git branch
master # base stuff here
version-silver # some normal features
version-gold # some better features
There is one repository, but I have 3 separate folders beside each other for each branch above. And make the common changes in master. then merge it with both other versions.
有一个存储库,但对于上面的每个分支,我都有 3 个单独的文件夹。并在 master 中进行通用更改。然后将其与其他两个版本合并。
cd master
vim basic.cpp
git add .
git commit -m "my common edit on basic.cpp"
cd ..\version-silver
vim silver.cpp
git add .
git commit -m "my specific edit on silver.cpp"
git merge master # here i get the basic.cpp latest changes for silver project
cd ..\version-gold
git merge master # here i get the basic.cpp latest changes for gold project
Specific changes of each version will go in the corresponding folder as well, and the works on each project are isolated and IDE wouldn't be confused.
每个版本的具体变化也会放在对应的文件夹中,每个项目的作品都是独立的,IDE不会混淆。
Hope that helps.
希望有帮助。