测试开发-Git常用命令总结

Git分布式版本管理系统,一般来说,日常使用只要记住下图6个命令,就可以了。但是熟(ti)练(sheng)使(bi)用(ge),恐怕要记住60~100个命令。Githug通关小游戏,从入门到放弃
这次没有原因,一次次的感受Git的高逼格后,经不住诱惑开始进行总结,仅此而已,本次总结按照常用的命令模块进行总结!
Git工作流程解析

1.Workspace:工作区
简单说就是Git版本库所在的目录
2.Index / Stage:暂存区
在.git下有个index文件,实际上是个包含文件索引的目录树,更像是一个虚拟的工作区,记录了文件名和文件的状态信息(时间戳和文件长度等),文件的内容没有存储在里面,保存在Git对象库
3.Repository:仓库区(或本地仓库)
对于git来说,仓库位于工作区跟目录下的.git目录中,且仅此一处,在工作区的的子目录下则没有任何其他跟踪文件或目录,一般来说git init 后此为隐藏
4.Remote:远程仓库(远程代码所在)
清楚明了的将Git流程分析解释,不吹不黑,若是不明白,请Google搭梯子(Git权威指南)
新建本地版本库/代码库

在当前目录新建一个Git代码库
gitinitGit git init [project-name]
下载一个项目和它的整个代码历史
$ git clone [url/http/SSH]

配置

Git的设置文件为.gitconfig,它可以在用户主目录下(–gloal全局配置),也可以在项目目录下(–local项目配置)

显示当前的Git配置

gitconfiglistGit git config -e [–global/–local]
设置提交代码时的用户信息
gitconfig[global/local]user.name[name] git config [–global/–local] user.email “[email address]”

增加/删除文件

添加指定文件到暂存区(-h,可以查询每个指令后的所有简短命令)
gitadd[file1][file2] git add [dir]
添加当前目录的所有文件到暂存区
gitadd. git add -p
删除工作区文件,并且将这次删除放入暂存区
gitrm[file1][file2] git rm –cached [file]
改名文件,并且将这个改名放入暂存区
$ git mv [file-original] [file-renamed]

代码提交

提交暂存区到仓库区
gitcommitm[message] git commit [file1] [file2] … -m [message]
提交工作区自上次commit之后的变化,直接到仓库区
gitcommitadiff git commit -v
使用一次新的commit,替代上一次提交
如果代码没有任何新变化,则用来改写上一次commit的提交信息
gitcommitamendm[message]commit git commit –amend [file1] [file2] …

分支

列出所有本地分支
gitbranch git branch -r
列出所有本地分支和远程分支
gitbrancha git branch [branch-name]
新建一个分支,并切换到该分支
gitcheckoutb[branch]commit git branch [branch] [commit]
新建一个分支,与指定的远程分支建立追踪关系
gitbranchtrack[branch][remotebranch] git checkout [branch-name]
切换到上一个分支
gitcheckout git branch –set-upstream [branch] [remote-branch]
合并指定分支到当前分支
gitmerge[branch]commit git cherry-pick [commit]
删除分支
gitbranchd[branchname] git push origin –delete [branch-name]
$ git branch -dr [remote/branch]

标签

列出所有tag
gittagtagcommit git tag [tag]
新建一个tag在指定commit
gittag[tag][commit]tag git tag -d [tag]
删除远程tag
gitpushorigin:refs/tags/[tagName]tag git show [tag]
提交指定tag
gitpush[remote][tag]tag git push [remote] –tags
新建一个分支,指向某个tag
$ git checkout -b [branch] [tag]

查看信息

显示有变更的文件
gitstatus git log
显示commit历史,以及每次commit发生变更的文件
gitlogstat git log -S [keyword]
显示某个commit之后的所有变动,每个commit占据一行
gitlog[tag]HEADpretty=format:commit git log [tag] HEAD –grep feature
显示某个文件的版本历史,包括文件改名
gitlogfollow[file] git whatchanged [file]
显示指定文件相关的每一次diff
gitlogp[file]5 git log -5 –pretty –oneline
显示所有提交过的用户,按提交次数排序
gitshortlogsn git blame [file]
显示暂存区和工作区的差异
gitdiffcommit git diff –cached [file]
显示工作区与当前分支最新commit之间的差异
gitdiffHEAD git diff [first-branch]…[second-branch]
显示今天你写了多少行代码
gitdiffshortstat@0dayago git show [commit]
显示某次提交发生变化的文件
gitshownameonly[commit] git show [commit]:[filename]
显示当前分支的最近几次提交
$ git reflog

远程同步
下载远程仓库的所有变动
gitfetch[remote] git remote -v
显示某个远程仓库的信息
gitremoteshow[remote] git remote add [shortname] [url]
取回远程仓库的变化,并与本地分支合并
gitpull[remote][branch] git push [remote] [branch]
强行推送当前分支到远程仓库,即使有冲突
gitpush[remote]force git push [remote] –all

撤销

恢复暂存区的指定文件到工作区
gitcheckout[file]commit git checkout [commit] [file]
恢复暂存区的所有文件到工作区
gitcheckout.commit git reset [file]
重置暂存区与工作区,与上一次commit保持一致
gitresethardcommit git reset [commit]
重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
gitresethard[commit]HEADcommit git reset –keep [commit]
新建一个commit,用来撤销指定commit
后者的所有变化都将被前者抵消,并且应用到当前分支
gitrevert[commit] git stash
$ git stash pop

其他

生成一个可供发布的压缩包
$ git archive

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值