<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Alex Ochoa</title>
    <description>The latest articles on Forem by Alex Ochoa (@mrtrom).</description>
    <link>https://forem.com/mrtrom</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1481378%2F905a8d52-0b17-4974-9b3a-af2910ca5e42.png</url>
      <title>Forem: Alex Ochoa</title>
      <link>https://forem.com/mrtrom</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mrtrom"/>
    <language>en</language>
    <item>
      <title>Git Config: A Beginner's Guide with Advanced Tips</title>
      <dc:creator>Alex Ochoa</dc:creator>
      <pubDate>Sun, 12 May 2024 06:00:00 +0000</pubDate>
      <link>https://forem.com/mrtrom/git-config-a-beginners-guide-with-advanced-tips-393f</link>
      <guid>https://forem.com/mrtrom/git-config-a-beginners-guide-with-advanced-tips-393f</guid>
      <description>&lt;h2&gt;
  
  
  Let's Get Started with Git!
&lt;/h2&gt;

&lt;p&gt;So, you're ready to dive into the world of Git? Excellent choice! Git is like your trusty assistant, keeping your project organized while you and your team work together seamlessly.&lt;/p&gt;

&lt;p&gt;We'll be covering the following topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Installing Git&lt;/li&gt;
&lt;li&gt;
Configuring Git

&lt;ul&gt;
&lt;li&gt;Identity&lt;/li&gt;
&lt;li&gt;Text Editor&lt;/li&gt;
&lt;li&gt;Signing Commits&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Commands Cheat Sheet&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Let's dig in!&lt;/p&gt;




&lt;h2&gt;
  
  
  Installing Git
&lt;/h2&gt;

&lt;p&gt;First things first, let's get Git installed on your machine. Head over to the &lt;a href="https://git-scm.com/downloads" rel="noopener noreferrer"&gt;official Git website&lt;/a&gt; and grab the latest version.&lt;/p&gt;

&lt;h3&gt;
  
  
  MacOS:
&lt;/h3&gt;

&lt;p&gt;If you're on a Mac, you probably already have Git installed, but just in case, here's how you can do it with &lt;a href="https://brew.sh/" rel="noopener noreferrer"&gt;brew&lt;/a&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

brew update &lt;span class="c"&gt;# Ensure everything's up to date&lt;/span&gt;
brew &lt;span class="nb"&gt;install &lt;/span&gt;git &lt;span class="c"&gt;# Install Git&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Linux (Debian/Ubuntu/WSL on Windows)
&lt;/h3&gt;

&lt;p&gt;For Linux users, installing Git is straightforward. Just run:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

apt-get &lt;span class="nb"&gt;install &lt;/span&gt;git


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For other Linux distributions, refer to the &lt;a href="https://git-scm.com/download/linux" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Verifying the Installation
&lt;/h3&gt;

&lt;p&gt;Once Git is installed, let's double-check to make sure it's working. Open your terminal and type:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git version &lt;span class="c"&gt;# Check Git's version&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you see something like this, you're good to go!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdhj5wfgsw71v87uoepoy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdhj5wfgsw71v87uoepoy.png" alt="Git Version Output"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Configuring Git
&lt;/h2&gt;

&lt;p&gt;Now that Git is on your machine, let's configure it to suit your preferences.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure your identity
&lt;/h3&gt;

&lt;p&gt;Let's start by telling Git who you are:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.name &lt;span class="s2"&gt;"Your Name"&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.email &lt;span class="s2"&gt;"your-email@example.com"&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.username &lt;span class="s2"&gt;"yourusername"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Choosing your text editor
&lt;/h3&gt;

&lt;p&gt;Next, let's set your preferred text editor for Git to use:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git config &lt;span class="nt"&gt;--global&lt;/span&gt; core.editor &lt;span class="s2"&gt;"your-editor"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I like to use &lt;a href="https://zed.dev/" rel="noopener noreferrer"&gt;zed&lt;/a&gt; for this given its speed, to use zed you can run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git config &lt;span class="nt"&gt;--global&lt;/span&gt; core.editor &lt;span class="s2"&gt;"zed"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For VSCode you can run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git config &lt;span class="nt"&gt;--global&lt;/span&gt; core.editor &lt;span class="s2"&gt;"code -w"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Signing your commits
&lt;/h3&gt;

&lt;p&gt;As you've become adept at navigating Git, you may now be considering enhancing your workflow by signing your commits for added security and authenticity. Traditionally, Git has relied on GPG (GNU Privacy Guard) for commit signing. However, configuring and understanding GPG can present challenges, especially for those new to the tool.&lt;/p&gt;

&lt;p&gt;Thankfully, there's a new solution. OpenSSH has introduced a feature allowing the signing of data using existing SSH keys and Git has seamlessly integrated this capability as an alternative to GPG.&lt;/p&gt;

&lt;p&gt;You can take a look at the official Github documentation &lt;a href="https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Signing Git Commits with SSH
&lt;/h4&gt;

&lt;p&gt;(Make sure to remove the &lt;code&gt;--global&lt;/code&gt; parameter if you only want to do this for a specific repository)&lt;/p&gt;

&lt;p&gt;First we need to switch the signature format to SSH:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git config &lt;span class="nt"&gt;--global&lt;/span&gt; gpg.format ssh


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You also need to tell Git which SSH key to use for signing. You can just use your public key for &lt;code&gt;user.signingKey&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.signingKey ~/.ssh/id_rsa.pub


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can now create signed commits:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git commit &lt;span class="nt"&gt;-S&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s1"&gt;'Create a signed commit'&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want to force all future commits to be signed you need to add:&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git config &lt;span class="nt"&gt;--global&lt;/span&gt; commit.gpgsign &lt;span class="nb"&gt;true&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you're using Github make sure you have added your Public Key to both "Authentication Keys" and "Signing Keys" in the &lt;a href="https://github.com/settings/keys" rel="noopener noreferrer"&gt;settings page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsum6879d0q9wikljfg3i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsum6879d0q9wikljfg3i.png" alt="Github Settings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Verifying the signatures
&lt;/h4&gt;

&lt;p&gt;To show and validate the signatures, you can run &lt;code&gt;git log&lt;/code&gt; with the &lt;code&gt;--show-signature&lt;/code&gt; parameter.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git log &lt;span class="nt"&gt;--show-signature&lt;/span&gt; &lt;span class="nt"&gt;--oneline&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34bbaqe6862aylxyq7vl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34bbaqe6862aylxyq7vl.png" alt="Git Log"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sometimes, as you embark on signing your commits, you may encounter an error message within the commit information. As is the case on this scenario with the error:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

error: gpg.ssh.allowedSignersFile needs to be configured and exist &lt;span class="k"&gt;for &lt;/span&gt;ssh signature verification


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This error stems from Git's lack of knowledge regarding which SSH keys to trust for signature verification. Git relies on a predefined list of trusted keys known as the "allowed signers file," which operates similarly to the "authorized keys file" utilized by SSH.&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating the Allowed Signers File
&lt;/h4&gt;

&lt;p&gt;To address this issue, begin by creating a file on your system (e.g., ~/.config/git/allowed_signers) to manage trusted signing keys. Each entry in this file corresponds to an email address used in committing, followed by the associated public key. For instance, you can do so running:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="c"&gt;# Create allowed_signers file&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;git config user.email&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; ~/.ssh/id_rsa.pub&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; ~/.config/git/allowed_signers
&lt;span class="c"&gt;# Add allowedSignersFile configuration&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; gpg.ssh.allowedSignersFile &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HOME&lt;/span&gt;&lt;span class="s2"&gt;/.config/git/allowed_signers"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And now the signature can be verified!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft9uuftbg78fgeh2xgxqq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft9uuftbg78fgeh2xgxqq.png" alt="Good Git Log"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Checking your configuration
&lt;/h3&gt;

&lt;p&gt;To confirm that everything is set up correctly, run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git config &lt;span class="nt"&gt;--list&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Expecting an output like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flfvlopds6yo9xdlfo8rh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flfvlopds6yo9xdlfo8rh.png" alt="Git Config Output"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations! You've successfully configured Git and you're ready to start coding and collaborating like a pro.&lt;/p&gt;

&lt;p&gt;Remember, Git is a powerful tool with many more features to explore. Happy coding!&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick Commands Cheat Sheet:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  MacOs:
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="c"&gt;# Ensure everything's up to date&lt;/span&gt;
brew update
&lt;span class="c"&gt;# Install Git&lt;/span&gt;
brew &lt;span class="nb"&gt;install &lt;/span&gt;git
&lt;span class="c"&gt;# Basic Configuration&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.name &lt;span class="s2"&gt;"Your Name"&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.email &lt;span class="s2"&gt;"your-email@example.com"&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.username &lt;span class="s2"&gt;"yourusername"&lt;/span&gt;
&lt;span class="c"&gt;# Signing configuration&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; gpg.format ssh
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.signingKey ~/.ssh/id_rsa.pub
git log &lt;span class="nt"&gt;--show-signature&lt;/span&gt; &lt;span class="nt"&gt;--oneline&lt;/span&gt;
&lt;span class="c"&gt;# Check your Git configuration&lt;/span&gt;
git config &lt;span class="nt"&gt;--list&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Linux:
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="c"&gt;# Make sure package list is updated&lt;/span&gt;
apt-get update
&lt;span class="c"&gt;# Install Git&lt;/span&gt;
apt-get &lt;span class="nb"&gt;install &lt;/span&gt;git
&lt;span class="c"&gt;# Basic configuration&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.name &lt;span class="s2"&gt;"Your Name"&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.email &lt;span class="s2"&gt;"your-email@example.com"&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.username &lt;span class="s2"&gt;"yourusername"&lt;/span&gt;
&lt;span class="c"&gt;# Signing configuration&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; gpg.format ssh
git config &lt;span class="nt"&gt;--global&lt;/span&gt; user.signingKey ~/.ssh/id_rsa.pub
git log &lt;span class="nt"&gt;--show-signature&lt;/span&gt; &lt;span class="nt"&gt;--oneline&lt;/span&gt;
&lt;span class="c"&gt;# Check your Git configuration&lt;/span&gt;
git config &lt;span class="nt"&gt;--list&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Allowed Signers File
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="c"&gt;# Create allowed_signers file&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;git config user.email&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; ~/.ssh/id_rsa.pub&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; ~/.config/git/allowed_signers
&lt;span class="c"&gt;# Add allowedSignersFile configuration&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; gpg.ssh.allowedSignersFile &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HOME&lt;/span&gt;&lt;span class="s2"&gt;/.config/git/allowed_signers"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
