简体   繁体   English

将默认分支更改为从master检出到开发

[英]Change default branch to checkout from master to develop

On a new repository I very often find myself having made changes to the master branch, where of course I should have started with the develop branch. 在一个新的存储库上,我经常发现自己已经对master分支进行了更改,当然我应该从develop分支开始。
Of course I should create a feature branch before I start making changes, in which case I'd notice that I'm on master, not develop. 当然,在开始进行更改之前,我应该先创建一个功能分支,在这种情况下,我会发现自己在开发中而不是开发中。
However is there also a setting in git to change the default clone/checkout branch to develop? 但是git中是否还有设置可以更改默认的clone / checkout分支进行开发?

Yes, there is a way to do it. 是的,有一种方法可以做到。

When you run git clone <url> you have your Git call up some other Git. 当您运行git clone <url>您的Git会调用其他Git。 That other Git has a repository; 另一个Git有一个存储库; your Git makes a new copy of their repository. 您的Git会为其存储库制作一个新副本。

If you supply a -b branch argument to your git clone , your Git ends the cloning process with git checkout branch . 如果您提供了-b branch参数传送 git clone使用 Git 结束与克隆过程git checkout branch So that's the easiest way to do it, but of course if you forgot to run git checkout , you might forget the -b argument too. 因此,这是最简单的方法,但是,如果您忘记运行git checkout ,那么您可能也会忘记-b参数。

If you don't supply -b branch , your Git gets a suggested best branch from the other Git. 如果您提供-b branch ,那么您的Git将从另一个 Git获得建议的最佳分支 Your Git then uses that for its final git checkout step. 然后,您的Git将其用于最后的git checkout步骤。 So if you have access to the other Git repository, you just have to go there and change which branch it will suggest. 因此,如果您有权访问其他 Git存储库,则只需去那里更改其建议的分支。

The branch the other Git will suggest is determined by which branch is checked-out / current in that repository. 其他Git将建议的分支由该存储库中已签出/当前的分支确定。 Since most server repositories are "bare" (have no work-tree), you can't actually check out a branch there, but they still have a current branch. 由于大多数服务器存储库都是“裸机”(没有工作树),因此您实际上无法在其中签出分支,但它们仍具有当前分支。 You can update it with git symbolic-ref : 您可以使用git symbolic-ref更新它:

server$ cd repo
server$ git symbolic-ref HEAD refs/heads/develop

If you can't log in to the server, the server may or may not provide some way to do the equivalent. 如果您无法登录服务器,则服务器可能会或可能不会提供某种方式来执行此操作。

The default branch git checks out is determined by the remote repo's HEAD , which is almost always pointed at master . git签出的默认分支是由远程仓库的HEAD决定的,该HEAD几乎总是指向master I'm not aware of a local setting that would change clone 's default. 我不知道会更改clone的默认值的本地设置。 (You can approximate that behavior with caveats; see below.) Because git can't generally assume a branch of any given name (including master ) would exist in an arbitrary remote, I wouldn't expect it to have such a feature; (您可以通过警告来近似表示这种行为;请参见下文。)由于git通常不能假定任何给定名称的分支(包括master )都将存在于任意远程中,所以我不希望它具有这样的功能; it relies on the remote to tell it what the default branch is (again, via the remote's HEAD ). 它依靠遥控器告诉它默认分支是什么(同样,通过遥控器的HEAD )。

Checking out master by default is not the most convenient thing for developers in a lot of branching models, but it is a pretty deeply ingrained convention; 在许多分支模型中,默认情况下签出master并不是对开发人员最方便的事情,但这是一个根深蒂固的约定。 so my recommendation is to adapt your workflow habits to that, instead of trying to do the opposite. 因此,我的建议是适应您的工作流程习惯,而不要尝试相反的做法。

But what are some options? 但是有什么选择呢?

For a given clone command, you can use the --branch option to select what is iitially checked out 对于给定的clone命令,可以使用--branch选项选择最初签出的内容

git clone --branch develop http://some.server/repo.git

Building on that, you could define an alias to clone with the --branch option. 在此基础上,您可以定义别名以使用--branch选项进行克隆。 (But the alias will fail if used on a repo that doesn't have a branch with your chosen name.) (但是,如果别名在没有分支的仓库中使用,别名将失败。)

For repos you control, you could change the HEAD to point to the branch of your choice, but I really recommend you don't for several reasons. 对于您可以控制的回购协议,您可以将HEAD更改为指向您选择的分支,但是出于某些原因,我确实建议您不要这样做。 First, it may cause confusion if the repo is shared. 首先,如果共享仓库,可能会造成混乱。 (Similarly it could complicate the setup of build tools, though that really isn't hard to deal with.) But the biggest reason I wouldn't is, then you're building habits when using your own repos, and those habits will not translate to using others' repos. (类似地,它可能会使构建工具的设置复杂化,尽管这实际上并不难处理。)但是我不愿意这样做的最大原因是,您在使用自己的存储库时会养成习惯,而这些习惯不会转化为使用他人的回购协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM