您的位置:首页 > 其它

Git的操作命令总结

2017-12-24 18:23 232 查看

首先需要了解以下四个概念:

  (1)Workspace(工作区):新添加的,和修改的未add操作的。  (2)Index/ Stage(暂存区):add操作过后,会进入暂存区。  (3)Repository(本地仓库):commit操作后,会进入本地仓库。  (4)Remote(远程仓库):push操作后,会提交到远程仓库。如图所示:一,创建代码库
gitinit#在当前目录新建一个Git代码库
[/code]
gitinit[mkdir-name]#新建一个目录,将其初始化
gitclone[url]#下载整个项目的代码
[/code]二,Git的配置
gitconfig--list#显示当前的Git配置
[/code]
[/code]
#设置提交代码时的用户信息
gitconfig[--global]user.name"[name]"
[/code]
gitconfig[--global]user.email"[emailaddress]"
[/code]三,增加/删除文件
gitadd[file1][file2]...#添加指定文件到暂存区
[/code]
gitadd[dir]#添加指定目录到暂存区,包括它下面的子目录
[/code]
gitadd.#添加当前目录下的所有文件到暂存区
[/code]
[/code]
gitadd-p#对于同一个文件的多处变化,可以实现分次提交
[/code]
[/code]
gitrm[file1][file2]...#删除工作区文件,并且将这次删除放入暂存区
gitrm--cached[file]#停止追踪指定文件,但该文件会保留在工作区
gitmv[file-original][file-renamed]#改文件名,并且将这个改名放入暂存区
四,代码提交
gitcommit-m[注释内容]#提交暂存区的代码到仓库区
[/code]
gitcommit[file1][file2]...-m[注释内容]#提交暂存区的指定文件到仓库区
gitcommit-a#提交工作区自上次commit之后的变化,直接到仓库区
[/code]
gitcommit-v#提交时显示所有diff信息
[/code]
gitcommit--amend-m[注释内容]#如果代码没有任何新的变化,则用来改写上一次commit的提交信息
[/code]
gitcommit--amend[file1][file2]...#重做上一次commit,并包括指定文件的新变化
[/code]五,分支操作
gitbranch#列出所有本地分支
[/code]
gitbranch-r#列出所有远程分支
gitbranch-a#列出所有本地分支和远程分支
[/code]
gitbranch[branch-name]#新建一个分支,但依然停留在当前分支
[/code]
gitcheckout-b[branch]#新建一个分支,并切换到该分支
[/code]
gitbranch[branch][commit]#新建一个分支,指向指定commit
[/code]
gitbranch--track[branch][remote-branch]#新建一个分支,与指定的远程分支建立建立追踪关系
[/code]
gitcheckout[branch-name]#切换到指定分支,并更新工作区
[/code]
gitcheckout-#切换到上一个分支
[/code]
gitbranch--set-upstream[branch][remote-branch]#建立追踪关系,在现有分支与指定的远程分支之间
[/code]
gitmerge[branch]#合并指定分支到当前分支
[/code]
gitcherry-pick[commit]#选择一个commit,合并到当前分支
[/code]
gitbranch-d[branch-name]#删除分支
[/code]
#删除远程分支
[/code]
gitpushorigin--delete[branch-name]
[/code]
gitbranch-dr[remote/branch]
[/code]六,标签操作
gittag#列出所有tag
[/code]
gittag[tag]#新建一个tag在当前commit
gittag[tag][commit]#新建一个tag,并指定commit
[/code]
gittag-d[tag]#删除本地tag
[/code]
gitpushorigin:refs/tags/[tagName]#删除远程tag
[/code]
gitshow[tag]#查看tag信息
[/code]
gitpush[remote][tag]#提交指定tag
[/code]
gitpush[remote]--tags#提交所有tag
[/code]
gitcheckout-b[branch][tag]#新建一个分支,并指向某个tag
[/code]七,查看信息
gitstatus#显示有变更的文件
[/code]
gitlog#显示当前分支的版本历史
gitlog--stat#显示commit历史,以及每次commit发生变更的文件
[/code]
gitlog-S[keyword]#根据关键词,搜索提交历史
[/code]
gitlog[tag]HEAD--pretty=format:%s#显示某个commit之后的所有变动,每个commit占据一行
[/code]
gitlog[tag]HEAD--grepfeature#显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件
[/code]
[/code]
#显示某个文件的版本历史,包括文件名
gitlog--follow[file]
gitwhatchanged[file]
gitlog-p[file]#显示指定文件相关的每一次diff
gitlog-5--pretty--oneline#显示过去5次的提交
gitshortlog-sn#显示所有提交过的用户,并按提交次数排序
gitblame[file]#显示指定文件是谁在什么时候修改过
gitdiff#显示暂存区和工作区的差异
gitdiff--cached[file]#显示暂存区和上一个commit的差异
gitdiffHEAD#显示工作区与当前分支最新commit之间的差异
gitdiff[first-branch][second-branch]#显示两次提交的之间的差异
gitdiff--shortstat"@{0dayago}"#显示今天你写了多少行代码
gitshow[commit]#显示某次提交的原数据和内容变化
gitshow--name-only[commit]#显示某次提交发生变化的文件
gitshow[commit]:[filename]#显示某次提交时,某个文件的内容
gitreflog#显示当前分支的最近几次提交
八,远程同步
gitfetch[remote]#下载远程仓库的所有变动
[/code]
gitremote-v#显示所有远程仓库
gitremoteshow[remote]#显示某个远程仓库的信息
[/code]
gitremoteadd[shortname][url]#增肌一个新的远程仓库,并命名
[/code]
gitpull[remote][branch]#获取远程仓库的变化,并与本地分支合并
[/code]
gitpush[remote][branch]#上传本地指定分支到远程仓库
[/code]
gitpush[remote]--force#强行推送当前分支到远程仓库,即使有冲突
[/code]
gitpush[remote]--all#推送所有分支到远程仓库
[/code]
九,撤销操作
gitcheckout[file]#恢复暂存区的指定文件到工作区
[/code]
gitcheckout[commit][file]#恢复某个commit的指定文件到暂存区和工作区
gitcheckout.#恢复暂存区的所有文件到工作区
[/code]
gitreset[file]#重置暂存区的指定的文件,与上一次commit保持一致,但工作区不变
[/code]
gitreset--hard#重置暂存区与工作区,与上一次commit保持一致
[/code]
gitreset[commit]#重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
[/code]
gitreset--hard[commit]#重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
[/code]
gitreset--keep[commit]#重置当前HEAD为指定commit,但保持暂存区和工作区不变
[/code]
gitrevert[commit]#后者的所有变化都将被前者抵消,并且应用到当前分支
[/code]
[/code]
#暂时将未提交的变化清除,稍后再移入
[/code]
gitstash
[/code]
gitstashpop
[/code]
十,其他操作
gitarchive#生成一个可供发布的压缩包
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: