基础命令
1. git --version 查看机器上安装的git的版本
2. git config 配置git的配置文件, git配置级别主要有以下3类:
git config --local 仓库级别, 优先级最高,仓库级别对应的配置文件是当前仓库下的.git/config
git config --global 用户级别,优先级次之, 用户级别对应的配置文件是用户宿主目录下的~/.gitconfig
git config --system 系统级别, 优先级最低, 系统级别对应的配置文件是git安装目录下的 /etc/gitconfig
查看仓库配置
git config [--local|--global|--system] -l
添加仓库配置 (以用户名和邮箱以作为标识)
git config --global user.name "your name"
git config --global user.emial "your email"
3. git checkout / revert / reset
4. git branch and merge
Git Branching - Basic Branching and Merging
5. git stash and clean
How to extract a single file or diff a file from a git stash ?
$ git diff stash@{0}^1 stash@{0} -- <filename>
Explanation: A stash is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HEAD when the stash was created. So you can treat stash (e.g. stash@{0}
is first / topmost stash) as a merge commit, and use stash@{0}^1
means the first parent of the given stash, which as stated in the explanation above is the commit at which changes were stashed away. Likewise, you can use git checkout to check a single file out of the stash:
$ git checkout stash@{0} -- <filename>
6. 可视化git提交历史和时间线
git log --graph
git log --graph --oneline
7. 更新.gitignore
.gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的, 需要
git rm -r --cached .
git add .
git commit -m 'update .gitignore'
8. git diff
用git diff 来比较项目中任意两个版本的差异
# 显示当前工作目录与上次提交之间的所有差别
git diff HEAD
# 显示当前工作目录下的lib目录与上次提交之间的差别
git diff HEAD -- ./lib
# 显示当前工作目录与另外一个分支(e.g., dev)的差别
git diff dev
# 显示两个分支间的差异
git diff master..test
9. git stash
# 缓存本地未提交的改变
git stash
# 列出所有stashes
git stash list
# 恢复某一个stash
git stash apply # 最近的一个
git stash apply stash@{1} # 指定的一个
# 删除一个stash
git stash drop # 最近的一个
git stash drop stash@{2} # 指定的一个
git stash clear # 所有的