幾個你不知道的 Git 小命令- 卻收穫快樂。

本文是對原文的翻譯 + 並在原文基礎上自我實踐,進行了大量的補充。

Git Commands You Didn't Know[1]

前言

關於 Git, 我最喜歡的它的原因之一就是它既簡單又可自定義,alias 功能就是其中的代表。Git 支持alias,這意味着你可以給命令自定義名字。當然,我更喜歡爲很長的命令設置別名 (alias),避免每次需要他們的時候,我要花時間去搜索它們。

別名 (alias) 最好設置成你最習慣的語義化方式,畢竟工具只是工具,是幫助我們提高效率的,打一長串命令,沒有必要。:)

Git 中的別名 (alias) 配置規則是這樣的。

git config --global alias.[new_alias] "[previous_git_command]"

# Example
git config --global alias.save commit

從上面的示例中,我將不再需要 git commit,我更習慣用 git save。

如果命令是多個,則需要用引號包住多個選項。

git recommit

git config --global alias.recommit 'commit --amend -m'

git commit --amend 允許你更改最後的提交信息 (message)。recommit命令讓提交變得更簡單,更容易記住。

# Change the last commit message with recommit
git recommit "New commit message"

# [master 64175390] New commit message
#  Date: Tue Sep 22 15:09:11 2020 +0000
#  1 file changed, 2 insertions(+)
#  create mode 100644 vue.js

git commend

git config --global alias.commend 'commit --amend --no-edit'

使用--no-edit標誌進行修改,可以在最近一次提交時在倉庫中提交新的更改,你不需要再次重複提交消息。

我來解釋一下這個命令,你是否有這種經歷,寫完代碼了 git add .git commit xxx ,一頓操作,剛想push 的時候發現 有個文件漏改了,又是 git add .git commit xxx 一頓操作,此時 commit 就會有兩次記錄,這對於項目來說是非常不好的,一次 commit 被分成了兩次,如果遇到需要revert 你就傻眼了。這個時候你就可以用這個命令輕鬆解決。

代碼演示

echo 'Hello world' > README.md
git add .
git commit -m "Hello Word"
git log --oneline
4b39c8a (HEAD -> master) Add README.md
echo 'Hello 秋風' >> README.md
git commend
git log --oneline 
60c5190 (HEAD -> master) Add README.md

此時git log依然只有一次記錄。

git config --global alias.search 'grep'

# Example
git search [search_term]

git grep允許你在存儲庫中搜索關鍵字 (且支持正則),並返回各種匹配項。這很酷,但是我不知道 grep 的意思,請告訴我是否這樣做。我更喜歡search,它易於記住並且易於使用。

git search createHotContext

git here

git config --global alias.here '!git init && git add . && git commit -m "init 🦄"'

通常,當我初始化一個新的倉庫時,我將暫存所有文件,並使用初始提交消息進行提交。我使用git here一步就完成了(這對於開源工具重度愛好者,真的是福星,太爽了,誰用誰知道)。只需在要創建新倉庫的文件夾中運行它,就可以了。

小技巧: 像我在公司開發代碼需要提交到公司的私有倉庫,因此全局配置了公司的 username 和 email,當我切換到開源項目的時候,老是會忘記修改回來,因此我會創建一個 git config user.name xxx \n git config user.email xxx@xx.com 的一個 sh 文件。因此我初始化的時候可以這樣 。

git config --global alias.here '!git init && sh ~/my/git.sh && git add . && git commit -m "init 🦄"'

這樣子,既可以提交到私有倉庫,創建開源項目的時候又不耽誤。

也有人說,我不改也能提交啊,=。= 爲啥要改?那是你不知道強迫症.... 看到這種灰色頭像的提交真的是心裏焦灼。

git who

git config --global alias.who 'blame'

# Example
git who index.ts
# 641753902 (Ephraim Atta-Duncan 2020-09-22 15:09:11 +0000 1)
# 641753902 (Ephraim Atta-Duncan 2020-09-22 15:09:11 +0000 2) console.log("who?")

git blame 用於逐行檢查文件的內容,並查看每行的最後修改時間以及修改的作者。如果有錯誤,你可以追溯到某一行的改動是誰修改的。vscode 插件 GitLens也是基於此原理。

總結: 追蹤 bug 小能手,以後誰寫出 bug,輕鬆定位某一行是誰寫的。

git zip

git config --global alias.zip 'archive --format=tar.gz -o repo.tar.gz'

# Example
git zip [branch_name]

使用 archive命令可以創建整個或部分倉庫的 tarballzipgit zip 更容易記住。只需添加分支名稱。

➜  git-test2 git:(master) ls
README.md
➜  git-test2 git:(master) git zip master
➜  git-test2 git:(master) ✗ ls
README.md   repo.tar.gz

git newbie

git config --global alias.newbie 'checkout --orphan'

# Example
git newbie [new_branch_name]

