VOOZH about

URL: https://thenewstack.io/how-to-sign-git-commits-with-an-ssh-key/

⇱ How to Sign git Commits with an SSH key - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-09-17 06:00:12
How to Sign git Commits with an SSH key
tutorial,
Security / Software Development

How to Sign git Commits with an SSH key

How can a maintainer of an open source project assure that the project's code hasn't been hacked? By signing the code with SSH. Here's how.
Sep 17th, 2022 6:00am by Jack Wallen
👁 Featued image for: How to Sign git Commits with an SSH key
Feature image by Ian Wilson via Pixabay 

Development projects can get very busy. They can also grow to massive proportions. When projects do expand (especially those of an open-source nature), they take on more and more developers. That’s great… until it’s not.

For example, what happens if a rogue developer hops onto a project and adds a commit that injects malicious software into the code? No project wants to have to deal with such an issue.

To that end, projects must be very careful who they allow in. But what happens if someone sneaks under the radar? Maybe a rogue developer gains access to your project and adds a commit that injects malicious code and does so under the guise of another developer. When you see that developer’s name associated with the commit, you let it pass, assuming the code is solid and there’s no need to worry.

Famous last words.

Little do you know, that rogue developer just trashed your project and unless you do catch it, you could wind up with a massive problem on your hands.

What can you do to avoid this?

There are many things. You could constantly keep tabs on every line of code to make sure it’s worthy of the project and safe. Of course, you want to trust your developers and might not have time to comb through every single line of code for malicious intent.

If that describes your project, what can you do?

One thing is to enforce Secure Shell (SSH) signing for commits, which uses public-key cryptography to create a signature for yourself that can’t be forged.

A-ha! You’ve been hunting high and low for such a solution.

By using signing in SSH commits, you can more easily verify that each commit was submitted by a legitimate developer and not an impostor.

I’m here to show you how this is done.

Requirements

To use SSH keys in git commits, you’ll need the git version control software installed and an SSH key. I’ll show you how to do both and will demonstrate on Ubuntu Linux 22.04. If you’re working on a different operating system, make sure to alter the installation steps accordingly.

Ready? Let’s do this.

Installing Git

The first thing we’ll do is install git. For that, log in to your Ubuntu instance, open a terminal window, and issue the command:

sudo apt-get install git -y

Huzzah!

Create Your SSH Key

Next, you must generate an SSH key that will be used. For this, issue the command:

ssh-keygen

Answer the questions and make sure to type/verify a strong and unique password for the key. That key will be housed in your ~/.ssh directory by default. That’s fine as git can work with that.

Initialize a Local Repository

Next, we must initialize a local repository. Create a folder for this with the command:

mkdir ~/PROJECT

You can name that directory whatever you like.

Change into the new directory with:

cd ~/PROJECT

Initialize the empty repository with:

git init

Configure Git

We now need to configure git to know who we are. This is accomplished with the following two commands:

git config --global user.email "EMAIL"
git config --global user.name "NAME"

Where EMAIL is your email address and NAME is your full name.

Our next configuration tells git we want to enable GPG singing and the format will be SSH. To do that, issue the following two commands:

git config --global commit.gpgsign true
git config --global gpg.format ssh

Next, list your SSH key(s) with:

ssh-add -L

You should see something like this listed:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCUlMZBvT363Qpg1PM9LzJHwP/ijaPD+0O7l24TchGoqVItu9N6PsnXigVT21VTNemltSfChS6A2qDPMsXKbeiK7It9tMcTDyeBJd2e9rdlzQ05cHQ65jerVIe9pyc/asbX0jtlm0+VcB0fim3gVHlVcR8a6DWrqijU5YXYt3jUzdleHv9XmRJANTRAOpPICTnkO4G9HyNgJeI9M2baFgSwiNohVFHG8KJI1rXM64ryr7Psgr/5DihYSbFfTinI731ZpA3qogIaUlyKDAP7t8Xt7ntSeIzsSfd8A8jiGeYdme+MyUBo/2ENb0UmKYI695Cf6Ua+DGR39j8kt2jGw3rgkkA3L9i2+v+6aHeMzzdgR1FFIsXHs3pfDO05+u1CUgclujamsJdreEbuEwLFHQGQ60m3it7J0T/JGRUcgvQeIukslCyFzz2Rj3j0aSsyVTeVTj7f3rZyWZJlSTVyFv5uMpfm62YzA5hPCPrMJg7XHpUbP4VkWkeTfAalTTtYz34= jack@docker1

Copy that entire string.

If you get an error after the ssh-add command, you might need to first run the command:

eval "$(ssh-agent)"

We can now set our signing key with the command:

git config --global user.signingkey "KEY"

Where KEY is the entire string you copied above.

You can then confirm the SSH signing has been set up correctly with the command:

git commit --allow-empty --message="Did SSH signing work?"

For our next trick, we need to configure the SignersFile with the command:

git config --global gpg.ssh.allowedSignersFile ~/.ssh/allowed_signers

Create the allowed signers file with:

touch ~/.ssh/allowed_signers

Finally, we have to populate that new file with our key using the command:

echo "EMAIL ssh-rsa KEY" > ~/.ssh/allowed_signers

Where EMAIL is your email address and KEY is the key you copied earlier.

How to Verify It’s Working

Now that everything is set up, let’s make sure everything works with the command:

git show --show-signature

The output should look something like this:

commit efb59f739f141f29b4c63d9c43edc7f46243ea47 (HEAD -> master)
Good "git" signature for EMAIL with RSA key SHA256:YgBNmIqR3Z2ff7XoVKptRkZffw6nHC5mDKF8g6AcjVQ
Author: Jack Wallen <EMAIL>
Date:   Thu Sep 15 16:53:44 2022 +0000

Testing SSH signing

Where EMAIL is the email address associated with your SSH key.

Congratulations, you now have SSH signing set up for your git commits. Everyone will know that it’s really you submitting to the project.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Famous.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.