Featured image of post Setup Github and get your commit signed With Git and Github CLI

Setup Github and get your commit signed With Git and Github CLI

setup github account with git, and gpg key, in Mac, Linux(debian), WSL 2 and Windows, and sign your commit with GPG key(get verified tag on Github)

Intro

One of the easiest way to connect your Github account with your git installed on Mac or Windows is through Github Desktop. But, we can’t use that on wsl, so we use PAT(personal access token) to resolve this issue!

Steps to setup on Windows 11

  1. install git on Windows
  2. install Github Desktop
  3. Login your Github Desktop with your Github account

install git on Mac/WSL 2/ Linux

  1. Install git(if not already installed)
    • Mac: brew install git
    • Linux/WSL2(debian): sudo apt-get install git
  2. check git version with
1
git --version
  1. setup git global config
1
2
git config --global user.email "[email protected]"
git config --global user.name "Your Name"

install Github CLI

  1. download and install Github CLI following this tutorial. In Ubuntu, you can install it with apt.

  2. login in the Github website

1
gh auth login
  1. test: clone, commit and try to push a github repo
1
2
3
4
git clone <repo url>
git add *
git commit -am "commit name"
git push
  1. output example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$ git add *
$ git commit -am "init"
[master 563c3a8] init
 1 file changed, 3 insertions(+), 16 deletions(-)
$ git push
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 20 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 608 bytes | 101.00 KiB/s, done.
Total 6 (delta 5), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (5/5), completed with 5 local objects.
To https://github.com/ryankert01/blog.git
   af6a1b1..563c3a8  master -> master

Sign your commit with GPG key

github verified tag

  1. Generate a GPG Key:

    Open a terminal and generate a new GPG key pair:

    1
    
    gpg --full-generate-key
    

    Follow the prompts to create your key. Choose the following options when prompted:

    • Select kind of key: (1) RSA and RSA (default)
    • Key size: 4096
    • Key expiration: 0 (never expires)
  2. List Your GPG Keys:

    After generating your key, you can list your keys to get the key ID:

    1
    
    gpg --list-secret-keys --keyid-format LONG
    

    Look for the sec section and you can get the long key ID at the second line.

  3. Copy Your GPG Key:

    Use the long key ID to copy your GPG key:

    1
    
    gpg --armor --export <your-long-key-id> > my-gpg-key.asc
    
  4. add GPG key to Github account

    Use the File Path with GitHub CLI:

    Now, you can use the file path with GitHub CLI:

    1
    
    gh gpg-key add my-gpg-key.asc
    

    Yes, you can use GitHub CLI (gh) to simplify the process of adding your GPG key to GitHub. Here are the steps to do so:

  5. Configure Git to Sign Your Commits:

    Configure Git to use your GPG key for signing commits:

    1
    2
    
    git config --global user.signingkey <your-long-key-id>
    git config --global commit.gpgSign true
    
  6. Make and Sign a Commit:

    Make a new commit and push it to GitHub. The commit should be signed automatically if you enabled commit signing by default:

    1
    2
    
    git commit -m "Your commit message"
    git push
    

    To manually sign a commit, use the -S flag:

    1
    
    git commit -S -m "Your commit message"
    

These steps leverage GitHub CLI to streamline the process of adding your GPG key to GitHub and configuring Git to sign your commits.

Reference

Git GPG Setup for macOS

Licensed under CC BY-NC-SA 4.0
RyanKert
Built with Hugo
Theme Stack designed by Jimmy