Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Git 進階技巧與最佳實踐:別名、自動補全、著色、Rebase 原則、忽略檔案、區域網共享倉庫

編輯:Linux 教學

基礎配置

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor vim
git config --global color.ui true          # 啟用著色輸出

自動補全腳本

複製 Git 原始碼 contrib/completion/git-completion.bash 至 ~/.git-completion.bash,在 ~/.bashrc 加入:

source ~/.git-completion.bash

即可使用 Tab 自動補全指令、分支、標籤。

實用別名

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.logg "log --pretty=format:'%h - %an - %ad - %s'"

黃金法則:不要 Rebase 已推送至公共倉庫的提交

Rebase 適用於 尚未公開 的本地提交整理歷史。若 Rebase 已公開提交,且他人已基於此開發,將造成歷史分歧、衝突與協作災難。

忽略特定檔案

專案根目錄建立 .gitignore,每行一個模式(支援萬用字元),例如:

*.log
build/
.DS_Store

區域網無 SSH 共享倉庫

# 伺服器端:建立裸倉庫並放至共享目錄
git clone --bare myproject myproject.git
# 放至 NFS 掛載點 /mnt/git/myproject.git

# 客戶端:掛載共享目錄後克隆
git clone /mnt/git/myproject.git
# 或透過 file 協定
git remote add origin file://192.168.x.x/opt/myproject.git
git fetch origin
git merge origin/master
# 或單指令
git pull origin master

其他實用技巧

  • git checkout -b newbranch = 建立並切換分支
  • git reset HEAD~1 撤銷最近提交
  • git reflog 找回丟失提交
  • 配置 --global 針對當前用戶,--system 針對全系統,無參數針對當前專案
Copyright © Windows教程網 All Rights Reserved