配置git
git config --global user.name fcj
git config --global user.email fcj@test.com
git config --global color.ui true
git config --list
vim ~/.gitconfig
创建repository
git init
git clone https://github.com/kennethreitz/requests.git
添加提交文件
git status
git add code.py
git commit -m 'init commit'
git三个环境:working directory(工作区)、staging area(cache或index)、history
查看git状态
git忽略文件:.gitignore
git status -s
[][]表示staging area和working directory的改变
[][M]本地修改没add
[M][]add后没有commit
[M][M]上次add没有commit,且本地已再次修改
git add:从working directory到staging area
git commit:从staging area到history
查看文件差别
git diff:比较working directory和staging area
git diff --staged:比较staging area和history
git diff HEAD:比较working directory和history
简化git diff信息:git diff --stat
撤销误操作
git reset:从history恢复到staging area
git checkout:从staging area恢复到working directory
git checkout HEAD:从history恢复到working directory、且恢复staging area
git commit -am 'add commit':从working directory同时add到staging area,并提交到history:
working directory和staging area:git add <===> git diff <===> git checkout
staging area和history:git commit <===> git diff --staged <===> git reset
history和working directory:git commit -am <===> git diff HEAD <===> git checkout HEAD
删除和重命名文件
git rm old.py:从本地和staging area删除文件
git rm --cached code.py:从staging area删除文件,但保留本地文件
git mv code.py code.go:重命名
等价于:
git rm --cached code.py
mv code.py code.go
git add code.go
暂存工作区
git stash:最新修改放入抽屉
git stash list:查看抽屉内容
git stash pop:从抽屉中恢复
commit对象
git log
git log --oneline
三种对象:commit、tree、blob
每个对象都有哈希值
git cat-file -t HEAD
git cat-file -p HEAD
git cat-file -t 对象哈希:查看对象类型
git cat-file -p 对象哈希:查看对象内容
tree-ish表达式
git rev-parse HEAD
git rev-parse HEAD~
git rev-parse HEAD~4
git rev-parse master
git rev-parse master~
git rev-parse master~4:定位到commit
git cat-file -p master~4
git rev-parse master~4^{tree}:定位到commit下的tree
git rev-parse HEAD~4:code.py:定位到文件
git cat-file -p HEAD~4:code.py
git show HEAD~4:code.py
git show <===> git cat-file -p
创建和删除分支
git branch:列出所有分支
git branch tryidea:创建分支
git checkout tryidea:切换分支
git checkout master:切换回master
git branch -d tryidea:删除分支
合并分支
git checkout -b tryidea:创建并立即切换分支
分支提交后必须合并才可以删除
git checkout master
git merge tryidea
Fast-forward
git branch -d tryidea
3-way merge:master也发生提交时