Git已經成為了現代軟件開發的標配,然而,大多數人可能只是使用了其中一小部分功能,往往忽略了一些有用的功能和技巧。在這篇文章中,我們將介紹一些有用的Git技巧和最佳實踐,這些技巧可以幫助你更高效地使用Git,使你的代碼管理更順暢。
一、使用Git別名
使用Git別名可以幫助你節省時間和擊鍵,可以讓常用的Git命令瞬間變得更簡潔。例如,將“git status”命令替換為“git s”命令。
git config --global alias.s status
那麼之後,你就可以使用“git s”取代“git status”了。
除了“git s”,還有很多別名可以使用,例如:
git config --global alias.a add
git config --global alias.b branch
git config --global alias.c commit
git config --global alias.co checkout
git config --global alias.d diff
git config --global alias.l log
git config --global alias.p push
git config --global alias.price '!git remote update -p; git status -uno'
通過使用別名,你可以使用更短的命令行來執行相同的Git操作。
二、使用Git配置文件
為了讓你的Git工作流更加高效,你可以創建一個Git配置文件,它可以包含你的別名、設置等信息,從而避免在每次執行Git命令時都需要手動輸入這些設置。
在Linux、macOS和Unix中,你可以創建一個名為“~/.gitconfig”的文件,並將以下內容添加到文件中:
[alias]
s = status
a = add
b = branch
c = commit
co = checkout
d = diff
l = log
p = push
price = !git remote update -p; git status -uno
[core]
editor = nano
[user]
name = Your Name
email = youremail@example.com
[diff]
tool = meld
如果你使用Windows,你可以在Git Bash中執行以下命令來創建一個名為“gitconfig”的文件並添加相同的內容。
echo "
[alias]
s = status
a = add
b = branch
c = commit
co = checkout
d = diff
l = log
p = push
price = !git remote update -p; git status -uno
[core]
editor = nano
[user]
name = Your Name
email = youremail@example.com
[diff]
tool = meld
" > ~/.gitconfig
這裡的“core.editor”設置了你的默認Git編輯器,這裡將其設置為了“nano”。另外,“user.name”和“user.email”是在Git中顯示你的用戶名和電子郵件地址,它們默認顯示為你安裝Git時所提供的信息。最後,“diff.tool”指定了Git默認使用的比較工具是meld。你可以根據自己的喜好調整這些設置。
三、使用Git Hooks
Git鉤子(Git Hooks)是一些腳本,它們在Git操作的不同階段自動執行。例如,在提交代碼時,可以使用Git pre-commit鉤子驗證代碼是否符合要求。在這個階段,程序員可以通過編寫鉤子來自動檢查代碼質量,或者在代碼提交前格式化代碼。
Git Hook有兩種類型:客戶端鉤子(Client-Side Hooks)和服務器端鉤子(Server-Side Hooks)。客戶端鉤子針對本地存儲庫,服務器端鉤子針對遠程存儲庫。
這裡介紹一些常用的Git Hooks:
1. Git pre-commit鉤子
在提交前運行的Hook,用於確保即將提交的代碼符合要求。
#!/bin/sh
# pre-commit hook
# Runs test suite and checks coding standards before allowing a commit to go through.
exec << /dev/null
case $(git diff --cached --name-only --diff-filter=ACM) in
*.php)
# run PHP syntax check
docker run --rm --interactive --tty --volume "$(pwd)":/app php:7.4-cli php -l {} \; || exit 1
# run PHP Code sniffer
docker run --rm --interactive --tty --volume "$(pwd)":/app phpqa/php-cs-fixer fix --rules=@Symfony --show-progress=estimating --verbose --ansi {} \; || exit 1
;;
*.css)
# run CSS LINT
docker run --rm --interactive --tty --volume "$(pwd)":/app node:10.23-alpine yarn --silent stylelint {}
;;
*)
;;
esac
exit 0
2. Git pre-push鉤子
在push代碼前運行的Hook,用於確保即將推送的代碼符合要求。
#!/bin/sh
# pre-push hook
# Runs test suite and checks coding standards before allowing a push to go through.
exec << /dev/null
# Run PHP Unit
docker run --rm --interactive --tty --volume "$(pwd)":/app php:7.4-cli vendor/bin/phpunit \; || exit 1
# run Behat test suite
docker-compose -f docker-compose.service.yml -f docker-compose.override.yml run --rm php ./vendor/bin/behat --no-interaction || exit 1
exit 0
四、其他Git技巧
1. 使用Git Autocomplete
Git命令補全可以幫助你更快地輸入命令並避免拼寫錯誤。在Linux或Unix中,可以使用以下命令啟用命令補全功能:
source /usr/share/bash-completion/completions/git
對於macOS用戶,可以在.bash_profile文件中添加以下行:
if [ -f /usr/local/etc/bash_completion.d/git-completion.bash ]; then
. /usr/local/etc/bash_completion.d/git-completion.bash
fi
2. Git Log中的–pretty選項
使用–pretty選項可以更好地顯示提交日誌,使其更易於閱讀。在Git中有幾種內置的日誌格式可供選擇,例如,要使用Git默認的日誌格式:
git log --pretty=oneline
你可以根據自己的需求使用其他的日誌格式,例如:
git log --pretty=format:'%h - %s (%cr) [%an]'
這將以短哈希值、提交信息、相對時間和作者的名稱的格式進行顯示。
3. Git Alias和外部命令的結合使用
除了使用別名外,你還可以將Git命令結合外部命令來使用。例如,你可以使用Git Alias將“git loc”定義為“git log –pretty=format:”%C(auto)%h%d %s %Cgreen(%an, %ar)%Creset””,然後將這個命令與grep一起使用,來篩選某個關鍵詞。
git loc | grep "keyword"
4. 使用Git Stash
當你需要切換分支或者合併代碼時,Git Stash可以幫助你暫存目前工作區中的更改。使用Git Stash命令,可以將當前所有未提交的更改保存到一個臨時的隱藏存儲區中。
要將更改存儲到Git Stash中,請執行以下命令:
git stash
然後你可以自由的修改,切換分支和進行其他操作。在需要恢復之前的變更時可以使用以下命令:
git stash apply
至此你的更改即重新被引用到當前分支。
5. 使用Git Reflog
當你想撤銷某個Git命令時,如果在命令時後沒有指定分支標籤等信息,那麼很有可能會丟失之前的更改。 Git Reflog可以記錄每一個操作Git的步驟,包括將游標移動到哪裡、合併的提交ID、拉命令、合併命令以及重置等操作。
如果你想查看你歷史的Git操作,在Git中使用Git Reflog命令:
git reflog
總結
作為一個軟件開發者,熟練掌握Git是非常重要的。通過使用Git的別名、配置文件、Hooks和其他有用的技巧,你可以讓自己的Git工作流更加高效和流暢。在這篇文章中,我們分享了Git一些實用的技巧,對於經常使用Git的新手和有經驗的開發者都非常有用。通過掌握這些技巧,您可以使Git管理更加流暢,並使開發工作更加高效。
原創文章,作者:MZFY,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/135114.html