Git使用简单笔记
准备
git config --global user.name "Your Name"
git config --global user.email "Your e-mail"
git init
git clone /some/existihng/repo /some/new/location
git clone git://example.com:some-repo.git
git clone git@example.com/some-repo.git
Git概念
指定版本
HEAD #最近一个提交
HEAD^ #HEAD之前
HEAD~1 #HEAD之前
HEAD^^ #HEAD早两个
HEAD~2 #HEAD早两个
提交
#添加新文件或暂存,提交
git add <some file>
/*git add -i 交互
git add -p 补丁模式*/
git commit -m "<some message>" //-a提交所有
#取消已暂存但尚未提交的变更的暂存表识
git reset HEAD <some file>
#修复上次提交中的问题
git add <some file>
git commit --amend
#重命名
git mv <old file name> <new file name>
使用分支
#列出已有分支
git branch
git branch -r //列出远程分支
git branch -a //列出所有分支
#创建新分支
git branch new-branch existing
//eg. git branch New_branch master
不加existing 是从当前分支新建分支
git checkout -b alternate master直接从master新建分支,并检出
#重命名
git branch -m <old branch name> <new branch name>
#切换分支
git checkout branch-name
#创建新分支并检出
git checkout -b new-branch existing
#把一条分支<one>合并到<another>
git checkout <another>
git merge<one>
撤销变更
#反转某个提交
git revert <commit>
#取消已暂存但尚未提交的变更的暂存标识
git reset HEAD some/file
#删除工作目录树中的变更
git checkout some/file //不可撤销
#在版本库中撤销已经提交的变更,并在工作目录树中清除,不可撤销
git reset --hard HEAD^
浏览历史
显示历史记录
git log
#显示最近5个
git log -5
#并显示文件变化
git log -5 -p
#显示至少2天前的提交
git log -5 -p --before=="2 days"
显示版本差异
#工作目录树与暂存区的差异
git diff
#暂存区和版本库的差异
git diff --cached
#工作目录树与版本库之间的差异
git diff HEAD
#两个提交之间的差异
git diff <start> <end>
#只显示统计数据
git diff --stat <start> <end>
问责
git blame <some file>
和远程版本库互动
git clone <some repo>
#最近200个
git clone --depth 200 <some repo>
#取来变更(不包含合并到本地分支)
git fetch <remote repo>
#拖入变更(包含合并到本地分支)
git pull <remote repo>
#变更推入
git push <remote repo>
添加与提交
git add -i //交互
git add -p //补丁模式
参考书目:《版本控制之道——使用Git》Travis Swicegood

浙公网安备 33010602011771号