Git:创建新分支并推送到远程的有效步骤

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

Git: Efficient steps to create a new branch and push to remote

gitbitbucket

提问by Mike

I figured out the steps but seems cumbersome, take bitbucketfor example, suppose i already have a project called prj

我想通了步骤,但似乎很麻烦,bitbucket举个例子,假设我已经有一个名为prj

  1. I branch a new project from server side(bitbucket.com), called prj-bz
  2. From local i add add a remote git remote add prj-bz https://blah...
  3. At the same time from local i create a new branch called prj-bz
  4. From local i call git push prj-bz prj-bzto let local repo and remote one connected.
  1. 我从服务器端(bitbucket.com)分支一个新项目,称为 prj-bz
  2. 从本地我添加添加一个遥控器 git remote add prj-bz https://blah...
  3. 同时从本地我创建一个名为的新分支 prj-bz
  4. 从本地我打电话git push prj-bz prj-bz让本地 repo 和远程一个连接。

I checked out some git books but seem none cover this. Any more efficient way to do this?

我查看了一些 git 书籍,但似乎没有涵盖这一点。有没有更有效的方法来做到这一点?

回答by Marcus

Generally, people usually do one or the other Forkor Branch. It sounds like you are making a Fork of a repo, then making a branch in the fork with the same name. If you're using a Pull Request to put data back in to the main repo, you don't need to do both. Pick one of the two workflows:

一般来说,人们通常会做一个或另一个ForkBranch。听起来您正在制作一个 repo 的 Fork,然后在 fork 中创建一个具有相同名称的分支。如果您使用拉取请求将数据放回主存储库,则无需同时执行这两项操作。选择以下两个工作流程之一:

  • Fork the repo on Bitbucket (or other site)
  • Clone the repo git clone https://bitbucket.org/username/repo-fork.git
  • Work in that fork git commit -m "some work done", git push -u origin master
  • Create a Pull request to request your changes to be placed back into the parent of the fork
  • 在 Bitbucket(或其他站点)上分叉 repo
  • 克隆回购 git clone https://bitbucket.org/username/repo-fork.git
  • 在那个叉子里工作git commit -m "some work done"git push -u origin master
  • 创建拉取请求以请求将您的更改放回分叉的父级

OR

或者

  • Clone the main repo git clone https://bitbucket.org/username/repo-fork.git
  • Create a new local branch git checkout -b my-branch
  • Work in that branch git commit -m "some work done"
  • Push up the branchgit push -u origin my-branch
  • Create a Pull request
  • 克隆主仓库 git clone https://bitbucket.org/username/repo-fork.git
  • 创建一个新的本地分支 git checkout -b my-branch
  • 在那个分支工作 git commit -m "some work done"
  • 推高树枝git push -u origin my-branch
  • 创建拉取请求

With the branch method, I'm assuming you have rights to write to the main repo. If not, you'll want to stick to the fork method. There are more workflows out there too. Bitbucket also has a doc explaining thisas well as one on Atlassian's websitewith a bit more depth on Git workflows.

使用分支方法,我假设您有权写入主存储库。如果没有,您将要坚持使用 fork 方法。还有更多的工作流程。Bitbucket 也有一个文档解释了这一点,以及在Atlassian 的网站上有一个关于 Git 工作流程的更深入的文档

回答by Faisal Sheikh

For creating new branch we will use: (using this command A new branch will create and the branch status will also change with newly created branch)

对于创建新分支,我们将使用:(使用此命令将创建一个新分支,并且分支状态也会随着新创建的分支而改变)

git checkout -b branch-name

And for pushing the changes we can run following commands:

为了推送更改,我们可以运行以下命令:

git add . 
git commit -m "with meaningful comments" 
git push origin branch-name

回答by Michael L.

Well, if creating a new repo instead of a new branch in an existing one, you could just git clone https://blah <target folder>to replace steps 2-4.

好吧,如果在现有的仓库中创建一个新的仓库而不是一个新的分支,你可以只git clone https://blah <target folder>替换步骤 2-4。

If not, your only real alternative is creating a simple script that accepts remote name, branch name and git url as arguments and executes steps 2-4 with that information.

如果没有,您唯一真正的替代方法是创建一个简单的脚本,该脚本接受远程名称、分支名称和 git url 作为参数,并使用该信息执行步骤 2-4。