如何小心地推送到 Git 中的新分支

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

How to push to a new branch in Git carefully

gitgithubpushgit-remote

提问by GoZoner

I am currently working on a team project, with many remote branches. And I cloned the project from our master's branch. Now, that I have made changes to this project locally, I would like to push it, but in-order to avoid pushing it to the projects main remote branch, I would like to create a new branch locally, and push this branch to remote.

我目前正在处理一个团队项目,有许多远程分支。我从我们的主分支克隆了这个项目。现在,我在本地对该项目进行了更改,我想推送它,但是为了避免将其推送到项目主远程分支,我想在本地创建一个新分支,并将此分支推送到远程.

The overall idea, is not to smash my local version of the project into the main remote project. I just need my branch to be separate.

总体思路,不是将我的本地版本的项目粉碎到主要的远程项目中。我只需要我的分支是分开的。

So, I'm guessing I will first do

所以,我猜我会先做

git checkout -b my_new_branch

git checkout -b my_new_branch

then

然后

git push -u origin my_new_branch

git push -u origin my_new_branch

Will this guarantee, that my branch will not get mixed with anyone's?

这能保证我的分支不会与任何人的分支混在一起吗?

回答by GoZoner

The -uoption sets up the defaults in your repository to track my_new_branchon origin. Thus both pushand pullwill, by default, use originand my_new_branch. Thereafter you won't need to provide those arguments. Here is an example:

-u选项设置在你的仓库默认跟踪my_new_branchorigin。因此,默认情况下,pushpull将使用originmy_new_branch。此后,您将不需要提供这些参数。下面是一个例子:

$ mkdir foo; cd foo; git init; touch README; git add README; git commit -m 'README'
Initialized empty Git repository in /private/tmp/foo/.git/
[master (root-commit) 03f3d46] README
 0 files changed
 create mode 100644 README

$ cd ..; git clone foo bar; cd bar
Cloning into 'bar'...
done.

$ git checkout -b my-br
Switched to a new branch 'my-br'

$ touch INSTALL; git add INSTALL; git commit -m 'INSTALL'
[my-br 627d6db] INSTALL
 0 files changed
 create mode 100644 INSTALL

$ git push -u origin my-br
Counting objects: 3, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 245 bytes, done.
Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
To /tmp/foo
 * [new branch]      my-br -> my-br
Branch my-br set up to track remote branch my-br from origin.

$ touch Makefile; git add Makefile; git commit -m 'Makefile'
[my-br f2390c1] Makefile
 0 files changed
 create mode 100644 Makefile

$ git push
Counting objects: 3, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 255 bytes, done.
Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
To /tmp/foo
   627d6db..f2390c1  my-br -> my-br

Notice that the final git pushuses the proper defaults. The main remote repository is safe from your repository smashing it.

请注意,finalgit push使用了正确的默认值。 主远程存储库是安全的,不会被您的存储库破坏。

回答by Polostor

As others have pointed out. You are absolutely correct.

正如其他人所指出的那样。你是完全正确的。



Just to sum up all the details.

只是总结所有的细节。

So, I'm guessing I will first do

git checkout -b my_new_branch

所以,我猜我会先做

git checkout -b my_new_branch

That is correct approach to create new branchwith name my_new_branch. You are adding all of the changes that you have made in master branch to this new branch.

这是使用 name创建新分支的正确方法my_new_branch。您正在将在 master 分支中所做的所有更改添加到这个新分支。

then

git push -u origin my_new_branch

然后

git push -u origin my_new_branch

This then creates your new branch on originwhich is being followed with your branch. The -uis the same as --set-upstream. More info in amazing answer for why always git branch --set-upstream.

然后,这会在原点上创建您的新分支,分支将跟随您的分支。该-u是一样的--set-upstream。有关为什么总是 git branch --set-upstream 的惊人答案中的更多信息。

From now on you can use git pushand git pullon remote repository as you wish with your branch linked from local repository to remote repository.

从现在开始,您可以根据需要使用git pushgit pull远程存储库,并将分支从本地存储库链接到远程存储库

Will this guarantee, that my branch will not get mixed with anyone's?

这能保证我的分支不会与任何人的分支混在一起吗?

YES. Your master branch is absolutely safe from your push(es). And your branch is safe of mixed code, unless anyone else pushes to this branch. If this condition can be applied it's safe to say your code will not get mixed with anyone else's code.

是的。您的主分支绝对不会受到您的推送。并且您的分支对混合代码是安全的,除非其他人推送到该分支。如果可以应用此条件,可以肯定地说您的代码不会与任何其他人的代码混合

回答by Atropo

In your example your pushing a local branch to the remote repo. In this way the branch will be separated from the masterbranch.

在您的示例中,您将本地分支推送到远程仓库。这样,分支将与分支分离master

So unless other people voluntarilypushin your branch it will not get mixed with anyone's.

因此,除非其他人自愿push加入您的分支机构,否则它不会与任何人混在一起。