<p ><code >git push</code>命令用于将本地分支的更新,推送到远程主机。它的格式与<code >git pull</code>命令相似。</p><pre class=" language-shell code-toolbar" >$gitpush<远程主机名><本地分支名>:<远程分支名>Shell</pre><p ><strong >使用语法</strong></p><pre class=" language-shell code-toolbar" >gitpush[--all|--mirror|--tags][--follow-tags][--atomic][-n|--dry-run][--receive-pack=<git-receive-pack>]
[--repo=<repository>][-f|--force][-d|--delete][--prune][-v|--verbose]
[-u|--set-upstream][--push-option=<string>]
[--[no-]signed|--sign=(true|false|if-asked)]
[--force-with-lease[=<refname>[:<expect>]]]
[--no-verify][<repository>[<refspec>…]]Shell</pre><h2 ><a class="reference-link" ></a><span class="header-link octicon octicon-link" ></span>描述</h2><p >使用本地引用更新远程引用,同时发送完成给定引用所需的对象。可以在每次推入存储库时,通过在那里设置挂钩触发一些事件。</p><p >当命令行不指定使用<code ><repository></code>参数推送的位置时,将查询当前分支的<code >branch.*.remote</code>配置以确定要在哪里推送。 如果配置丢失,则默认为<code >origin</code>。</p><h2 ><a class="reference-link" ></a><span class="header-link octicon octicon-link" ></span>示例</h2><p >以下是一些示例 -</p><pre class=" language-shell code-toolbar" >$gitpushoriginmasterShell</pre><p >上面命令表示,将本地的<code >master</code>分支推送到<code >origin</code>主机的<code >master</code>分支。如果<code >master</code>不存在,则会被新建。</p><p >如果省略本地分支名,则表示删除指定的远程分支,因为这等同于推送一个空的本地分支到远程分支。</p><pre class=" language-shell code-toolbar" >$gitpushorigin:master
#等同于
$gitpushorigin--deletemasterShell</pre><p >上面命令表示删除<code >origin</code>主机的<code >master</code>分支。如果当前分支与远程分支之间存在追踪关系,则本地分支和远程分支都可以省略。</p><pre class=" language-shell code-toolbar" >$gitpushoriginShell</pre><p >上面命令表示,将当前分支推送到<code >origin</code>主机的对应分支。如果当前分支只有一个追踪分支,那么主机名都可以省略。</p><pre class=" language-shell code-toolbar" >$gitpushShell</pre><p >如果当前分支与多个主机存在追踪关系,则可以使用<code >-u</code>选项指定一个默认主机,这样后面就可以不加任何参数使用<code >git push</code>。</p><pre class=" language-shell code-toolbar" >$gitpush-uoriginmasterShell</pre><p >上面命令将本地的<code >master</code>分支推送到<code >origin</code>主机,同时指定<code >origin</code>为默认主机,后面就可以不加任何参数使用<code >git push</code>了。</p><p >不带任何参数的<code >git push</code>,默认只推送当前分支,这叫做<code >simple</code>方式。此外,还有一种<code >matching</code>方式,会推送所有有对应的远程分支的本地分支。Git 2.0版本之前,默认采用<code >matching</code>方法,现在改为默认采用<code >simple</code>方式。如果要修改这个设置,可以采用<code >git config</code>命令。</p><pre class=" language-shell code-toolbar" >$gitconfig--globalpush.defaultmatching
#或者
$gitconfig--globalpush.defaultsimpleShell</pre><p >还有一种情况,就是不管是否存在对应的远程分支,将本地的所有分支都推送到远程主机,这时需要使用<code >–all</code>选项。</p><pre class=" language-shell code-toolbar" >$gitpush--alloriginShell</pre><p >上面命令表示,将所有本地分支都推送到<code >origin</code>主机。<br/>如果远程主机的版本比本地版本更新,推送时Git会报错,要求先在本地做<code >git pull</code>合并差异,然后再推送到远程主机。这时,如果你一定要推送,可以使用<code >–force</code>选项。</p><pre class=" language-shell code-toolbar" >$gitpush--forceoriginShell</pre><p >上面命令使用<code >-–force</code>选项,结果导致在远程主机产生一个”非直进式”的合并(non-fast-forward merge)。除非你很确定要这样做,否则应该尽量避免使用<code >–-force</code>选项。</p><p >最后,<code >git push</code>不会推送标签(tag),除非使用<code >–tags</code>选项。</p><pre class=" language-shell code-toolbar" >$gitpushorigin--tagsShell</pre><p >将当前分支推送到远程的同名的简单方法,如下 -</p><pre class=" language-shell code-toolbar" >$gitpushoriginHEADShell</pre><p >将当前分支推送到源存储库中的远程引用匹配主机。 这种形式方便推送当前分支,而不考虑其本地名称。如下 -</p><pre class=" language-shell code-toolbar" >$gitpushoriginHEAD:masterShell</pre><p ><strong >其它示例</strong></p><p >1.推送本地分支<code >lbranch-1</code>到新大远程分支<code >rbranch-1</code>:</p><pre class=" language-shell code-toolbar" >$gitpushoriginlbranch-1:refs/rbranch-1Shell</pre><p >2.推送<code >lbranch-2</code>到已有的<code >rbranch-1</code>,用于补充<code >rbranch-1</code>:</p><pre class=" language-shell code-toolbar" >$gitcheckoutlbranch-2
$gitrebaserbranch-1
$gitpushoriginlbranch-2:refs/rbranch-1Shell</pre><p >3.用本地分支<code >lbranch-3</code>覆盖远程分支<code >rbranch-1</code>:</p><pre class=" language-shell code-toolbar" >$gitpush-foriginlbranch-2:refs/rbranch-1Shell</pre><p >或者 -</p><pre class=" language-shell code-toolbar" >$gitpushorigin:refs/rbranch-1//删除远程的rbranch-1分支
$gitpushoriginlbranch-1:refs/rbranch-1Shell</pre><p >4.查看<code >push</code>的结果</p><pre class=" language-shell code-toolbar" >$gitkrbranch-1Shell</pre><p >5.推送tag</p><pre class=" language-shell code-toolbar" >$gitpushorigintag_nameShell</pre><p >6.删除远程标签</p><pre class=" language-shell code-toolbar" >$gitpushorigin:tag_name</pre>