简体   繁体   English

Git-如何将本地存储库添加到新分支

[英]Git - How to add local repository to new Branch

I'm working on a project where we have reached a milestone. 我正在一个已经达到里程碑的项目中工作。 My git repository is up to date and everything is working correctly. 我的git存储库是最新的,一切正常。 Our client has now updated their api's and we're unsure how it will affect our code. 我们的客户现在已经更新了他们的api,我们不确定它将如何影响我们的代码。

What I want to do is create a branch (already done) and upload the full repository to the branch. 我要做的是创建一个分支(已完成)并将完整的存储库上载到该分支。 So I will have the project duplicated, one in the master and one in the new branch. 因此,我将复制该项目,一个在主数据库中,另一个在新分支中。 I will then work and commit to the new branch leaving the master untouched. 然后,我将工作并提交到新分支,而使主服务器保持不变。 So going forward then for each release I can do the same, so we can fall back to the previous branch if there's any problem. 因此,对于每个发行版,我都可以做同样的事情,因此,如果有任何问题,我们可以退回到上一个分支。

Can someone tell me how to do this please. 有人可以告诉我该怎么做。 I'm using Git and I'm working through a mac terminal. 我正在使用Git,并且正在通过Mac终端进行工作。

I've gone through relevant questions on SO but I don't see the info I require, or perhaps I've missed it. 我已经解决了有关SO的相关问题,但没有看到所需的信息,或者我错过了。

You don't upload a repository to a branch. 您不将存储库上载到分支。

You have your code in a repository, and the repository has branches. 您将代码存储在存储库中,并且该存储库具有分支。 No duplication, but you can get (checkout) master or the branch to set your working files to the version you want: master for untouched changes, branch for your recent changes. 没有重复,但是您可以获取(签出)master或分支以将工作文件设置为所需的版本:master保留未更改的更改,branch保留最近的更改。

Always good to do when you have reached a milestone is to tag the commit: 当您达到里程碑时,总是最好标记提交:

$ git tag v1.0

Then from there you can start a branch to work on integrating the new API 然后,您可以从那里开始一个分支,以集成新的API

$ git checkout -b client_api_update

Work and commit in your branch. 工作并提交您的分支。 It does not affect the content in master, which you can checkout when you want to get your working files as they were in your milestone version (or checkout the tag). 它不会影响master中的内容,您可以在要获取工作文件时将其签出,就像您在里程碑版本中一样(或签出标签)。

When the integration of the new API is done, it's time to merge your branch to master in order to release support of the new API: 完成新API的集成后,是时候将您的分支合并到master以便释放对新API的支持了:

$ git checkout master
$ git merge client_api_update --no-ff

--no-ff is optional, but I think it makes for a more elegant history. --no-ff是可选的,但我认为它使历史更加优美。

You are at a new milestone, so tag: 您正处于一个新的里程碑,因此标记:

$ git tag v1.1

Here is how your history would look like: 这是您的历史记录:

    /- v1.0            /- v1.1 
A---B------------------G - master
    \---C---D---E---F-/ - client_api_update

Let's say the branch you created in the main repo is <main_repo_new_branch> . 假设您在主<main_repo_new_branch>创建的分支是<main_repo_new_branch> Then the steps would be: 然后的步骤将是:

cd <local_repo_dir>
git remote add mainrepo <main_repo_dir>
git push mainrepo <local_repo_branch>:<main_repo_new_branch>
cd <main_repo_dir>
git checkout <main_repo_new_branch>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM