如果我创建一个新分支并尝试推送它,则会被告知必须明确说出上游分支的名称。
> git checkout -b feature/long-branch-name-I-dont-want-to-have-to-type-out
Switched to a new branch 'feature/long-branch-name-I-dont-want-to-have-to-type-out'
> git push
fatal: The current branch feature/long-branch-name-I-dont-want-to-have-to-type-out has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature/long-branch-name-I-dont-want-to-have-to-type-out
Run Code Online (Sandbox Code Playgroud)
有什么方法可以不必输入上游分支的名称吗?实际上,我一直希望它在服务器上与本地名称相同。
有什么方法可以执行类似的操作 git push --set-upstream <current_branch_name>,而与当前分支名称的名称无关吗?
tor*_*rek 11
[编辑] sajib khan 的第一个答案,设置push.default为current将启用推送,但实际上并未设置上游。这意味着在 future 之后git fetch,您的 Git 将不会报告提前/落后计数,并且您的 Git 将不知道要用于git rebaseor的上游git merge(或者git pull尽管我建议避免使用git pull)。
您可以使用 [edit, as in his answer的第二部分]:
git push -u origin HEAD
Run Code Online (Sandbox Code Playgroud)
如果需要,这会在另一个 Git 上创建分支,以便您的 Git 获取该origin/变体。然后在任何情况下,它都会将您拥有的(可能是新的)远程跟踪分支设置为分支的上游。但是在origin/feature/long-branch-name-I-dont-want-to-have-to-type-out实际存在之前,您不能将其设置为上游。1
1其实,你可以,只是你不能git branch --set-upstream用来做。而且,无论如何您都不想再次输入它。要“手动”执行此操作,您需要:
git config \
branch.feature/long-branch-name-I-dont-want-to-have-to-type-out.remote origin
git config \
branch.feature/long-branch-name-I-dont-want-to-have-to-type-out.merge \
feature/long-branch-name-I-dont-want-to-have-to-type-out
Run Code Online (Sandbox Code Playgroud)
这意味着输入三遍 (!),或者自己编写脚本。
配置git config
$ git config --global push.default current
Run Code Online (Sandbox Code Playgroud)
现在,结帐到分支后,您应该简单地使用 git push
$ git checkout -b new-branch
$ git push # similar to git push -u origin new-branch
Run Code Online (Sandbox Code Playgroud)
您也可以使用HEAD而不是键入当前分支名称。
$ git push -u origin HEAD
Run Code Online (Sandbox Code Playgroud)
NB HEAD和本地电流分支通常保持相同状态。
这是最好的回应版本,但被@Good Night Nerd Pride的评论所掩盖
git config --global alias.pushnew 'push -u origin HEAD'
Run Code Online (Sandbox Code Playgroud)
解释:
push -u origin HEAD是将当前分支名称推送到远程上游的更短方法。git config --global alias.pushnew设置它,以便当您运行时git push它将设置上游,就像我们在 #1 中所做的那样。并结合@Sajib Khan当前接受的答案:
git config --global push.default current
Run Code Online (Sandbox Code Playgroud)