事后创建 git 分支?

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

creating git branch after the fact?

gitversion-controlgithubbranch

提问by Herbert Sitz

I suspect the answer to my problem is fairly simple, but I'm a git newbie and my head is getting confused at reading answers to all the similar SoF questions that don't seem to be quite my problem.

我怀疑我的问题的答案相当简单,但我是一个 git 新手,我的头脑在阅读所有类似 SoF 问题的答案时感到困惑,这些问题似乎不是我的问题。

Here's my problem:

这是我的问题:

I have a repo at github with a single branch ('master') that I've been working on in my local repo. At some point I stopped pushing commits back up to master on github because I was worried that they'd break things. So now I have lots of commits in my local repo that I want to push back up to github.

我在 github 有一个仓库,有一个分支('master'),我一直在我的本地仓库中工作。在某些时候,我停止将提交推送回 github 上的 master,因为我担心它们会破坏某些东西。所以现在我在我的本地存储库中有很多提交,我想推回 github。

However, rather than pushing back up to master I would prefer to create a new branch on github ('development') and push all my local commits back up to that branch (which I will merge back into master only after they've been better tested).

但是,我宁愿在 github 上创建一个新分支('development')并将我所有的本地提交推回该分支(只有在它们变得更好之后我才会合并回 master),而不是推回 master测试)。

What is the simple way to do this?

这样做的简单方法是什么?

回答by Grant Limberg

on master:

在主人上:

git checkout -b newbranchor as suggested below, simply do a git branch newbranchto create the new branch without switching to it.

git checkout -b newbranch或者按照下面的建议,只需执行 agit branch newbranch即可创建新分支,而无需切换到它。

This will create a branch that is at your current commit at master. Once done:

这将创建一个分支,该分支位于您当前在 master 上的提交。完成后:

git checkout master

git checkout master

followed by:

其次是:

git reset --hard <commit_hash>

git reset --hard <commit_hash>

Where <commit_hash>should be replaced by the commit ID you want master rolled back to.

Where<commit_hash>应该替换为您希望 master 回滚到的提交 ID。

Now you can switch to your new branch and push it out to the remote.

现在您可以切换到新分支并将其推送到远程。

git checkout newbranch
git push origin newbranch