git 配置本地分支以推送到特定分支

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

Configure a local branch for push to specific branch

gitgit-branchgit-pushremote-branchgit-config

提问by Senthil A Kumar

Sorry if this question has been asked already.

对不起,如果这个问题已经被问到了。

Am cloning from a repo named "git_lab" which has a branch named "test" When cloning i use "-b myname_test" to create a local branch named "myname_test" and local clone is named "myname_git_lab"

我从一个名为“git_lab”的仓库克隆,它有一个名为“test”的分支克隆时我使用“-b myname_test”创建一个名为“myname_test”的本地分支,本地克隆名为“myname_git_lab”

When i do "git pull" it automatically fetches and merges changes from "test" to "myname_test", but for git push, i need to specify the repo and branch name.

当我执行“git pull”时,它会自动获取并合并从“test”到“myname_test”的更改,但是对于 git push,我需要指定 repo 和分支名称。

$>git remote show git_lab

$>git 远程显示 git_lab

Local branch configured for 'git pull': myname_test merges with remote test

为“git pull”配置的本地分支:myname_test 与远程测试合并

Is there a way where i can configure "local branch configured for 'git push'" so that i dont need to specify the branch and repo name?

有没有办法可以配置“为'git push'配置的本地分支”,这样我就不需要指定分支和回购名称?

回答by Cascabel

There are two things you can do here.

您可以在这里做两件事。

  • Set push.defaultto tracking, so that it will push all branches to the remote branches they track, not the ones they have the same name as, then configure your branch with appropriate tracking information. (e.g. set branch.master.remoteto originand branch.master.mergeto refs/heads/foo.)

  • Push manually. git push origin master:foowill push your local masterbranch to the branch fooon the remote origin.

  • 设置push.defaulttracking,以便它将所有分支推送到它们跟踪的远程分支,而不是与它们同名的分支,然后使用适当的跟踪信息配置您的分支。(例如集branch.master.remoteoriginbranch.master.mergerefs/heads/foo)。

  • 手动推。git push origin master:foo将您的本地master分支推送到foo远程分支origin

However, I'd suggest that what you reallywant to do is just make the branch names the same.

但是,我建议您真正想做的只是使分支名称相同。

(You can set config parameters either with git config, e.g. git config push.default tracking, or by directly editing the .git/config file.)

(您可以使用git config,例如git config push.default tracking或通过直接编辑 .git/config 文件来设置配置参数。)

回答by Adam Dymitruk

git checkout --track origin/branchname

Alternatively, you can edit the config file in the .git folder.

或者,您可以编辑 .git 文件夹中的配置文件。