简体   繁体   English

git 推送不创建/更新远程分支,而是直接更新远程

[英]git push doesn't create/update remote branch, but updates remote directly

Recently, my local git has changed how it pushes branches to remote and I can't figure out why.最近,我的本地 git 更改了将分支推送到远程的方式,我不明白为什么。 Previously, I would create new branches with the command:以前,我会使用以下命令创建新分支:

git checkout -b feature_x origin/master

Then, push the new branch to remote (in order to create a Pull Request):然后,将新分支推送到远程(以创建合并请求):

git push origin feature_x

This worked fine up until last week, however, now when I run that last command, git pushes directly to remote without creating a branch (or allowing a PR to be created for that branch).直到上周这一切都很好,但是,现在当我运行最后一个命令时,git 直接推送到远程而不创建分支(或允许为该分支创建 PR)。 I'm thinking I must have changed a setting without realizing it, to get to this situation, but I'm not sure which one.我想我一定是在不知不觉中更改了设置,才会出现这种情况,但我不确定是哪一个。 Does anyone have any idea what would cause this behavior and perhaps a way to go back to the old way of doing things?有谁知道什么会导致这种行为,也许还有一种方法可以让 go 回到原来的做事方式?

Output from git push origin feature_x : Output 来自git push origin feature_x

To https://github.com/[URL]
  2826f0c..66748dc  feature_x -> master

Config:配置:

push.default=tracking
core.editor=vim
core.askpass=git-gui--askpass
branch.autosetupmerge=true
credential.helper=osxkeychain
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
remote.origin.url=https://github.com/[URL]
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.feature_x.remote=origin
branch.feature_x.merge=refs/heads/master

Please don't mix up braches and repositories. 请不要混淆分支机构和存储库。

You have two different repositories. 您有两个不同的存储库。 Your local one and a remote one at github. 您的本地人和远程人在github上。

You create a branch feature_x in your local repository and commit your changes on that local branch. 您在本地存储库中创建了一个分支feature_x ,并在该本地分支上提交了更改。

Later you try to push your local branch to the remote repository. 稍后,您尝试将本地分支推送到远程存储库。 The complete syntax is: 完整的语法为:

git push origin src:dst

This means push to a remote location named origin (which is probably configured as your github repository) and take the local branch src and name it dst at the remote repository. 这意味着推送到名为origin的远程位置(可能已配置为您的github存储库),并获取本地分支src并在远程存储库中将其命名为dst

You did not specify :dst , therefore some default will be used. 您没有指定:dst ,因此将使用一些默认值。

From your description you expected feature_x:feature_x but you got feature_x:master . 根据您的描述,您期望feature_x:feature_x但您却拥有feature_x:master

git help push says: git help push说:

   The <dst> tells which ref on the remote side is updated with this push.
   Arbitrary expressions cannot be used here, an actual ref must be named. If git
   push [<repository>] without any <refspec> argument is set to update some ref
   at the destination with <src> with remote.<repository>.push configuration
   variable, :<dst> part can be omitted---such a push will update a ref that
   <src> normally updates without any <refspec> on the command line. Otherwise,
   missing :<dst> means to update the same ref as the <src>.

Thanks for your update. 感谢您的更新。

git branch -vv should show, that your feature branch is tracking origin/master . git branch -vv应该显示,您的功能分支正在跟踪origin/master This is fine so far. 到目前为止还好。 But then push.default=tracking says you want to push to the tracking branch as a default. 但是,然后push.default=tracking表示您要默认推送到跟踪分支。 -> This explains the feature_x:master behavior. ->这说明了feature_x:master行为。

Maybe you want push.default=current or push.default=simple . 也许您想要push.default=currentpush.default=simple (Since git 2.0 simple is also the default.) (因为git 2.0 simple也是默认设置。)

Does anyone have any idea what would cause this behavior and perhaps a way to go back to the old way of doing things?有谁知道什么会导致这种行为,也许还有一种方法可以让 go 回到原来的做事方式?

It looks like @michas, with the help of your config, already identified push.default=tracking as the problem.看起来 @michas 在您的配置的帮助下,已经将push.default=tracking确定为问题所在。

As long as you're using git 2.37+, I would like to recommend, based on your expectations, however, that you use the following setup:只要您使用的是 git 2.37+,根据您的期望,我建议您使用以下设置:

git config --global push.default simple
git config --global branch.autoSetupMerge simple
git config --global push.autoSetupRemote true
  • push.default=simple will ensure that you only push "implicitly" to remote branches with the same name as your local branch name, and fail otherwise. push.default=simple将确保您仅“隐式”推送到与本地分支名称同名的远程分支,否则将失败。
  • branch.autoSetupMerge=simple will ensure that you only get tracking relationships on new branches when they are created from same-name remote branches. branch.autoSetupMerge=simple将确保您仅在从同名远程分支创建新分支时获取新分支的跟踪关系。
  • push.autoSetupRemote=true will ensure that when you push a new local branch to the remote, it automatically gets a tracking relationship (so git pull works, git status shows you how many local unpushed commits you have, etc), and it also makes git push just work on new branches - without needing to specify the remote, or the branch. push.autoSetupRemote=true将确保当您将新的本地分支推送到远程时,它会自动获得跟踪关系(因此git pull有效, git status显示您有多少本地未推送的提交等),并且它还使git push仅在新分支上工作 - 无需指定远程或分支。 For a new branch, git push then does what you would otherwise need to run git push -u origin HEAD for.对于新分支, git push然后执行您需要运行git push -u origin HEAD的操作。

You can execute the following command to update the upstream 您可以执行以下命令来更新上游

git push -u origin feature_x 

After that it should start to work as usually 之后,它应该可以正常工作了

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

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