如何将主干/分支概念从 Subversion 转换为 Git?

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

How can I translate trunk/branch concepts from Subversion to Git?

gitbranchgithubversion-control

提问by M. Ryan

So I am not much of a source control expert, I've used Subversion for projects in the past. I have to use Git for a particular project (client supplied Git repo).

所以我不是一个源代码控制专家,我过去曾在项目中使用过 Subversion。我必须将 Git 用于特定项目(客户端提供的 Git 存储库)。

My workflow is as such that I will be working on the files from two different computers, and often I need to check in changes that are unstable when I move from place to place so I can continue my work. What then occurs is when, say, the client goes to get the latest version, they will also download the unstable code.

我的工作流程是这样的,我将处理来自两台不同计算机的文件,并且经常需要检查当我从一个地方移动到另一个地方时不稳定的更改,以便我可以继续我的工作。然后发生的是,当客户端去获取最新版本时,他们也会下载不稳定的代码。

In SVN, you can address this by creating a trunk and use working branches, or use the trunk as the working version and create stable branches.

在 SVN 中,您可以通过创建主干并使用工作分支来解决此问题,或者使用主干作为工作版本并创建稳定分支。

What is the equivalent concept in Git, and is there a simple way to do this via GitHub?

Git 中的等效概念是什么,是否有通过 GitHub 执行此操作的简单方法?

采纳答案by Evan Barkley

There are a lot of different ways to do this. If you have to move from computer to computer, you'll be switching to a different repository, so that means you'd be pushing up your changes to the remote repo. That's fine, but it also means you

有很多不同的方法可以做到这一点。如果您必须在计算机之间移动,您将切换到不同的存储库,这意味着您会将更改推送到远程存储库。那很好,但这也意味着你

A very simple example is to only perform your unstable work on a private branch, and to name it something obvious, e.g. unstable-development. Here's how to do this from scratch. First, let's make a new repo from your client's site, which I'll call "secret-sauce".

一个非常简单的例子是只在私有分支上执行不稳定的工作,并将其命名为明显的名称,例如unstable-development. 这是从头开始的方法。首先,让我们从您客户的站点创建一个新的存储库,我将其称为“secret-sauce”。

$ git clone git://example.com/repositories/secret-sauce.git

You're still on the masterbranch, the default. Let's make a new branch so that you can commit stuff there instead of on master.

master默认情况下,您仍在分支上。让我们创建一个新分支,以便您可以在那里提交内容而不是 on master

$ git branch unstable
$ git checkout unstable
Switched to branch 'unstable'

Okay. Now let's add some unstable code:

好的。现在让我们添加一些不稳定的代码:

$ touch kablammo.txt
$ git add *
$ git commit -m "Added unstable code."
[master (root-commit) 9428aef] Initial checkin.
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 kablammo.txt

Right now, unstableexists only on your side. Notice that when we cloned, we got a remote repository called origin, which has a corresponding masterbranch. When your local repository knows about a remote repository's branches, we call that a "tracking branch". You can see all your remote tracking branches with git branch -r:

现在,unstable只存在于你身边。请注意,当我们克隆时,我们得到了一个名为 的远程存储库origin,它有一个相应的master分支。当您的本地存储库知道远程存储库的分支时,我们将其称为“跟踪分支”。您可以使用以下命令查看所有远程跟踪分支git branch -r

$ git branch -r
  origin/HEAD -> origin/master
  origin/master

Okay. Let's push our changes back!

好的。让我们将更改推回去!

$ git push origin unstable

That's it -- our changes now live in the unstablebranch on the remote repo. If we want to see what people are up to on the masterbranch again, we can switch back again with git checkout master.

就是这样 - 我们的更改现在位于unstable远程仓库的分支中。如果我们想再次查看人们在master分支上的动态,我们可以使用git checkout master.

回答by dbyrne

I find this a valuable resource: A Successful Git Branching Model

我发现这是一个宝贵的资源:一个成功的 Git 分支模型

Unlike SVN, git is decentralized. You shouldn't ever need to push unstable code to your client's repository. You can simply pull the unstable code to your 2nd computer from the 1st.

与 SVN 不同,git 是去中心化的。您永远不需要将不稳定的代码推送到客户的存储库。您可以简单地将不稳定的代码从第一台计算机拉到您的第二台计算机上。

Having said that, git handles branching great and should be an integral part of whatever methodology you end up using.

话虽如此,git 可以很好地处理分支,并且应该成为您最终使用的任何方法的一个组成部分。