Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for วิธี sign commit ด้วย GPG บน GitHub
Anuchit Prasertsang
Anuchit Prasertsang

Posted on • Edited on

     

วิธี sign commit ด้วย GPG บน GitHub

ทำไมต้อง sign commit ของเรา

การ sign GPG กับ Git commit เป็นการรับรองว่าโค้ดถูกเปลี่ยนแปลงจากเราจริงๆ (ไม่ใช่คนอื่นแอบอ้างเอาอีเมล์หรือชื่อของเราไป commit โค้ดแทนเรา)ทำให้โค้ดมีความน่าเชื่อถือเพิ่มมากขึ้นและป้องกันการแก้ไขโดยไม่ได้รับอนุญาต พร้อมสามารถตรวจสอบย้อนกลับได้ ทำให้โปรเจคเรามีความน่าเชื่อถือสูงขึ้น อีกทั้งยังเป็นการเพิ่มความปลอดภัยและสร้างความมั่นใจให้คนใช้งานโปรเจคและทีมของเรา

สิ่งที่ต้องเตรียม

วิธี sign commit ด้วย GPG บน GitHub

วิธี sign commit ด้วย GPG บน GitHub

  1. สร้าง GPG key ใหม่
    • gpg --full-generate-key
  2. นำ GPG public key ไปเพิ่มใน GitHub
    • gpg --armor --export EFABXXXXXXXX174
  3. บอก Git ให้ใช้ GPG key ของเรา sign commit
    • git config user.signingkey EFABXXXXXXXX174
  4. ทำการ sign ตอน git commit
    • สั่งsignตอนcommit โดยส่ง-S ตอนcommitgit commit -S -m "message"
    • หรือ
    • ตั้งค่าsignอัตโนมัติgit config commit.gpgsign true เพียงครั้งเดียว หลังจากนั้นgit commit -m "message" ตามปกติ
  5. push ขึ้น GitHub ตามปกติ และเช็คว่า commit ของเรา sign แล้วหรือยัง บน GitHub
    • git push origin maincommit verified

1. สร้าง GPG key ใหม่

gpg--full-generate-key
Enter fullscreen modeExit fullscreen mode

จากนั้นเลือก

  • ประเภทของ key เป็นECC (sign and encrypt) *default* (Elliptic Curve Cryptography)
  • เลือก elliptic curve เป็นCurve 25519 *default*
  • กำหนดระยะเวลาในการใช้งาน key หรือเลือก0 ให้ key ไม่มีวันหมดอายุ
  • กรอกข้อมูลในการสร้าง key เช่นReal name:,Email address:,Comment:
  • ทำการยืนยันการข้อมูลโดยกดO (O)kay
  • กำหนด passphrase ให้กับ key ของเรา (เพื่อป้องกันการเข้าถึง key ของเรา เป็นรหัสผ่าน) แล้วเลือก<OK>

ตัวอย่างผลลัพธ์จากขึ้นตอนด้านบน

gpg (GnuPG) 2.4.5; Copyright (C) 2024 g10 Code GmbHThis is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.Please select what kind of key you want:   (1) RSA and RSA   (2) DSA and Elgamal   (3) DSA (sign only)   (4) RSA (sign only)   (9) ECC (sign and encrypt) *default*  (10) ECC (sign only)  (14) Existing key from cardYour selection?Please select which elliptic curve you want:   (1) Curve 25519 *default*   (4) NIST P-384   (6) Brainpool P-256Your selection?Please specify how long the key should be valid.         0 = key does not expire      <n>  = key expires in n days      <n>w = key expires in n weeks      <n>m = key expires in n months      <n>y = key expires in n yearsKey is valid for? (0)Key does not expire at allIs this correct? (y/N) yGnuPG needs to construct a user ID to identify your key.Real name: Your NameEmail address: your@email.comComment: your comment explanation your keyYou selected this USER-ID:    "Your Name (your comment explanation your key) <your@email.com>"Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O                  ┌─────────────────────────────────────────────────────────────┐                  │ Please enter the passphrase to                              │                  │ protect your new key                                        │                  │                                                             │                  │ Passphrases match.                                          │                  │                                                             │                  │ Passphrase: _______________________________________________ │                  │                                                             │                  │ Repeat: ___________________________________________________ │                  │ ┌─────────────────────────────────────────────────────────┐ │                  │ │                                                         │ │                  │ └─────────────────────────────────────────────────────────┘ │                  │        <OK>                                   <Cancel>      │                  └─────────────────────────────────────────────────────────────┘
Enter fullscreen modeExit fullscreen mode

