将本地分支推送到GitHub

119

我已经配置了Git,这样当我运行git push时,它会将更改推送到我的GitHub存储库。直到现在,我只有一个主分支。

然而,我现在创建了一个本地分支并使用以下命令提交:

git checkout -b my_new_branch
git commit

我现在想把这个分支的更改推送到GitHub上。我只需要执行git push命令吗?

刚开始设置时,我运行了以下命令:

git config push.default current

你的问题是什么?如果设置了 push.default,那么是的,git push 将会把当前分支推送到 origin,也就是你的 Github 仓库,假设你是从那里克隆的。(如果你想要指定不同的远程仓库,可以使用 branch.my_new_branch.remote。)所以你尝试过这个方法但没有成功吗? - Cascabel
1
如果您希望所有本地分支都推送到同一个远程分支,请明确指定:git push origin HEAD:remote_branch - Ustaman Sangat
4个回答

222

我相信你正在寻找git push origin my_new_branch,假设你的origin远程配置为连接到你的github代码仓库。


根据https://www.atlassian.com/git/tutorials/syncing/git-push,将git push <remote> <branch>翻译为中文。 - vikramvi
3
如果你想和其他人一起在这个分支上工作并执行 git pull,你需要为你的新分支设置跟踪信息:git branch --set-upstream-to=origin/my_new_branch my_new_branch - gloriphobia

9

根据您本地的git设置,如果您签出的分支不是您克隆的分支或者在您尝试推送的位置存在一个分支,git将不会推送您的本地分支。

以下是它提供的信息:

warning: push.default is unset; its implicit value has changed in Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the traditional behavior, use:

git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

git config --global push.default simple

When push.default is set to 'matching', git will push local branches to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple' behavior, which only pushes the current branch to the corresponding remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information. (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode 'current' instead of 'simple' if you sometimes use older versions of Git)

fatal: The current branch MyLocalBranch has no upstream branch. To push the current branch and set the remote as upstream, use

git push --set-upstream origin MyLocalBranch

0

如果你真的很懒,你可以通过简单地使用push all local branches来推送所有本地分支。

git push --all

--all

推送所有分支(即 refs/heads/ 下的引用);不能与其他 <refspec> 一起使用。


-12
如果你已经配置好了git来推送到你的GitHub主仓库,那么无论你在哪个分支上,它都会推送到你的GitHub主仓库。
请记住,如果许多开发人员在同一个存储库中工作,可能会发生冲突。

我运行了git config push.default current。 - Noam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接