Git命令快速参考

1.初始化

配置全局用户名和电子邮件地址

gitconfig--globaluser.name"YourName"

gitconfig--globaluser.email"youe@example.com"

可以为不用的版本库(项目)单独设置用户名和邮件地址

cd/path/to/repo

gitconfiguser.name"youname"

gitconfiguser.email"you@example.com"

在命令行中使用不同的颜色

gitconfig--globalcolor.ui"auto"

初始化新版本库

mkdir/path/to/repo

cd/path/to/repo

gitinit

gitadd.

gitcommit-m"initalimport"

克隆版本库

gitclone<repositoryurl>

gitclonehttp://git.oschina.net/oschina/git-osc.git

将目录中的内容纳入Git版本控制

cd/path/to/existing/directory

gitinit

gitadd.

gitcommit-m"initalimportofsomeproject."

在本地版本库中设置远程版本库的别名

gitremoteadd<remoterepository><repositoryurl>

2.日常操作

gitadd<somefile>

gitcommit-m"<somefilemessage>"

暂存已有文件上的部分修改

gitadd-p[<somefile>[<somefile>[andsoon]]]

使用交互式添加文件

gitadd-i

暂存已纳入Git版本控制之下的文件的修改

gitadd-u[<somepath>[<somepath>]]

提交纳入Git版本控制之下的文件修改

gitcommit-m"somemessage"-a

清除工作目录数的一些修改

gitcheckoutHEAD<somefile>[<somefile>]

取消已暂存但尚未提交的暂存标识

gitresetHEAD<somefile>[<somefile>]

修复上一次提交中的问题

修改文件并暂存

gitcommit-m"somemessage"--amend

修复上一次提交中的问题,并有上次的提交注释

gitcommit-CHEAD--amend

3.分支

列出本地分支

gitbranch

列出远程分支

gitbranch-r

列出所有分支

gitbranch-a

基于当前分支(的末梢)创建新分支

gitbranch<newbranch>

检出另一条分支

gitcheckout<somebranch>

基于当前分支创建新分支并检出该分支

gitcheckout-b<newbranch>

基于另一起点创建新分支

gitbranch<newbranch><startpoint>

创建同名新分支,覆盖已有分支

gitbranch-f<someexitsingbranch>[startpoint]

移动和重命名分支

gitcheckout-m<exitsingbranch><newbranchname>

把另一条分支合并到当前分支

gitmerge<somebranch>

命并但不提交

gitmerge--no-commit<somebranch>

拣选合并并提交

gitcherry-pick<commitname>

拣选合并但不提交

gitcherry-pick-n<commitname>

把一打分支上的内容压合(压缩合并)到另一条分支(上一个提交)

gitmerge--squash<somebranch>

删除分支

gitbranch-d<branchtodelete>

不论是否已合并都删除某分支

gitbranch-D<branchtodelete>

4.历史

显示全部历史

gitlog

显示版本历史以及版本间的内容差异

gitlog-p

只显示最近3个提交

gitlog-3

显示最近20个提交以及版本间的内容差异

gitlog-20-p

显示最近6小时的提交

gitlog--since="6hours"

gitlog--before="2days"

显示HEAD(当前检出分支的末梢)早3个提交的那个提交

gitlog-1HEAD~3

gitlog-1HEAD^^^或者gitlog-1HEAD~1^^

显示两个版本之间的提交

gitlog<startpoint><endpoint>

显示历史,每个提交显示一行,包括提交的注释的第一行

gitlog--pretty=oneline

显示改动文件的名称和状态

gitlog--name-status

显示当前工作目录树和暂存区间的差别

gitdiff

显示暂存区和版本库之间的差别

gitdiff--cached

显示当前工作目录树和版本库中某次版本之间的差别

gitdiffHEAD

显示工作目录与版本库中某次提交版本之间的差别

gitdiff<startpoint>

显示版本库中两个版本之间的差别

gitdiff<startpoint><endpoint>

显示差别相关的统计

gitdiff--stat<startpoint><endpoint>

显示文件中各个部分的修改者及相关提交信息

gitblame<somefile>和gitblame-M<somefile>

显示文件中各部分的修改及相关的提交信息,包括在文件间的移动内容方面的情况

gitblame-C-c<somefile>

显示历史时,显示复制和粘贴信息

gitlog-C-c-p-1<somepoint>

5.远程版本库

克隆远程版本库

gitclone<somerepositoryurl>

克隆远程版本库,但只下载其中最近200个提交的历史记录

gitclone--depth200<somerepositoryurl>

在本地版本库中设置远程版本库的别名

gitremoteadd<remoterepository><repositoryurl>

显示远程分支

gitbranch-r

基于远程分支创建本地分支

gitbranch<newbranch><remotebranch>

基于远程标签创建本地分支

gitbranch<newbranch><remotetag>

从别名Origin的远程版本库中取来修改变化,但不合并到本地分支

gitfetch

从任意的远程版本库中取来修改变化,但不合并到本地分支

gitfetch<remoterepository>

从任意的远程版本库中取来修改变化,并合并到当前检出的本地分支

gitpull<remoterepository>

从别名为"Origin"的远程版本库中取来修改变化,并合并到当前的检出本地分支

gitpull

把修改变化从本地分支推入远程版本库

gitpush<remoterepository><localbranch>:<remotebranch>

把修改变化从本地分支推入到远程本库同名分支

gitpush<remoterepository><localbranch>

把修改变化从票地新建分支推入到远程本库

gitpush<remoterepository><localbranch>

把修改变化推入别名为"Origin"的远程本库

gitpush

在远程版本库中删除分支

gitpush<remoterepository>:<remeotebranch>

在本地版本库中删除所有远程版本库中已不存在的分支

gitremoteprune<remoterepository>

在本地版本库中删除某个远程版本库的简称,以及该远程本库相关的分支

gitremoterm<remoterepository>

6.连接Git和SVN

克隆SVN版本库的全部内容

gitsvnclone<svnrepository>

克隆具有标准结构的SVN版本库(主干命名为trunk其它都在branches目录下)

gitsvnclone-s<svnrepository>

克隆非标准结构的svn版本库

gitsvnclone-T<trunkpath>-b<branchpath>-t<tagpath><svnrepository>

克隆标准结构的SVN中的版本库的某个版本

gitsvnclone-s-r2321

克隆具有标准结构的SVN版本库,并对SVN中的分支添加前缀

gitsvnclone-s--prefixsvn/<svnrepository>

从上游SVN版本库中获得更新内容,并依此在本地GIT版本库中基变本地分支

gitsvnrebase

把修改变化推回上游SVN版本库

gitsvndcommit

列出所有推入上游SVN版本库的提交

gitsvndcommit-n

显示svn历史记录

gitsvnlog

显示文件中各个部分的svn的修改者及相关的提交信息

gitsvnblame<somefile>

相关推荐