从另一个分支还是从 master 创建 Git 分支? [英] Creating Git branch from another branch or from master?

查看:55
本文介绍了从另一个分支还是从 master 创建 Git 分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是 Git 的新手.我最近从 repo 中提取了一个新版本的 master 分支.

So I am new to Git. I recently pulled a fresh version of the master branch from the repo.

我为一个功能创建了一个 branch(1) 并将其推送到存储库并创建了一个拉取请求.现在我用另一个功能创建了一个新的 branch(2),但是由于我的拉取请求还没有合并,再次拉取 master 不会有我在 branch1.

I created a branch(1) for a feature and pushed it to the repo and created up a pull request. Now I created a new branch(2) with another feature, but since my pull request hasn't merged yet, pulling master again won't have that feature that I created in branch1.

只有当我从 branch1 创建一个新分支时,我才能获得该功能.之前,我已经从以前的分支创建了新分支,所以我没有遇到这个问题,但是有人告诉我,虽然这不是什么大问题,但他们希望我从主分支创建新分支.

Only when I create a new branch from branch1 I get that feature. Before, I've been created new branches from the previous branches so I haven't had this issue, but I've been told that although it isn't a big deal, they want me to create new branch from the master instead.

我必须使用 git 命令做什么?在我的本地 master 上进行 Git 合并?

What would git command would I have to do? Git merge on my local master?

推荐答案

有几种方法可以做到这一点.

There are a couple ways to do this.

如果你只是想从 master 创建和检出一个新分支而不检出它,你可以执行 git checkout -b new-branch master.相反,如果您不想检查它,而只想创建它,则可以运行 git branch new-branch master.

If you just want to create and check out a new branch from master without checking it out, you can do git checkout -b new-branch master. If, instead, you'd prefer not to check it out, but just create it, you can run git branch new-branch master.

如果您已经在 master 上,也许因为您正在从远程获取新的更改,那么您可以使用 git checkout -b new-branchgit branch new-branch,看你要不要马上check out.

If you are already on master, perhaps since you're pulling in new changes from the remote, then you can use git checkout -b new-branch or git branch new-branch, depending on whether you want to check it out immediately or not.

当我新建分支时,我一般会到主分支,运行git pull --ff-only origin main更新我的主分支,然后使用git checkout-b new-branch 创建并检查我的新分支,以便我可以处理它.如果您不确定该怎么做,我建议您采用这种方法.

When I'm creating new branches, I usually go to the main branch, run git pull --ff-only origin main to update my main branch, and then use git checkout -b new-branch to create and check out my new branch so I can work on it. If you're not sure what to do, I recommend that approach.

这篇关于从另一个分支还是从 master 创建 Git 分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