帶有--orphan 標誌的git checkout允許您創建一個分支,而沒有來自父分支的任何歷史記錄。

➜  git-test2 git:(master) git newbie pages
Switched to a new branch 'pages'
➜  git-test2 git:(pages) ✗ ls
README.md
➜  git-test2 git:(pages) ✗ git log
fatal: your current branch 'pages' does not have any commits yet
➜  git-test2 git:(pages) ✗

實踐

那麼它的應用場景是什麼呢?

還記得github pages 嗎?利用他能快速創建站點,可以用某個分支來當做站點展示,但是如果我們把源碼和打包後的文件都放在一個分支,就會顯得累贅與混亂,這個時候我們就可以利用這個特性來創建一個全新無 commit 的分支。兩個工程(一個源文件工程,一個打包後的工程)放到同一個倉庫 (repo) 中。

代碼演示

➜  git-test2 git:(master) git newbie pages
Switched to a new branch 'pages'
➜  git-test2 git:(pages) ✗ ls
README.md
➜  git-test2 git:(pages) ✗ git log
fatal: your current branch 'pages' does not have any commits yet
➜  git-test2 git:(pages) ✗ git st
On branch pages

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

 new file:   README.md

➜  git-test2 git:(pages) ✗

git clonely

git config --global alias.clonely 'clone --single-branch --branch'

# Example
git clonely [branch_name] [remote_url]

git clonely v3 https://github.com/vuejs/vue-apollo
# Cloning into 'vue-apollo'...
# remote: Enumerating objects: 2841, done.
# remote: Total 2841 (delta 0), reused 0 (delta 0), pack-reused 2841
# Receiving objects: 100% (2841/2841), 1.92 MiB | 330.00 KiB/s, done.
# Resolving deltas: 100% (1743/1743), done.

帶有--single-branch --branch標誌的git clone允許你從存儲庫中clone特定分支,毫不誇張的說,這個命令我在 Google 中搜索了 10 多次。別名(alias)更好用,更好記憶~

能幹嘛呢?

當然是減少clone時間,這對大倉庫而言簡直是福星。

git plg

git config --global alias.plg "log --graph --pretty=format:'%C(yellow)%h%Creset -%Cred%d%Creset %s %Cgreen| %cr %C(bold blue)| %an%Creset' --abbrev-commit --date=relative"

# Example
git plg # plg - Pretty Log

git log沒什麼問題,除了它有點醜陋,沒有顏色差異,如果要自定義它,我們需要在 google 上查詢相關的命令。幸運的是,我們有別名 (alias)。使用該命令的別名,你將獲得非常漂亮的日誌。

git fresh

git config --global alias.fresh "filter-branch --prune-empty --subdirectory-filter"

# Example
git fresh [subfolder] [branch_name]
git fresh src main # Don't do this unless you know what you are doing

通過一系列參數,使用fresh命令用於從子文件夾中創建新的存儲庫。帶有多個參數的 filter-branch獲取指定子文件夾的內容,並將其中的內容替換爲該子文件夾的內容。

實踐

假設有這樣一個項目,目錄結構如下

.
├── script
│   └── index.js
├── README.md

如果我們需要改造項目,將 script 作爲單獨的項目, 這個時候我們需要將 script 拆出來,我們一般會通過拷貝來解決,這樣做沒有什麼問題,但是你將丟失script目錄以及子文件所有歷史修改記錄。

現在我們成功將 script 目錄拆成了單獨的項目。

再來看 commit 記錄,依舊保留了script 的相關commit記錄,對於管理項目來說非常有幫助。

commit 8b311558195684d6420baedce74e0f9951208038 (HEAD -> master)
Author: qiufeng <qiufeng@163.com>
Date:   Thu Oct 1 22:37:21 2020 +0800

    feat: script
(END)

如果我們不小心拆分錯了,還可以進行還原。

git reset --hard refs/original/refs/heads/{branch_name}

還可以繼續拆分, 這個時候拆分需要先清除一下備份~

git update-ref -d refs/original/refs/heads/master

然後從頭開始繼續操作即可~

最後

將此添加到你的 .gitconfig 文件。

[alias]
    recommit = commit --amend -m
    commend = commit --amend --no-edit
    here = !git init && git add . && git commit -m \"Initialized a new repository\"
    search = grep
    who = blame
    zip = archive --format=tar.gz -o ../repo.tar.gz
    lonely = clone --single-branch --branch
    plg = log --graph --pretty=format:'%C(yellow)%h%Creset -%Cred%d%Creset %s %Cgreen| %cr %C(bold blue)| %an%Creset' --abbrev-commit --date=relative
    fresh = filter-branch --prune-empty --subdirectory-filter

參考資料

[1]

Git Commands You Didn't Know: https://dev.to/dephraiim/git-commands-you-probably-didn-t-know-3bcm?utm_source=digest_mailer&utm_medium=email&utm_campaign=digest_email

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/TeAN4f3FFMj63JSRtf_X-g