2. นำ GPG public key ไปเพิ่มใน GitHub

2.1. list GPG keys

gpg--list-secret-keys--keyid-format=long
Enter fullscreen modeExit fullscreen mode
~/.gnupg/pubring.kbx----------------------------------sec   ed25519/EFABA125EBAF174 2024-08-16 [SC]      1E37ABCEFAEF1F8B6697990BEFABA125EBAF174uid                 [ultimate] Your Name (your comment explanation your key) <your@email.com>ssb   cv25519/2A2C03ACFEFDBA9C 2024-08-16 [E]
Enter fullscreen modeExit fullscreen mode

2.2. export GPG key

gpg--armor--export EFABA125EBAF174 | pbcopy
Enter fullscreen modeExit fullscreen mode

บน MacOS ใช้pbcopy คัดลอก public key ไปยัง clipboard ได้เลย
ถ้าไม่มีpbcopy ให้ใช้คำสั่งgpg --armor --export EFABA125EBAF174 แล้วคัดลอก public key ไปยัง GitHub

  1. นำ public key ที่คัดลอกไว้ไปใส่เพิ่มในGitHub GPG Keys โดยกดปุ่มNew GPG key ->Add GPG key

3. บอก Git ให้ใช้ GPG key ของเรา sign commit

ทำการบอก Git ให้ใช้ GPG key ของเรา sign commit โดยใช้คำสั่ง

git config--global user.signingkey EFABA125EBAF174
Enter fullscreen modeExit fullscreen mode

--global คือการบอก Git ให้ใช้ GPG key นี้ sign commit ของเราในทุก repository ในเครื่องของเรา ไม่ว่าจะเป็น GitHub, github หรือ อื่นๆ
ซึ่งอาจจะไม่เหมาะสำหรับเราถ้าเรามีหลาย GPG key หรือต้องการใช้ GPG key ต่างกันใน repository ต่างๆ (บทความนี้จะไม่พูดถึงกรณีนี้)

หากต้องการใช้ GPG key เฉพาะใน repository นั้นๆ ให้cd เข้าไปใน repository นั้นๆ แล้วใช้คำสั่งเดียวกันแต่ไม่ต้องใส่--global

git config commit.gpgsigntruegit config user.signingkey EFABA125EBAF174
Enter fullscreen modeExit fullscreen mode

4. ทำการ sign ตอน git commit

หากเรา config ให้ใช้ GPG key ของเรา sign commit แล้ว ในขึ้นตอนก่อนหน้า
เราสามารถสั่งgit commit -m "message" ได้เหมือนเดิม เพียงเท่านี้ commit ของเราจะถูก sign ด้วย GPG key ของเรา

หรือหากเราไม่ได้ config ให้ใช้ GPG key ของเรา sign commit ดังขึ้นตอนก่อนหน้า
เราสามารถใช้คำสั่งgit commit -S -m "message" ในการ sign commit ด้วย GPG key ของเราเช่นกัน

5. push ขึ้น GitHub ตามปกติ และเช็คว่า commit ของเรา sign แล้วหรือยัง บน GitHub

หลังจากที่เรา push commit ขึ้น GitHub แล้ว เราสามารถเช็คว่า commit ของเรา sign แล้วหรือยัง ได้โดยดูที่หน้า commit บน GitHub ว่ามีเครื่องหมายVerified และGPG key ของเราอยู่ด้านล่างของ commit หรือไม่
commit verified

FAQ:

หากใช้ gnupg2 และ gpg-agent 2.x อย่าลืมตั้งค่าตัวแปรสภาพแวดล้อม GPG_TTY

export GPG_TTY=$(tty)
Enter fullscreen modeExit fullscreen mode

อ้างอิง

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Thailand
  • Joined

More fromAnuchit Prasertsang

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp