95 คะแนน โดย xguru 2025-02-26 | 7 ความคิดเห็น | แชร์ทาง WhatsApp
  • Scott Chacon ผู้เขียน "Pro Git" อธิบายการตั้งค่า Git บางอย่างที่เขาเปิดใช้แบบ global และเหตุผลที่ทำเช่นนั้น
  • หลายการตั้งค่านี้เป็นสิ่งที่เขาเรียนรู้มาจากนักพัฒนาที่ทำงานกับโค้ดเบส Git core โดยตรง
  • ด้านล่างคือการตั้งค่า ~/.gitconfig ที่ทำให้ Git ใช้งานได้ดีขึ้น
    [column]  
    ui = auto  
    [branch]  
    sort = -committerdate  
    [tag]  
    sort = version:refname  
    [init]  
    defaultBranch = main  
    [diff]  
    algorithm = histogram  
    colorMoved = plain  
    mnemonicPrefix = true  
    renames = true  
    [push]  
    default = simple  
    autoSetupRemote = true  
    followTags = true  
    [fetch]  
    prune = true  
    pruneTags = true  
    all = true  
    
    # มีเหตุผลอะไรที่จะไม่ใช้ล่ะ?  
    
    [help]  
    autocorrect = prompt  
    [commit]  
    verbose = true  
    [rerere]  
    enabled = true  
    autoupdate = true  
    [core]  
    excludesfile = ~/.gitignore  
    [rebase]  
    autoSquash = true  
    autoStash = true  
    updateRefs = true  
    
    # การตั้งค่าตามความชอบส่วนตัว (ยกเลิกคอมเมนต์เมื่อจำเป็นแล้วใช้งาน)  
    
    [core]  
    # fsmonitor = true  
    # untrackedCache = true  
    [merge]  
    # (หากเวอร์ชัน Git ต่ำกว่า 2.3 ให้ใช้ ‘diff3’)  
    # conflictstyle = zdiff3  
    [pull]  
    # rebase = true  
    

นักพัฒนา Git core ตั้งค่า Git กันอย่างไร?

  • ใน mailing list ของ Git เคยมีครั้งหนึ่งที่ Felipe Contreras เสนอให้ทีม core ลบการตั้งค่าและ alias ทั้งหมด แล้วลองใช้ Git แบบค่าเริ่มต้น
  • ผลของการทดลองนั้นคือมีการเสนอการตั้งค่า 9 รายการและ alias 3 รายการให้เป็นค่าเริ่มต้นใหม่
    merge.conflictstyle = zdiff3  
    rebase.autosquash = true  
    rebase.autostash = true   
    commit.verbose = true  
    diff.colorMoved = true  
    diff.algorithm = histogram  
    grep.patternType = perl  
    feature.experimental = true  
    branch.sort = committerdate  
    
  • การตั้งค่าเหล่านี้ยังไม่ได้ถูกรับไปใช้เป็นค่าเริ่มต้น
  • แต่สิ่งที่น่าสนใจคือ นักพัฒนา Git จำนวนมากกลับใช้งาน Git ได้ลำบากหากไม่เปิดบางตัวในรายการนี้
  • และที่น่าสนใจกว่านั้นคือ คนส่วนใหญ่ในพวกคุณอาจไม่รู้เลยว่าสิ่งเหล่านี้หมายถึงอะไร
  • จะอธิบายโดยแบ่งเป็น 3 หมวดหมู่
    • สิ่งที่ทำให้ Git ดีขึ้นอย่างชัดเจน (Clearly Makes Git Better)
    • มีเหตุผลอะไรที่จะไม่ใช้ล่ะ? (Why the Hell Not?)
    • เรื่องของรสนิยม (A Matter of Taste)

# สิ่งที่ทำให้ Git ดีขึ้นอย่างชัดเจน

การจัดเรียงรายการ branch

  • โดยปกติ Git จะเรียง branch ตามตัวอักษร แต่การเรียงตามวันที่ commit ล่าสุดอาจมีประโยชน์มากกว่า
  • สามารถตั้งค่าให้เรียง branch ตาม commit ล่าสุด และแสดงผลแบบคอลัมน์ได้ดังนี้
    git config --global column.ui auto  
    git config --global branch.sort -committerdate  
    

การจัดเรียงรายการ tag

  • หากต้องการเรียง tag ตามเวอร์ชันแทนการเรียงตามตัวอักษร ให้ใช้การตั้งค่านี้
    git config --global tag.sort version:refname  
    

การตั้งชื่อ default branch

  • หากต้องการกำหนดชื่อ default branch ตอนสร้าง repository ใหม่ ให้ตั้งค่าดังนี้
    git config --global init.defaultBranch main  
    

การตั้งค่า diff ที่ดีขึ้น

  • เปลี่ยนอัลกอริทึม diff เริ่มต้นเป็น histogram เพื่อให้เปรียบเทียบได้แม่นยำขึ้น
  • หากต้องการตรวจจับการย้ายโค้ดและแสดงสี ให้เพิ่มการตั้งค่าดังนี้
    git config --global diff.algorithm histogram  
    git config --global diff.colorMoved plain  
    git config --global diff.mnemonicPrefix true  
    git config --global diff.renames true  
    

การตั้งค่า push ที่ดีขึ้น

  • สามารถปรับพฤติกรรมของ push ให้ดีขึ้นได้ด้วยการตั้งค่าต่อไปนี้
    git config --global push.default simple  
    git config --global push.autoSetupRemote true  
    git config --global push.followTags true  
    

การตั้งค่า fetch ที่ดีขึ้น

  • หากต้องการลบ branch และ tag ที่ไม่จำเป็นโดยอัตโนมัติระหว่าง fetch ให้ใช้การตั้งค่าดังนี้
    git config --global fetch.prune true  
    git config --global fetch.pruneTags true  
    git config --global fetch.all true  
    

# มีเหตุผลอะไรที่จะไม่ใช้ล่ะ?

พรอมต์แก้คำสั่งอัตโนมัติ

  • หากต้องการให้ระบบตรวจจับคำสั่งพิมพ์ผิดและเสนอคำแนะนำ ให้ใช้ดังนี้
    git config --global help.autocorrect prompt  
    

แสดง diff ตอน commit

  • หากต้องการให้แสดงเนื้อหาที่เปลี่ยนไปพร้อมกันตอนเขียนข้อความ commit ให้เพิ่มการตั้งค่านี้
    git config --global commit.verbose true  
    

ใช้การแก้ conflict เดิมซ้ำ

  • หากต้องการนำวิธีแก้ conflict ครั้งก่อนกลับมาใช้ซ้ำโดยอัตโนมัติ ให้ใช้การตั้งค่านี้
    git config --global rerere.enabled true  
    git config --global rerere.autoupdate true  
    

ตั้งค่าไฟล์ .gitignore แบบ global

  • หากต้องการระบุไฟล์ที่ต้องการ ignore แบบ global ให้ตั้งค่าดังนี้
    git config --global core.excludesfile ~/.gitignore  
    

ปรับปรุงการตั้งค่า rebase

  • หากต้องการให้ทำ squash และ stash อัตโนมัติระหว่าง rebase ให้ใช้การตั้งค่าต่อไปนี้
    git config --global rebase.autoSquash true  
    git config --global rebase.autoStash true  
    git config --global rebase.updateRefs true  
    

# เรื่องของรสนิยม

การแสดง merge conflict ที่ดีขึ้น

  • หากต้องการให้แสดงเวอร์ชันฐานร่วมด้วยเมื่อเกิด merge conflict ให้พิจารณาการตั้งค่านี้
    git config --global merge.conflictstyle zdiff3  
    

ใช้ rebase ตอน pull

  • หากต้องการให้ git pull ทำ rebase โดยอัตโนมัติ ให้ตั้งค่าดังนี้
    git config --global pull.rebase true  
    

เพิ่มความเร็วในการตรวจจับการเปลี่ยนแปลงของไฟล์

  • หากต้องการเพิ่มประสิทธิภาพของ git status เป็นต้น สามารถใช้การตั้งค่าต่อไปนี้ได้
    git config --global core.fsmonitor true  
    git config --global core.untrackedCache true  
    

# สรุป

  • การตั้งค่าเหล่านี้ช่วยให้ใช้งาน Git ได้สะดวกขึ้น และบางรายการก็เป็นการตั้งค่าที่นักพัฒนา core ใช้งานอย่างจริงจัง
  • การปรับแต่งการตั้งค่า Git ให้เหมาะสมสามารถช่วยปรับปรุง workflow และทำให้ใช้งานได้มีประสิทธิภาพมากขึ้น

7 ความคิดเห็น

 
brainypooh 2025-02-28

"สิ่งที่น่าสนใจกว่านั้นคือ คนส่วนใหญ่ในพวกคุณไม่รู้เลยด้วยซ้ำว่านี่หมายความว่าอะไร" แทงใจมากเลยครับ/ค่ะ ฮือๆๆ

 
tested 2025-02-27

ไม่ใช่ --global แต่เป็น -global เหรอ?

 
xguru 2025-02-27

--global ถูกต้องครับ มีความคลาดเคลื่อนตอนคัดลอกวาง ตอนนี้แก้ไขแล้วครับ

 
ilikeall 2025-02-26

การนำการแก้ไข conflict กลับมาใช้ซ้ำนี่ดีมาก

 
tujuc 2025-02-26

เวลา diff ผมใช้ git-delta เพื่อดูในรูปแบบ TUI อยู่ครับ

  10   │ [core]  
  11   │     pager = delta  
  12   │ [interactive]  
  13   │     diffFilter = delta --color-only  
  14   │ [delta]  
  15   │     line-numbers = true  
  16   │     side-by-side = true  
  17   │     navigate = true  
  18   │     diff-so-fancy = true  
  19   │     hyperlinks = true  

ถ้าขี้เกียจไปหมดทุกอย่างก็ใช้ tig ... ฮ่าๆๆๆ
มีอะไรที่ดีกว่านี้อีกไหมครับ...?

 
GN⁺ 2025-02-26

ความเห็นจาก Hacker News

  • alias ที่ฉันชอบที่สุดคือ git out ซึ่งจะแสดงรายการ commit ทั้งหมดที่ยังไม่ได้ push ใช้ตลอด
    [alias]   
      out = "log @{u}.."   
    
  • ระหว่างที่หลายคนกำลังขบคิดเรื่อง ~/.gitconfig ของตัวเอง ขอแนะนำ delta อย่างมากให้เป็นคู่หูของ git CLI
  • ~/.gitconfig ของฉันมีดังนี้
    [alias]  
      co = checkout  
      ci = commit  
      st = status  
      br = branch  
      hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short  
      type = cat-file -t  
      dump = cat-file -p  
      dft = difftool  
    [tag]  
      sort = version:refname  
    [tar "tar.xz"]  
      command = xz -c  
    [tar "tar.zst"]  
      command = zstd -T0 -c  
    [log]  
      date = iso-local  
    [pull]  
      ff = only  
    [diff]  
      tool = difftastic  
    [safe]  
      directory = *  
    [advice]  
      detachedHead = false  
    [init]  
      defaultBranch = master  
    
  • สงสัยว่าทำไมการเซ็น commit ถึงไม่ได้รวมอยู่ในการตั้งค่านี้ ทั้งที่ทำได้ง่ายด้วย SSH key แบบสมัยใหม่
    [user]  
      name = xyz  
      email = xyz@domain.com  
      signingkey = ~/.ssh/id_algorithm.pub  
    
    [commit]  
      gpgsign = true  
    [tag]  
      gpgsign = true  
    
    [gpg]  
      format = ssh  
    
    # restrict allowed signers  
    # echo "$(git config --get user.email) namespaces=\"git\" $(cat ~/.ssh/id_*.pub)" >> ~/.git_allowed_signers  
    [gpg "ssh"]  
      allowedSignersFile = ~/git_allowed_signers  
    
    • บน GitHub สามารถเพิ่ม SSH key สำหรับทั้งการยืนยันตัวตนและการเซ็นได้ ต้องเพิ่มสองครั้ง แต่พอตั้งค่าแล้วก็เอาป้าย "unverified" ออกจาก commit ได้
  • ตัวเลือกการตั้งค่า Git ยอดนิยมของ Julia Evans
  • ตัวเลือกเพิ่มเติมที่ฉันชอบ
    [apply]  
      # Remove trailing whitespaces  
      whitespace = fix  
    [color "diff"]  
      whitespace = red reverse  
    [diff]  
      colorMovedWS = allow-indentation-change  
    [format]  
      pretty = fuller  
    [log]  
      date = iso  
    [pull]  
      ff = only  
    
  • การตั้งค่าของฉันอยู่ที่นี่ โดยพื้นฐานแล้วตั้งไว้แบบนี้อยู่แล้ว (ยกเว้น column UI) น่าจะเป็นเพราะเคยอ่านบทความดี ๆ ของ Scott และคนอื่น ๆ
  • เห็นด้วยกับคำแนะนำให้ใช้ (z)diff3 บทความนี้ประเมินความสำคัญของมันต่ำเกินไป diff แบบสามทางช่วยให้แก้ conflict ที่สไตล์เริ่มต้นทำไม่ได้
  • เพิ่งค้นพบว่าสามารถตั้งค่าให้ git ใช้ pager ที่ชอบได้ เลยตั้งเป็น bat