บทความ

ให้ git จำรหัสผ่านยังไง ไม่ต้องพิมพ์ใหม่ทุกครั้ง

ให้ Git Credential Manager หรือ Keychain เก็บ token แทนเรา หรือเปลี่ยนไปใช้ SSH key ส่วน credential.helper store เขียน token ลงไฟล์แบบอ่านออกได้ เก็บไว้ใช้เฉพาะเครื่องที่ไม่มีทางอื่น

เผยแพร่
อัปเดต
— แนะนำวิธีเก็บ token ที่ปลอดภัยกว่า store
เวลาอ่าน
3 นาที
แท็ก
#Tools

เวลาเรา git pull หรือ git push กับ repository ที่ clone มาด้วย URL แบบ https:// git จะถามชื่อผู้ใช้กับรหัสผ่านทุกครั้ง ถ้าอยากให้ git จำไว้ ต้องตั้ง credential helper ซึ่งก็คือโปรแกรมที่ git เรียกไปเก็บและดึงรหัสให้

ทางที่ใช้กันมาตลอดคือ git config --global credential.helper store สั้นและใช้ได้ทุกเครื่อง แต่ git เขียนรหัสลงไฟล์เป็นข้อความธรรมดา ใครเปิดไฟล์ได้ก็เห็นรหัส เลยขอเริ่มจากวิธีที่ปลอดภัยกว่าก่อน แล้วค่อยกลับมาที่ store ตอนท้าย

GitHub ไม่รับรหัสผ่านบัญชีแล้ว ต้องใช้ token

ตั้งแต่ 13 สิงหาคม 2021 GitHub เลิกรับรหัสผ่านบัญชีตอนสั่ง git ตรงช่อง Password ที่ git ถาม เราต้องใส่ personal access token แทน (เอกสารของ GitHub) GitLab ก็มี personal access token ให้ใช้แบบเดียวกัน

ถ้าใช้ Git Credential Manager หรือ GitHub CLI ตามหัวข้อถัดไป เราไม่ต้องสร้าง token เอง เครื่องมือจะเปิดเบราว์เซอร์ให้ login แล้วเก็บ token ให้

ให้ระบบปฏิบัติการเก็บ token แทนไฟล์ข้อความ

แต่ละระบบมีที่เก็บรหัสที่เข้ารหัสไว้อยู่แล้ว เราแค่บอก git ให้ไปใช้ ก่อนตั้งค่า ลองดูก่อนว่าตอนนี้ git ใช้ helper ตัวไหนอยู่

git config --show-origin --get-all credential.helper

Windows ใช้ Git Credential Manager ที่มากับ Git for Windows

Git Credential Manager (GCM) มากับ Git for Windows อยู่แล้ว และตอนติดตั้ง ตัวเลือก credential helper ก็ตั้งเป็น GCM ไว้ให้ (วิธีติดตั้ง GCM) ครั้งแรกที่ push จะมีหน้าต่างให้ login แล้ว GCM เก็บ token ไว้ใน Windows Credential Manager

ถ้าคำสั่งข้างบนแสดง manager แปลว่าใช้ GCM อยู่แล้ว ถ้ายังไม่ใช่ ตั้งเองได้

git config --global credential.helper manager

macOS ใช้ Keychain

git ที่มากับ Xcode Command Line Tools ตั้ง helper เป็น osxkeychain ไว้ให้แล้ว รหัสที่ใส่ครั้งแรกจะไปอยู่ใน Keychain ถ้าลง git จาก Homebrew แล้วยังไม่มี helper สั่งเองได้

git config --global credential.helper osxkeychain

หรือจะลง GCM ผ่าน Homebrew ก็ได้ GCM เก็บ token ใน Keychain เหมือนกัน และพา login GitHub ผ่านเบราว์เซอร์ให้ด้วย

brew install --cask git-credential-manager

WSL ใช้ GCM ฝั่ง Windows ร่วมกัน

