简体   繁体   English

如何获取git以按名称自动匹配远程分支?

[英]How can I get git to automatically match a remote branch by name?

I'm often working with cloned repos, having an "origin" (my) and "upstream" (original source). 我经常使用克隆的存储库,有一个“原始”(我)和“上游”(原始资源)。 I'm always cloning the "origin" repo from github for working on it and create PRs and from time to time need to pull from "upstream" to sync with the latest changes going on there. 我总是从github上克隆“原始”存储库以进行处理并创建PR,并且有时需要从“上游”中提取信息以与正在进行的最新更改保持同步。

After git clone <origin uri> I can do git clone <origin uri>我可以做

git push/pull

without specifying the branch since its already tracked; 由于分支已经被跟踪,所以不指定分支; however doing this on the other remote 但是在另一个遥控器上执行此操作

git pull upstream

when eg on master branch I would like git to actually do 当例如在master分支上我想git实际上

git pull upstream master

instead of: 代替:

You asked to pull from the remote 'upstream', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

I understand I can configure the tracking remote but I'd like to know if this can be automatic as long as branch names are identical. 我知道我可以配置跟踪遥控器,但是我想知道只要分支名称相同,它是否可以自动进行。

You asked to pull from the remote 'upstream', but did not specify a branch. 您要求从远程“上游”拉出,但未指定分支。 Because this is not the default configured remote for your current branch, you must specify a branch on the command line. 因为这不是当前分支的默认配置远程服务器,所以必须在命令行上指定一个分支。

Looking at the message, the first thing is as you haven't configured upstream as your default remote. 查看消息,第一件事是您尚未将upstream配置为默认遥控器。 So, first, you need to set it as the default remote like: 因此,首先,您需要将其设置为默认遥控器,例如:

git config branch.branch_name.remote upstream

Now, you need to specify a branch from where you want to fetch the commits. 现在,您需要指定一个要从中提取提交的分支。 You can specify that by running 您可以通过运行来指定

git config branch.branch_name.merge refs/heads/branch_name

Hope it helps. 希望能帮助到你。

Just use --track or --set-upstream-to(-u) : 只需使用--track--set-upstream-to(-u)

$ git branch --track master upstream/master

For the current branch: 对于当前分支:

$ git branch -u upstream/master
$ git branch --set-upstream-to=upstream/master

This'll assign the remote-tracking branch upstream/master to your master branch so that it's automatically fetched from in the next git pull invocation. 这会将远程跟踪分支upstream/master分配给您的master分支,以便在下一个git pull调用中自动获取它。

From the man page: 从手册页:

   -t, --track
       When creating a new branch, set up branch.<name>.remote and branch.<name>.merge
       configuration entries to mark the start-point branch as "upstream" from the new
       branch. This configuration will tell git to show the relationship between the 
       two branches in git status and git branch -v. Furthermore, it directs git pull
       without arguments to pull from the upstream when the new branch is checked out.

   -u <upstream>, --set-upstream-to=<upstream>
       Set up <branchname>'s tracking information so <upstream> is considered 
       <branchname>'s upstream branch. If no <branchname> is specified, then it defaults
       to the current branch.

您可以使用以下配置,因此,每当您从远程签出新分支时,都会自动对其进行跟踪,而无需使用--set-upstream-to--track设置跟踪

git config --global branch.autosetupmerge true

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

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