Git

#git #tool

创建

初始化当前工作目录为Git仓库

cd project_dir
git init

仓库

git clone REPO          # 本地仓库名与远程相同
git clone REPO DIR      # 指定本地仓库名

git pull = git fetch + git merge
git pull . BRANCH       # 将指定分支合并到的当前分支

git fetch REPO master:test    # 将远程仓库的master分支抓到本地test分支(下载objects和refs)

配置

git config --global user.name "xxx"
git config --global user.email "xxx@xxx.com"
git config --global core.editor vim

分支

git branch          # 创建分支
git branch -b       # 创建并切换到分支
git branch BRANCH TAG   # 基于TAG创建分支

git checkout        # 切换分支

git branch -d       # 删除已经合并到主干的分支
git branch -D       # (慎用)强制删除分支

git merge BRANCH    # 将BRANCH合并到当前分支

日志

git show ID/TAG/Commit
git log TAG1..TAG2
git log TAG1..
git log --since="2 weeks ago"
git log BRANCH1..BRANCH2        # 不在分支1,但在分支2的日志
git log -p                          # 显示代码变更

比较

git whatchanged -p master..BRANCH       # 分支的区别

提交

git add             # 1、新建blob记录最新修改;2、index file添加blob链接
git commit -m "xxx"

git commit -a = git add + git commit

TAG

git tag TAG ID      # 给commit ID一个别名

搜索

git grep "xxx"

对象

对象类型

blog        # 文件
tree        # 目录
commit      # 提交
tag         # 别名

命令

git cat-file -t ID      # 查看类型
git cat-file TYPE ID    # 查看内容
git ls-tree ID          # 查看目录内容

关系

commit -> tree 
tree 包含 blob/tree

撤销

git reset - FILE        # 从index file中撤销文件
git checkout - FILE     # 撤销对文件的修改(包括找回删除文件)
git checkout */Dir/File # 恢复已删除或修改的文件
git reset

working tree, index file, repo

git add             # working tree -> index file
git commit          # index file -> repo
git commit -a       # working tree -> index file & repo

git diff            # index file -> working tree
git diff --cached    # repo -> index file
git diff HEAD       # repo -> working tree

reset --soft         # 仅撤销commit
reset --mixed        # (默认)撤销commit和index file,保留working tree
reset --hard         # 撤销全部(commit,index file,working tree)

分支表达式

HEAD            # 当前分支的最新提交
HEAD^           # 父母
HEAD^^          # 父母的父母
HEAD~4          # 上溯4代
HEAD^1          # 第1个父母