Git骚操作

862 阅读2分钟

以下快捷操作配合 oh-my-zsh + git plugin 一并食用,效率更佳

Git Aliases

整理功能分支冗余commit记录 -> rebase

# git checkout
gco <branch-name>

# git rebase -i commit-hash
grbi $(g merge-base $(current_branch)  master)

# Commands:
# p, pick = 使用commit 包括commit message
# r, reword = 使用commit, 但是会弹出交互式界面要求你重新编辑该条commit message
# e, edit = 使用commit, 但是会中断rebase,使用 git commit --amend 编辑该条commit message; 使用 git rebase --continue 继续
# s, squash = 使用commit, 但是会弹出交互式界面要求你重新编辑该条commit message,会同时显示上一条commit message
# f, fixup = 将此commit合并入上个commit, 并且抛弃commit message
# x, exec = 执行之后的命令, 如果执行失败则中断rebase, 使用git rebase --continue 继续, 会抛弃该条commit!
# d, drop = 抛弃该条commit

键入 i 开始修改,修改 commit-hash 前置命令完成后 键入 esc -> : -> w -> q -> enter 保存退出即可

整理功能分支冗余commit记录 -> reset

gco <branch-name>

glol

复制回退的第一个功能提交之前的 commit-hash

# 千万不要加--hard,会把工作区的记录一并清掉!!
git reset <commit-hash>

# git add --all
gaa

# git commit -m
gcmsg 'commit message'

合并master commit至功能分支

# git rebase master
grbm

强制推送本地分支至远程同名分支

# git push --force origin $(current_branch)
ggf

推送本地分支至远程(远程未创建该分支)

# git push --set-upstream origin $(current_branch)
gpsup

cherry-pick -> 单 commit

gco <origin-branch-name>

# git log --graph --pretty = format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
glol

复制想要的commit-hash

gco <target-branch-name>

# git cherry-pick
gcp <commit-hash>

cherry-pick -> 多 commit

# 左开右闭,不包含start-commit-id
gcp <start-commit-id>..<end-commit-id>

# 闭区间,包含start-commit-id
gcp <start-commit-id>^..<end-commit-id>

将当前commit修改合并至上一个commit

git commit -a -amend

# git commit -v -a -s --no-edit --amend
gcan!

查看操作记录(仅限本地)

git reflog

拉取master代码

# git pull --rebase
gup

删除已合并分支

# git branch --merged | command grep -vE "^(*|\smaster\s$)" | command xargs -n 1 git branch -d
gbda