ถ้าเครื่อง Windows ลง Git for Windows ไว้แล้ว ให้ git ใน WSL เรียก GCM ฝั่ง Windows ได้เลย token จะไปอยู่ใน Windows Credential Manager ที่เดียวกัน (วิธีตั้งจากเอกสารของ GCM) โฟลเดอร์ของ GCM เปลี่ยนไปตามรุ่นของ Git for Windows (mingw64, ucrt64 หรือ clangarm64) เลยต้องหาก่อนว่าอยู่ตรงไหน

ls /mnt/c/Program\ Files/Git/*/bin/git-credential-manager.exe

แล้วเอา path ที่เจอมาตั้งเป็น helper เช่น

git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/bin/git-credential-manager.exe"

Linux ที่มีหน้าจอ desktop

ลง GCM ตามเอกสารการติดตั้ง แล้วให้เก็บใน Secret Service ซึ่งเป็น keyring ของ GNOME หรือ KDE (ตัวเลือกที่เก็บรหัสของ GCM)

git-credential-manager configure
git config --global credential.credentialStore secretservice

Server ที่ไม่มีหน้าจอ ให้จำไว้แค่ในหน่วยความจำ

helper cache จำรหัสไว้ในหน่วยความจำ ไม่เขียนลงดิสก์ ค่าเริ่มต้นจำไว้ 900 วินาที (15 นาที) ตั้งให้นานขึ้นได้ (เอกสารของ git)

git config --global credential.helper 'cache --timeout=3600'

พอครบเวลาหรือรีสตาร์ทเครื่อง git ก็ถามใหม่ แต่ไม่มี token วางทิ้งไว้ในไฟล์

ให้ GitHub CLI login ให้

ถ้าใช้แต่ GitHub ลง GitHub CLI แล้วสั่ง

gh auth login

เลือก HTTPS แล้วตอบ Y ตอนถามว่าจะให้ git ใช้ credential ของ GitHub ไหม gh จะตั้งตัวเองเป็น helper ให้ (เอกสารของ GitHub)

ใช้ SSH key แทน ไม่ต้องมีรหัสให้จำเลย

อีกทางคือเลิกใช้ HTTPS แล้ว clone ผ่าน SSH เราสร้าง key คู่หนึ่ง เอา public key ไปใส่ใน GitHub ที่ Settings → SSH and GPG keys แล้วเปลี่ยน remote เป็น URL แบบ [email protected]:…

ssh-keygen -t ed25519 -C "[email protected]"
cat ~/.ssh/id_ed25519.pub
git remote set-url origin [email protected]:your-name/your-repo.git
ssh -T [email protected]

ขั้นตอนเต็ม รวมถึงให้ ssh-agent จำ passphrase ของ key อยู่ในเอกสารของ GitHub

credential.helper store ใช้ง่าย แต่เก็บ token เป็นไฟล์ข้อความธรรมดา

วิธีเดิมของบทความนี้คือ

git config --global credential.helper store
git pull

ใส่ชื่อผู้ใช้กับ token ครั้งแรก แล้ว git จะเขียนลงไฟล์ ~/.git-credentials ครั้งต่อไปก็ไม่ถามอีก ข้างในไฟล์หน้าตาประมาณนี้

https://your-name:[email protected]

ถ้าเปลี่ยนรหัสหรือ token หมดอายุ คำสั่ง git จะไม่ผ่าน แล้ว git ลบรหัสเก่าออกจาก ~/.git-credentials ให้เอง สั่งใหม่แล้วใส่ token ใหม่ git ก็จำตัวใหม่แทน

เคยใช้ store มาก่อน ต้องลบไฟล์เก่าด้วย

ตั้ง helper ตัวใหม่แล้ว ไฟล์ ~/.git-credentials ยังอยู่ที่เดิม token ข้างในก็ยังอ่านออก ล้าง helper เดิมแล้วลบไฟล์ทิ้ง จากนั้นค่อยตั้ง helper ตัวใหม่ตามหัวข้อข้างบน

git config --global --unset-all credential.helper
rm ~/.git-credentials

ถ้าไม่แน่ใจว่าไฟล์นี้เคยหลุดไปที่ไหนหรือเปล่า เข้าไป revoke token ตัวนั้นใน GitHub แล้วสร้างใหม่ไปเลย

อ่านต่อ

ล่าสุด

ดูทั้งหมด →