该文截取于《沉浸式学Git》
开始使用Git 的时候 ,设置姓名和Email
$ git config --global user.name "Your Name"
$ git config --global user.email "your_email@whatever.com"
创建空目录
$ mkdir hello
$ cd hello
创建仓库
$ git init
添加程序到仓库
$ git add xxxxx.xx // 上传至缓冲区 git add . 添加所有改动的文件
$ git commit -m "First Commit" // 上传至仓库 -m 是加注释的意思
检查仓库、工作的状态
$ git status
查看历史
$ git log
控制显示哪个条目
$ git log --pretty=oneline --max-count=2
$ git log --pretty=oneline --since='5 minutes ago'
$ git log --pretty=oneline --until='5 minutes ago'
$ git log --pretty=oneline --author=<your name>
$ git log --pretty=oneline --all
设置别名
git status 、git add 、git commit 、git checkout 是非常常用的命令,因此对它们进行缩写十分有用。
添加下列内容到你的$HOME 目录的.gitconfig 文件中:
[alias]
co = checkout
ci = commit
st = status
br = branch
hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
type = cat-file -t
dump = cat-file -p
从这开始,可以使用 git hist 获得比较简洁的历史界面,如果重新改过head ,后面加--all
检查 xxxx.xx 文件的内容
$ git checkout <hash> // 切换分支 可以输入hash 也可以输入tag 也可以输入master
$ cat xxxx.xx // 输出文本类容
回到在master 分支中的最新版本
$ git checkout master //master 是默认分支的名称。通过名称检出分支,你能够回到该分支的最新版本。
给版本打标签
$ git tag name
使用tag 命令查看标签
$ git tag
移除标签
$ git tag -d name
重置暂存区
$ git reset HEAD XXXX.xx
创建还原提交
$ git revert HEAD --no-edit
git log 指令看撤销和原始提交, 按q 回车退出
重置分支位置
$ git reset --hard name //如果为远程仓库,容易造成混乱
将 hello.rb 文件移到 lib 目录
$ git mv hello.rb lib
持续更新......