Git是个很有用的工具…太方便了…
Git常用指令整理
初始化一个新仓库
git init
提交当前目录的新文件和被修改文件到索引
git add .
查看当前目录状态(未被管理和被git管理但被修改但还未提交的文件)
git status
提交保存仓库历史记录
git commit -m "commit message"
显示工作版本和HEAD的差别
git diff HEAD
查看提交日志
git log
回溯历史版本 这里[hashcode]为你要回溯到的commit hash值
git reset --hard [hashcode]
clone一个项目
git clone [url]
分支操作
创建并切换分支
git checkout -b [newbranch]
切换分支
git checkout [branch]
创建分支
git branch [newbranch]
查看所有分支(本地和远程分支)
git branch -a
查看本地分支
git branch
查看远程分支
git branch -r
删除分支
git branch -d [branch]
合并branch分支到当前分支 --no-ff参数避免"快进式合并"
git merge --no-ff [branch]
展示已合并到当前分支的分支
git branch --merge
展示未合并到当前分支的分支
git branch --no-merge
远程仓库与远程分支管理
建立指向url的远程仓库命名为[shortname]
git remote add [shortname] [url]
查看本地已配置的远程仓库
git remote -v
查看[shortname]远程仓库详细信息
git remote show [shortname]
从远程获取最新版本到本地
git fetch
将本地分支[b1]的更新推送到远程主机[shortname]分支[b2]
git push [shortname] [b1]:[b2]
移除远程仓库[shortname]
git remote rm [shortname]
标签
获取仓库内所有tag
git tag
指令详解
按时间正序查看日志(最后一次commit最后出现)
git log --reverse
查看完整的版本变迁树
git log --graph --all
显示最近两次提交
git log -p
显示添加删除的行数
git log --stat
格式化输出日志
git log --pretty=format:"commit: %H%nparent commit: %P%ncommitter: %cn<%ce>%ncommit date: %cd%nauthor: %an<%ae>%nauthor date: %ad" -1
—pretty=format:””表示给出我们自己定义的输出格式
-1 代表输出最近一次 commit 的 log
%H 表示 commit 的完整哈希字串
%P 表示父 commit 的完整哈希字串
%cn 表示提交者的姓名
%an 表示作者的姓名
%ce 表示提交者的email
%ae 表示作者的email
%cd 表示提交日期
%ad 表示作者的修订日期
%n 表示换行
git log --committer="xxx@gmail.com" --pretty=format:"%H %cd %s"
–committer 指定提交者
%s 表示提交信息
查询[file]文件的每一行的 owner
输出格式为
commit ID (代码提交者 最早提交日期 代码位于文件中的行数) 实际代码
git blame [file]
查询该仓库当前 HEAD 的位置
git show HEAD