Ver código fonte

Git SSH Command

codeskraps 6 meses atrás
pai
commit
a6c3812c8a

+ 100 - 0
content/posts/git_ssh_command.md

@@ -0,0 +1,100 @@
++++
+title = 'Configuring SSH Keys for Individual Git Commands'
+date = 2024-10-02T16:55:53+02:00
+draft = true
++++
+When working with Git repositories, you often need to authenticate using SSH keys. While you can configure SSH settings globally, there are times when you need to use a specific SSH key for just one command. This is particularly useful when cloning a new repository or adding a submodule, where there isn't yet a local `.git/config` file to modify.
+
+## The Problem
+
+Imagine you're working on a project that requires you to clone a repository using a specific SSH key. Normally, you might edit your SSH config file or the repository's `.git/config` file. But what if you're just getting started and don't have these files set up yet?
+
+## The Solution
+
+Git provides a handy way to set configuration options for a single command using the `-c` flag. This allows you to specify the SSH command to use, including the path to your private key file.
+
+Here's the syntax:
+
+{{<highlight bash>}}
+git -c core.sshCommand="ssh -i /path/to/private_key_file" <git command>
+{{</highlight>}}
+
+## Examples
+
+### Cloning a Repository
+
+To clone a repository using a specific SSH key:
+
+{{<highlight bash>}}
+git -c core.sshCommand="ssh -i /path/to/private_key_file" clone git@github.com:username/repo.git
+{{</highlight>}}
+
+### Adding a Submodule
+
+Similarly, when adding a submodule:
+
+{{<highlight bash>}}
+git -c core.sshCommand="ssh -i /path/to/private_key_file" submodule add git@github.com:username/submodule.git
+{{</highlight>}}
+
+## Making SSH Keys Available
+
+Before using SSH keys with Git, it's important to check which keys are already available on your system:
+
+1. **Check Available Keys**: List the contents of your SSH directory:
+   {{<highlight bash>}}
+   ls -al ~/.ssh
+   {{</highlight>}}
+   This command will show you all files in your SSH directory, including your key files (typically with extensions like .pub for public keys).
+
+2. **Common Key Names**: Look for files like:
+   - id_rsa, id_rsa.pub
+   - id_ed25519, id_ed25519.pub
+   - id_ecdsa, id_ecdsa.pub
+
+3. **Add to SSH Agent**: If you find the key you want to use, ensure it's added to your SSH agent:
+   {{<highlight bash>}}
+   eval "$(ssh-agent -s)"
+   ssh-add ~/.ssh/your_private_key_file
+   {{</highlight>}}
+
+4. **Verify Key in Git Host**: Make sure the corresponding public key (the .pub file) is added to your Git host (e.g., GitHub, GitLab) in the SSH keys section of your account settings.
+
+If you don't find any suitable keys, you may need to generate a new one. However, for the purposes of this guide, we're assuming you're working with existing keys.
+
+## Adding sshCommand to .git/config
+
+After cloning a repository using the method described above, you might want to make this configuration permanent for the specific repository. Here's how to add the sshCommand to your local `.git/config` file:
+
+1. Navigate to your repository's root directory.
+
+2. Open the `.git/config` file in a text editor.
+
+3. Add the following lines under the `[core]` section (or create it if it doesn't exist):
+
+   {{<highlight ini>}}
+   [core]
+       sshCommand = ssh -i /path/to/private_key_file
+   {{</highlight>}}
+
+4. Save and close the file.
+
+Now, all Git commands for this repository will use the specified SSH key without needing the `-c` flag.
+
+## Why This Matters
+
+This technique is invaluable in several scenarios:
+
+1. **Multiple SSH Keys**: If you work with different Git hosts or have multiple accounts on the same host, you may need to use different SSH keys for different repositories.
+
+2. **Temporary Access**: When you need to grant someone temporary access to clone a repo without modifying global SSH settings.
+
+3. **Automation**: In CI/CD pipelines or scripts where you need to clone repositories with different authentication methods.
+
+4. **Testing**: When troubleshooting SSH issues, this allows you to test different keys without changing your global configuration.
+
+## Conclusion
+
+Using the `-c` flag with Git commands provides a flexible way to configure SSH settings on a per-command basis. This is especially useful when working with new repositories or in environments where you can't or don't want to modify global configurations. By mastering this technique, you can streamline your Git workflows and handle complex authentication scenarios with ease.
+
+Remember, while the `-c` flag method is convenient for one-off commands, for long-term projects, it's usually better to set up your SSH config file or the repository's `.git/config` for a more permanent solution.

+ 1 - 1
content/posts/mvi_architecture.md

@@ -208,4 +208,4 @@ The MVI helper class we've explored simplifies the implementation of the MVI pat
 
 Remember that you can use either data classes or sealed interfaces for your State, Intent, and Action definitions, depending on your specific needs. This flexibility allows you to choose the most appropriate structure for each component of your MVI architecture.
 
-While this helper class provides a solid foundation, you may need to adapt it to fit the specific needs of your project. Happy coding! 
+While this helper class provides a solid foundation, you may need to adapt it to fit the specific needs of your project. 

+ 186 - 0
public/posts/git_ssh_command/index.html

@@ -0,0 +1,186 @@
+<!DOCTYPE html>
+<html lang="en-us">
+
+<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=49193&amp;path=livereload" data-no-instant defer></script>
+    <title>
+Configuring SSH Keys for Individual Git Commands | codeskraps
+</title>
+
+    <meta http-equiv="content-type" content="text/html; charset=utf-8">
+<meta name="viewport" content="width=device-width, initial-scale=1.0">
+<meta name="description" content="Your website description">
+
+<meta name="generator" content="Hugo 0.134.3">
+
+
+<link rel="canonical" href="http://localhost:49193/posts/git_ssh_command/" >
+
+
+
+
+<link href="/css/style.min.ee0d47e4d4346c71a65a9e873108c81ffae54d60a2fc2338f6df394eb4b25a82.css" rel="stylesheet">
+
+
+
+
+</head>
+
+<body>
+
+    <div class="flexWrapper">
+        <header class="headerWrapper">
+    <div class="header">
+        <div>
+            <a class="terminal" href="http://localhost:49193/">
+                <span>me@codeskraps.com ~ $</span>
+            </a>
+        </div>
+        <input class="side-menu" type="checkbox" id="side-menu">
+        <label class="hamb" for="side-menu"><span class="hamb-line"></span></label>
+        <nav class="headerLinks">
+            <ul>
+                
+                <li>
+                    <a href="http://localhost:49193/projects/" title="" >
+                        ~/projects</a>
+                </li>
+                
+                <li>
+                    <a href="http://localhost:49193/about/" title="" >
+                        ~/about</a>
+                </li>
+                
+                <li>
+                    <a href="http://localhost:49193/posts/" title="" >
+                        ~/posts</a>
+                </li>
+                
+            </ul>
+        </nav>
+    </div>
+</header>
+
+
+        <div class="content">
+            <main class="main">
+                
+<div class="postWrapper">
+    <h1>Configuring SSH Keys for Individual Git Commands</h1>
+    
+    
+    <section class="postMetadata">
+        <dl>
+            
+            
+            
+            
+                <dt>published</dt>
+                
+                <dd><time datetime="2024-10-02">October 2, 2024</time></dd>
+            
+            
+                <dt>reading time</dt>
+                <dd>3 minutes</dd>
+            
+        </dl>
+    </section>
+    
+    <div>
+        <h1 id="configuring-ssh-keys-for-individual-git-commands">Configuring SSH Keys for Individual Git Commands</h1>
+<p>When working with Git repositories, you often need to authenticate using SSH keys. While you can configure SSH settings globally, there are times when you need to use a specific SSH key for just one command. This is particularly useful when cloning a new repository or adding a submodule, where there isn&rsquo;t yet a local <code>.git/config</code> file to modify.</p>
+<h2 id="the-problem">The Problem</h2>
+<p>Imagine you&rsquo;re working on a project that requires you to clone a repository using a specific SSH key. Normally, you might edit your SSH config file or the repository&rsquo;s <code>.git/config</code> file. But what if you&rsquo;re just getting started and don&rsquo;t have these files set up yet?</p>
+<h2 id="the-solution">The Solution</h2>
+<p>Git provides a handy way to set configuration options for a single command using the <code>-c</code> flag. This allows you to specify the SSH command to use, including the path to your private key file.</p>
+<p>Here&rsquo;s the syntax:</p>
+<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>git -c core.sshCommand<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;ssh -i /path/to/private_key_file&#34;</span> &lt;git command&gt;</span></span></code></pre></div>
+<h2 id="examples">Examples</h2>
+<h3 id="cloning-a-repository">Cloning a Repository</h3>
+<p>To clone a repository using a specific SSH key:</p>
+<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>git -c core.sshCommand<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;ssh -i /path/to/private_key_file&#34;</span> clone git@github.com:username/repo.git</span></span></code></pre></div>
+<h3 id="adding-a-submodule">Adding a Submodule</h3>
+<p>Similarly, when adding a submodule:</p>
+<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>git -c core.sshCommand<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;ssh -i /path/to/private_key_file&#34;</span> submodule add git@github.com:username/submodule.git</span></span></code></pre></div>
+<h2 id="making-ssh-keys-available">Making SSH Keys Available</h2>
+<p>Before using SSH keys with Git, it&rsquo;s important to check which keys are already available on your system:</p>
+<ol>
+<li>
+<p><strong>Check Available Keys</strong>: List the contents of your SSH directory:
+<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>ls -al ~/.ssh</span></span></code></pre></div>
+This command will show you all files in your SSH directory, including your key files (typically with extensions like .pub for public keys).</p>
+</li>
+<li>
+<p><strong>Common Key Names</strong>: Look for files like:</p>
+<ul>
+<li>id_rsa, id_rsa.pub</li>
+<li>id_ed25519, id_ed25519.pub</li>
+<li>id_ecdsa, id_ecdsa.pub</li>
+</ul>
+</li>
+<li>
+<p><strong>Add to SSH Agent</strong>: If you find the key you want to use, ensure it&rsquo;s added to your SSH agent:
+<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>eval <span style="color:#e6db74">&#34;</span><span style="color:#66d9ef">$(</span>ssh-agent -s<span style="color:#66d9ef">)</span><span style="color:#e6db74">&#34;</span>
+</span></span><span style="display:flex;"><span>ssh-add ~/.ssh/your_private_key_file</span></span></code></pre></div></p>
+</li>
+<li>
+<p><strong>Verify Key in Git Host</strong>: Make sure the corresponding public key (the .pub file) is added to your Git host (e.g., GitHub, GitLab) in the SSH keys section of your account settings.</p>
+</li>
+</ol>
+<p>If you don&rsquo;t find any suitable keys, you may need to generate a new one. However, for the purposes of this guide, we&rsquo;re assuming you&rsquo;re working with existing keys.</p>
+<h2 id="adding-sshcommand-to-gitconfig">Adding sshCommand to .git/config</h2>
+<p>After cloning a repository using the method described above, you might want to make this configuration permanent for the specific repository. Here&rsquo;s how to add the sshCommand to your local <code>.git/config</code> file:</p>
+<ol>
+<li>
+<p>Navigate to your repository&rsquo;s root directory.</p>
+</li>
+<li>
+<p>Open the <code>.git/config</code> file in a text editor.</p>
+</li>
+<li>
+<p>Add the following lines under the <code>[core]</code> section (or create it if it doesn&rsquo;t exist):</p>
+<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ini" data-lang="ini"><span style="display:flex;"><span><span style="color:#66d9ef">[core]</span>
+</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">sshCommand</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">ssh -i /path/to/private_key_file</span></span></span></code></pre></div>
+</li>
+<li>
+<p>Save and close the file.</p>
+</li>
+</ol>
+<p>Now, all Git commands for this repository will use the specified SSH key without needing the <code>-c</code> flag.</p>
+<h2 id="why-this-matters">Why This Matters</h2>
+<p>This technique is invaluable in several scenarios:</p>
+<ol>
+<li>
+<p><strong>Multiple SSH Keys</strong>: If you work with different Git hosts or have multiple accounts on the same host, you may need to use different SSH keys for different repositories.</p>
+</li>
+<li>
+<p><strong>Temporary Access</strong>: When you need to grant someone temporary access to clone a repo without modifying global SSH settings.</p>
+</li>
+<li>
+<p><strong>Automation</strong>: In CI/CD pipelines or scripts where you need to clone repositories with different authentication methods.</p>
+</li>
+<li>
+<p><strong>Testing</strong>: When troubleshooting SSH issues, this allows you to test different keys without changing your global configuration.</p>
+</li>
+</ol>
+<h2 id="conclusion">Conclusion</h2>
+<p>Using the <code>-c</code> flag with Git commands provides a flexible way to configure SSH settings on a per-command basis. This is especially useful when working with new repositories or in environments where you can&rsquo;t or don&rsquo;t want to modify global configurations. By mastering this technique, you can streamline your Git workflows and handle complex authentication scenarios with ease.</p>
+<p>Remember, while the <code>-c</code> flag method is convenient for one-off commands, for long-term projects, it&rsquo;s usually better to set up your SSH config file or the repository&rsquo;s <code>.git/config</code> for a more permanent solution.</p>
+<p>Happy coding!</p>
+
+    </div>
+</div>
+
+            </main>
+        </div>
+
+
+        <footer class="footer">
+    
+        <span>Built with <a href="https://gohugo.io" class="footerLink">Hugo</a> and <a href="https://github.com/LordMathis/hugo-theme-nightfall" class="footerLink">Nightfall</a> theme</span>
+    
+</footer>
+    </div>
+
+</body>
+
+</html>

+ 1 - 1
public/posts/mvi_architecture/index.html

@@ -278,7 +278,7 @@ Simplifying MVI Architecture | codeskraps
 <h2 id="conclusion">Conclusion</h2>
 <p>The MVI helper class we&rsquo;ve explored simplifies the implementation of the MVI pattern, providing a clean and type-safe way to manage state, handle user intents, and emit actions. By using this helper class, you can create more maintainable and testable view models, leading to more robust applications.</p>
 <p>Remember that you can use either data classes or sealed interfaces for your State, Intent, and Action definitions, depending on your specific needs. This flexibility allows you to choose the most appropriate structure for each component of your MVI architecture.</p>
-<p>While this helper class provides a solid foundation, you may need to adapt it to fit the specific needs of your project. Happy coding!</p>
+<p>While this helper class provides a solid foundation, you may need to adapt it to fit the specific needs of your project.</p>
     </div>
 </div>
 

+ 6 - 6
public/posts/my-first-post/index.html

@@ -1,7 +1,7 @@
 <!DOCTYPE html>
 <html lang="en-us">
 
-<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=61938&amp;path=livereload" data-no-instant defer></script>
+<head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=49193&amp;path=livereload" data-no-instant defer></script>
     <title>
 My First Post | codeskraps
 </title>
@@ -13,7 +13,7 @@ My First Post | codeskraps
 <meta name="generator" content="Hugo 0.134.3">
 
 
-<link rel="canonical" href="http://localhost:61938/posts/my-first-post/" >
+<link rel="canonical" href="http://localhost:49193/posts/my-first-post/" >
 
 
 
@@ -31,7 +31,7 @@ My First Post | codeskraps
         <header class="headerWrapper">
     <div class="header">
         <div>
-            <a class="terminal" href="http://localhost:61938/">
+            <a class="terminal" href="http://localhost:49193/">
                 <span>me@codeskraps.com ~ $</span>
             </a>
         </div>
@@ -41,17 +41,17 @@ My First Post | codeskraps
             <ul>
                 
                 <li>
-                    <a href="http://localhost:61938/projects/" title="" >
+                    <a href="http://localhost:49193/projects/" title="" >
                         ~/projects</a>
                 </li>
                 
                 <li>
-                    <a href="http://localhost:61938/about/" title="" >
+                    <a href="http://localhost:49193/about/" title="" >
                         ~/about</a>
                 </li>
                 
                 <li>
-                    <a href="http://localhost:61938/posts/" title="" >
+                    <a href="http://localhost:49193/posts/" title="" >
                         ~/posts</a>
                 </li>
                 

+ 6 - 6
public/sitemap.xml

@@ -3,15 +3,18 @@
   xmlns:xhtml="http://www.w3.org/1999/xhtml">
   <url>
     <loc>https://codeskraps.com/about/</loc>
+  </url><url>
+    <loc>https://codeskraps.com/</loc>
+    <lastmod>2024-10-02T16:55:53+02:00</lastmod>
+  </url><url>
+    <loc>https://codeskraps.com/posts/</loc>
+    <lastmod>2024-10-02T16:55:53+02:00</lastmod>
   </url><url>
     <loc>https://codeskraps.com/tags/android/</loc>
     <lastmod>2024-09-27T13:45:09+02:00</lastmod>
   </url><url>
     <loc>https://codeskraps.com/tags/architecture/</loc>
     <lastmod>2024-09-27T13:45:09+02:00</lastmod>
-  </url><url>
-    <loc>https://codeskraps.com/</loc>
-    <lastmod>2024-09-27T13:45:09+02:00</lastmod>
   </url><url>
     <loc>https://codeskraps.com/tags/kmp/</loc>
     <lastmod>2024-09-27T13:45:09+02:00</lastmod>
@@ -24,9 +27,6 @@
   </url><url>
     <loc>https://codeskraps.com/tags/mvvm/</loc>
     <lastmod>2024-09-27T13:45:09+02:00</lastmod>
-  </url><url>
-    <loc>https://codeskraps.com/posts/</loc>
-    <lastmod>2024-09-27T13:45:09+02:00</lastmod>
   </url><url>
     <loc>https://codeskraps.com/posts/mvi_architecture/</loc>
     <lastmod>2024-09-27T13:45:09+02:00</lastmod>