从 master 更改为新的默认分支 git

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

Change from master to a new default branch git

gitgithub

提问by Harry

Here is a scenerio:

这是一个场景:

We have a default branch Master and we work off from this, creating branches and pushing up etc...

我们有一个默认的分支 Master,我们从这个分支开始工作,创建分支并向上推等等......

We have now created a Developbranch off Masterand set this as a default development branch.

我们现在已经在Master 上创建了一个Develop分支,并将其设置为默认的开发分支。

What I would like to know is, how do I now know if my git pull command is requesting changes from the default branch via the command line? or point to this new default branch?

我想知道的是,我现在如何知道我的 git pull 命令是否通过命令行请求来自默认分支的更改?或者指向这个新的默认分支?

What I have done: - Since creating a new default branch git pull into my master from origin/master

我做了什么: - 自从创建了一个新的默认分支 git pull 从 origin/master 进入我的 master

Also, any neww branch, will it be from Master or Develop?

另外,任何新的分支,是来自 Master 还是 Develop?

As you can see

如你看到的

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
PS C:\Users\dir\Documents\GitHub\repo> git log
commit 867cxx

fd956f73dc91d0022b (HEAD -> master, origin/master, origin/develop, origin/HEAD)

update:this change of default branch occurred after cloning the repo.

更新:这个默认分支的改变发生在克隆 repo 之后。

回答by Oliver Evans

As far as I know, git has no concept of a "default branch". User interfaces such as GitHub do have a default branch in the sense that when you open the web page, you see a certain branch (generally master) by default. But just speaking about git, the master branch is not special, it's just the name given to the first branch.

据我所知,git 没有“默认分支”的概念。GitHub 等用户界面确实有一个默认分支,因为当您打开网页时,默认情况下您会看到某个分支(通常是 master)。但是就git来说,master分支并不特殊,它只是给第一个分支取的名字。

To create a new branch, use the -bflag with checkout, as in:

要创建一个新分支,请使用-b带有的标志checkout,如下所示:

git checkout -b develop

The git branchcommand will list all of the existing branches, with a *next to the current branch. Any commits you make will be added to the current branch.

git branch命令将列出所有现有分支,并*在当前分支的旁边列出。您所做的任何提交都将添加到当前分支。

In your question, you mention pulling from a remote. The relevant concept is "Tracking Branches"; see the section with that name at https://git-scm.com/book/id/v2/Git-Branching-Remote-Branches.

在您的问题中,您提到从遥控器拉。相关概念是“跟踪分支”;请参阅https://git-scm.com/book/id/v2/Git-Branching-Remote-Branches 上具有该名称的部分。

In short, when you do

简而言之,当你做

git pull origin master

it will pull changes from the masterbranch of the originrepository into whichever branch is currently checked out. If you'd like, you can set up remote tracking so that git already knows where you want to pull from based on which branch you have checked out.

它会将更改从存储库的master分支拉origin入当前检出的任何分支。如果您愿意,您可以设置远程跟踪,以便 git 已经根据您检出的分支知道您想要从哪里提取。

e.g. if you have a remote develop-remotebranch and a local develop-localbranch, you can set your local branch to track the remote branch via:

例如,如果您有一个远程develop-remote分支和一个本地develop-local分支,则可以通过以下方式设置本地分支以跟踪远程分支:

git checkout develop-local
git branch --set-upstream-to origin/develop-remote

Also, they might both be simply called develop, I just distinguished them here for clarity.

此外,它们可能都被简单地称为develop,为了清楚起见,我只是在这里区分了它们。

Finally, I should mention that when you do git pull <url-of-repo>, remote tracking is automatically established.

最后,我应该提一下,当您这样做时git pull <url-of-repo>,会自动建立远程跟踪。

P.S. for more information about remotes (e.g. what does originmean?), see https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes

PS 有关遥控器的更多信息(例如是什么origin意思?),请参阅https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes

回答by friederbluemle

When cloning a repo from GitHub, the default branch gets stored in the HEADfile:

从 GitHub 克隆存储库时,默认分支将存储在HEAD文件中:

$ cat .git/refs/remotes/origin/HEAD
ref: refs/remotes/origin/master

If the default branch is changedon GitHub afterthe repo has been cloned, this is not updated automatically, but can easily be fixed locally:

如果在克隆 repo更改了 GitHub 上的默认分支,则不会自动更新,但可以在本地轻松修复:

git remote set-head origin -a

-awill set refs/remotes/<name>/HEAD according to remote

-a将要 set refs/remotes/<name>/HEAD according to remote

Or explicitly to a named branch:

或者显式到一个命名分支:

git remote set-head origin develop

Now, HEADpoints to the new location:

现在,HEAD指向新位置:

$ cat .git/refs/remotes/origin/HEAD
ref: refs/remotes/origin/develop