uncategorized

Git 常用场景和指令

head

最近几年的工作,零零碎碎也积累了一些 git 的常用操作

本文统一做一个归纳,方便以后的查阅或给予他人帮助

或许一些常用的对比操作、清理维护操作,可以抽象成一个工具,方便可视化使用 👍🏻

本地分支维护

清理已经合入的分支

1
git branch --merged master | grep -v '^[ *]*master$' | xargs git branch -d

Or if you just wanna do dry-run (see what’s going to happen), remove | xargs git branch -d part

Show git log as one line (And find my own commit)

1
git log --pretty=format:"%h - %an - %s" | grep -E "myName|我的名字"

Compare two branches

1
2
3
4
5
# find commits that in `branch_b` but not in `branch_a`
git log branch_a..branch_b

# btw, by the similar logic, list commits that in branch_b but not in branch_a
git log branch_b ^branch_a

To check whether there are unresolved conflicts

(although brutally search <<< / >>> works too)

1
git diff --check

Just create new branch without checkout

1
2
3
4
5
6
7
8
9
# This is what I usually do, 
# checkout to target branch, and `checkout -b` from that branch to new one
git checkout -b <nb-name>

# create branch from current branch, without checkout
git branch <nb-name>

# copy<clone> branch from branch_a to branch_b
git branch -c branch_a branch_b

搜索历史 commit 中的内容

HEAD UP: repo with tons of commits may suffer performance issue

1
git grep "content" $(git rev-list --all)
Share