您可能已經為Git 配置加入了一些設置,但以下這些設置您可能尚未配置。如果您尚未設置這些設置,那麼您正在進行不必要的繁瑣手動操作。
git config --global pull.rebase true
每次不使用 rebase 就進行拉取操作時,Git 都會建立一個合併提交。如果團隊每天都這樣做幾次,你的 Git 日誌就會變成一堆亂七八糟的「將分支 'main' 合併到 main」的條目,這些條目毫無意義。而使用 rebase,你的提交會始終與最新更改保持同步,你的程式碼歷史記錄也會像一條清晰的時間軸一樣呈現。
額外提示:我經常這樣做, g pull -r origin main ( g是我的 shell 中git的別名)來保持我的分支與主分支同步。你也可以加入
git config --global branch.main.rebase true
現在我只用g pull origin main 。雖然少了兩個角色,但至少少了一件要考慮的事。
git config --global push.autoSetupRemote true
你建立了一個新分支,完成了工作,推送了程式碼,然後 Git 卻報錯了:
fatal: The current branch my-branch has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin my-branch
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
每次都這樣。這個設定能徹底解決這個問題。 Git 會在你第一次推送程式碼到新分支時自動設定上游。
git config --global fetch.prune true
過時的遠程分支會悄無聲息地堆積起來。有人幾週前就合併並刪除了他們的分支,但當你執行git branch -r時,本地仍然會顯示它。每次執行 git fetch 指令時,都會清除這些無效的引用,使你的分支清單反映遠端實際存在的內容。
我在 shell 中使用了這個別名來修剪本地和遠端分支。
rmmerged() {
git branch --merged | grep -Ev "(\*|master|main)" | xargs -n 1 git branch -d && git remote prune origin
}
現在,由於 fetch prune 設定可以處理遠端分支,因此只需修剪本機分支即可。
rmmerged() {
git branch --merged | grep -Ev "(\*|master|main)" | xargs -n 1 git branch -d
}
git config --global diff.algorithm histogram
預設的差異比較演算法雖然也能用,但當程式碼中存在大量結構相似的行時(例如重複的 return 語句、右大括號或空白行),直方圖演算法能產生更清晰的差異結果。預設演算法有時會搞不清楚哪些相同的行需要匹配,導致產生的差異結果中增刪操作交錯,難以理解。直方圖演算法則能更好地處理這種情況。文件越大、結構越重複,這種改進就越明顯。這是一個即插即用的升級,沒有任何缺點。
這裡舉個虛構的例子,因為我很難在自己最近的提交中找到一個能反映兩者差異的好例子。
diff --git a/tmp/example_before.js b/tmp/example_after.js
index 30d9ab3c..8ec95ef5 100644
--- a/tmp/example_before.js
+++ b/tmp/example_after.js
@@ -8,15 +8,10 @@ function validateUser(user) {
if (!user.name) {
return { error: 'Name is required' };
}
- return { valid: true };
-}
-
-function processData(data) {
- const result = transform(data);
- if (!result) {
- return { error: 'Transform failed' };
+ if (!user.id) {
+ return { error: 'ID is required' };
}
- return result;
+ return { valid: true };
}
function validateProduct(product) {
@@ -26,13 +21,28 @@ function validateProduct(product) {
if (!product.price) {
return { error: 'Price is required' };
}
+ if (!product.name) {
+ return { error: 'Name is required' };
+ }
return { valid: true };
}
+function processData(data) {
+ const result = transform(data);
+ if (!result) {
+ return { error: 'Transform failed' };
+ }
+ return result;
+}
+
function saveToDatabase(item) {
const connection = getConnection();
if (!connection) {
return { error: 'Database connection failed' };
}
+ const validated = validateItem(item);
+ if (!validated) {
+ return { error: 'Validation failed' };
+ }
return connection.save(item);
}
diff --git a/tmp/example_before.js b/tmp/example_after.js
index 30d9ab3c..8ec95ef5 100644
--- a/tmp/example_before.js
+++ b/tmp/example_after.js
@@ -8,6 +8,22 @@ function validateUser(user) {
if (!user.name) {
return { error: 'Name is required' };
}
+ if (!user.id) {
+ return { error: 'ID is required' };
+ }
+ return { valid: true };
+}
+
+function validateProduct(product) {
+ if (!product) {
+ return { error: 'Product is required' };
+ }
+ if (!product.price) {
+ return { error: 'Price is required' };
+ }
+ if (!product.name) {
+ return { error: 'Name is required' };
+ }
return { valid: true };
}
@@ -19,20 +35,14 @@ function processData(data) {
return result;
}
-function validateProduct(product) {
- if (!product) {
- return { error: 'Product is required' };
- }
- if (!product.price) {
- return { error: 'Price is required' };
- }
- return { valid: true };
-}
-
function saveToDatabase(item) {
const connection = getConnection();
if (!connection) {
return { error: 'Database connection failed' };
}
+ const validated = validateItem(item);
+ if (!validated) {
+ return { error: 'Validation failed' };
+ }
return connection.save(item);
}
git config --global rerere.enabled true
Rerere代表「重複使用已記錄的解決方案」。當你解決合併衝突時,Git 會記住你的解決方法。下次遇到同樣的衝突時,Git 會自動套用你先前的解決方案。如果你曾經對一個長期存在的分支進行變基操作,並且必須反覆解決同樣的衝突,那麼 Rerere 就是你的理想解決方案。它不會在你無法審查的情況下悄悄合併程式碼。它會記錄你的解決方案並在下次需要時重播,這樣你就無需重複相同的工作。
想看看你目前設定了哪些內容嗎?執行git config --global --list ,看看還缺什麼。
如果你喜歡這類技巧,我有一個電子報,網址是 OneTipAWeek.com 。每週一條開發者技巧,簡短實用。就是這樣!
如果你想和我保持聯繫,我的所有社交帳號都在nickyt.online 。
期待下次!
原文出處:https://dev.to/nickytonline/five-git-config-settings-every-dev-needs-3e55