Git騷操作

顧重發表於2018-02-09

以下快捷操作配合 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
複製程式碼

相關文章