Git命令详解 下载本文

内容发布更新时间 : 2024/4/28 15:45:32星期一 下面是文章的全部内容请认真阅读。

git push

git push命令用于将本地分支的更新,推送到远程主机。它的格式与git pull命令相仿。 $ git push <远程主机名><本地分支名>:<远程分支名>

注意,分支推送顺序的写法是<来源地>:<目的地>,所以git pull是<远程分支>:<本地分支>,而git push是<本地分支>:<远程分支>。

如果省略远程分支名,则表示将本地分支推送与之存在”追踪关系”的远程分支(通常两者同名),如果该远程分支不存在,则会被新建。 $ git push origin master

上面命令表示,将本地的master分支推送到origin主机的master分支。如果后者不存在,则会被新建。

如果省略本地分支名,则表示删除指定的远程分支,因为这等同于推送一个空的本地分支到远程分支。

$ git push origin :master # 等同于

$ git push origin --delete master

上面命令表示删除origin主机的master分支。

如果当前分支与远程分支之间存在追踪关系,则本地分支和远程分支都可以省略。 $ git push origin

上面命令表示,将当前分支推送到origin主机的对应分支。

如果当前分支只有一个追踪分支,那么主机名都可以省略。 $ git push

如果当前分支与多个主机存在追踪关系,则可以使用-u选项指定一个默认主机,这样后面就可以不加任何参数使用git push。 $ git push -u origin master

上面命令将本地的master分支推送到origin主机,同时指定origin为默认主机,后面就可以不加任何参数使用git push了。

不带任何参数的git push,默认只推送当前分支,这叫做simple方式。此外,还有一种matching方式,会推送所有有对应的远程分支的本地分支。Git 2.0版本之前,默认采用matching方法,现在改为默认采用simple方式。如果要修改这个设置,可以采用gitconfig命令。 $ gitconfig --global push.default matching # 或者

$ gitconfig --global push.default simple

还有一种情况,就是不管是否存在对应的远程分支,将本地的所有分支都推送到远程主机,这时需要使用–all选项。 $ git push --all origin

上面命令表示,将所有本地分支都推送到origin主机。

如果远程主机的版本比本地版本更新,推送时Git会报错,要求先在本地做git pull合并差异,然后再推送到远程主机。这时,如果你一定要推送,可以使用–force选项。

git查看commit的内容 git查看commit的内容

在push之前有时候会不放心是不是忘记加某些文件,或者是不是多删了个什么东西,这时候希望能够看看上次commit都做了些什么。

一开始想到的是用gitdiff,但是gitdiff用于当前修改尚未commit的时候较为方便,一旦commit后,需要指定上次节点的名称(一个hash值),不方便。这种时候用gitlog更合适,因为commit的内容会以log来记录。

下面记录几个常用的情境以及对应的命令。

仅仅想看最近谁有提交,以及提交的描述 对应命令gitlog 显示Sample

commit6305aa81a265f9316b606d3564521c43f0d6c9a3 Author:XXX

Date: Thu Nov 3 11:38:15 2011 +0800

fillauthor information in the head of files and format some code commit8e8a4a96e134dab8f045937efee35bd710006946 Author:XXX

Date: Thu Nov 3 04:05:34 2011 +0800 usermanagement is mostly complete details:

add support for account disable/enable

rewrite most related views to suit the above need

provide two decorators for access control (see README) fixed many errors in Milestone 1

commit2870cd564371d8ad043d0da426a5770d36412421 Author:XXX

Date: Mon Oct 17 20:19:04 2011 -0400 fixthe bug of get_ori_url_from_shorturl().

commitb6cdd881a19ecaff838d5825c3a6b7058fdd498a Author:XXX

Date: Mon Oct 17 20:17:37 2011 -0400 fixthe bug of get_article_from_short_url. 仅仅想看最后一次的提交 对应命令参数-n1 显示Sample

commit6305aa81a265f9316b606d3564521c43f0d6c9a3 Author: XXX

Date:Thu Nov 3 11:38:15 2011 +0800

fillauthor information in the head of files and format some code 想看到最近一次提交所有更改过的文件 对应命令gitlog -n 1 --stat 显示Sample

commit6305aa81a265f9316b606d3564521c43f0d6c9a3 Author:XXX

Date: Thu Nov 3 11:38:15 2011 +0800

fillauthor information in the head of files and format some code Site/accounts/decorators.py | 2+- Site/accounts/forms.py | 1+ Site/accounts/models.py | 1+ Site/accounts/readme | 3++- Site/accounts/templates/account_activate.html | 1+ Site/accounts/templates/account_disabled.html | 1 + … …

28files changed, 37 insertions(+), 8 deletions(-)

想看到最近一次提交所有更改的细节 对应命令gitlog -n 1 -p 显示Sample

commit6305aa81a265f9316b606d3564521c43f0d6c9a3 Author:XXX

Date: Thu Nov 3 11:38:15 2011 +0800

fillauthor information in the head of files and format some code

diff--git a/Site/accounts/decorators.pyb/Site/accounts/decorators.py index 22522bc..a6bb440100755 --- a/Site/accounts/decorators.py +++b/Site/accounts/decorators.py @@ -1,9 +1,9@@ #!/usr/bin/env python