<!DOCTYPE html>
<html lang="en-us">

<head>
    <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="Mobile development blog by Carles Sentis, sharing expertise in Android, iOS, and Kotlin Multiplatform development">

<meta name="generator" content="Hugo 0.145.0">


<link rel="canonical" href="https://codeskraps.com/posts/2024/git_ssh_command/" >




<link href="/css/style.min.ef8e99489b0b85e34523800e588426b1b4c05e27adcb9d7193952cef205afe6d.css" rel="stylesheet">

<script defer src="https://umami.codeskraps.com/script.js" data-website-id="a80a52da-0b7d-4faf-9dfc-ee6ca5a8421f" data-do-not-track="true"></script>


<link rel="stylesheet" href="/css/code-copy.css">
<script defer src="/js/code-copy.js"></script> 


</head>

<body>

    <div class="flexWrapper">
        <header class="headerWrapper">
    <div class="header">
        <div>
            <a class="terminal" href="https://codeskraps.com/">
                <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="https://codeskraps.com/posts/" title="" >
                        ~/posts</a>
                </li>
                
                <li>
                    <a href="https://codeskraps.com/projects/" title="" >
                        ~/projects</a>
                </li>
                
                <li>
                    <a href="https://codeskraps.com/about/" title="" >
                        ~/about</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>tags</dt>
<dd><span></span>
    <a href="/tags/git/">#Git</a><span></span>
    <a href="/tags/ssh/">#Ssh</a></dd>
            
            
            
            
                <dt>published</dt>
                
                <dd><time datetime="2024-04-17">April 17, 2024</time></dd>
            
            
                <dt>reading time</dt>
                <dd>3 minutes</dd>
            
        </dl>
    </section>
    
    <div>
        <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>

<style>
.code-block-container {
    position: relative;
    margin: 1em 0;
    border: 1px solid #5E5E5E;
    border-radius: 1px;
    overflow: hidden;
}

.code-header {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 999;
}

.copy-button {
    display: inline-flex;
    align-items: center;
    background: #2d2d2d;
    color: white;
    border: 1px solid #404040;
    border-radius: 4px;
    padding: 4px 8px;
    font-size: 0.8em;
    cursor: pointer;
    transition: all 0.2s ease;
}

.copy-button:hover {
    background: #404040;
}

.copy-button svg {
    width: 14px;
    height: 14px;
    margin-right: 4px;
}

.tooltip {
    position: absolute;
    top: -30px;
    left: 50%;
    transform: translateX(-50%);
    background: black;
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    display: none;
}

.tooltip::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: 5px solid transparent;
    border-top-color: black;
}

.copy-button.copied .tooltip {
    display: block;
}

.code-content {
    margin: 0;
    padding: 0;
}

.code-content pre {
    margin: 0;
    padding: 10px;
}
</style>

<div class="code-block-container" data-lang="bash">
    <div class="code-header">
        <button class="copy-button" onclick="copyCodeBlock(this)" aria-label="Copy code">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M8 4v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V7.242a2 2 0 0 0-.602-1.43L16.083 2.57A2 2 0 0 0 14.685 2H10a2 2 0 0 0-2 2z"/>
                <path d="M16 18v2a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h2"/>
            </svg>
            <span>Copy</span>
            <div class="tooltip">Copied!</div>
        </button>
    </div>
    <div class="code-content">
        <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-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:#ff79c6">=</span><span style="color:#f1fa8c">&#34;ssh -i /path/to/private_key_file&#34;</span> &lt;git command&gt;</span></span></code></pre></div>
    </div>
</div>

<script>
function copyCodeBlock(button) {
    const container = button.closest('.code-block-container');
    const codeContent = container.querySelector('.code-content pre');
    const code = codeContent.textContent;
    
    navigator.clipboard.writeText(code.trim()).then(() => {
        button.classList.add('copied');
        setTimeout(() => {
            button.classList.remove('copied');
        }, 2000);
    }).catch(err => {
        console.error('Failed to copy:', err);
        
        const textarea = document.createElement('textarea');
        textarea.value = code.trim();
        document.body.appendChild(textarea);
        textarea.select();
        try {
            document.execCommand('copy');
            button.classList.add('copied');
            setTimeout(() => {
                button.classList.remove('copied');
            }, 2000);
        } catch (e) {
            console.error('Fallback failed:', e);
        }
        document.body.removeChild(textarea);
    });
}
</script> 
<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>

<style>
.code-block-container {
    position: relative;
    margin: 1em 0;
    border: 1px solid #5E5E5E;
    border-radius: 1px;
    overflow: hidden;
}

.code-header {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 999;
}

.copy-button {
    display: inline-flex;
    align-items: center;
    background: #2d2d2d;
    color: white;
    border: 1px solid #404040;
    border-radius: 4px;
    padding: 4px 8px;
    font-size: 0.8em;
    cursor: pointer;
    transition: all 0.2s ease;
}

.copy-button:hover {
    background: #404040;
}

.copy-button svg {
    width: 14px;
    height: 14px;
    margin-right: 4px;
}

.tooltip {
    position: absolute;
    top: -30px;
    left: 50%;
    transform: translateX(-50%);
    background: black;
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    display: none;
}

.tooltip::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: 5px solid transparent;
    border-top-color: black;
}

.copy-button.copied .tooltip {
    display: block;
}

.code-content {
    margin: 0;
    padding: 0;
}

.code-content pre {
    margin: 0;
    padding: 10px;
}
</style>

<div class="code-block-container" data-lang="bash">
    <div class="code-header">
        <button class="copy-button" onclick="copyCodeBlock(this)" aria-label="Copy code">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M8 4v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V7.242a2 2 0 0 0-.602-1.43L16.083 2.57A2 2 0 0 0 14.685 2H10a2 2 0 0 0-2 2z"/>
                <path d="M16 18v2a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h2"/>
            </svg>
            <span>Copy</span>
            <div class="tooltip">Copied!</div>
        </button>
    </div>
    <div class="code-content">
        <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-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:#ff79c6">=</span><span style="color:#f1fa8c">&#34;ssh -i /path/to/private_key_file&#34;</span> clone git@github.com:username/repo.git</span></span></code></pre></div>
    </div>
</div>

<script>
function copyCodeBlock(button) {
    const container = button.closest('.code-block-container');
    const codeContent = container.querySelector('.code-content pre');
    const code = codeContent.textContent;
    
    navigator.clipboard.writeText(code.trim()).then(() => {
        button.classList.add('copied');
        setTimeout(() => {
            button.classList.remove('copied');
        }, 2000);
    }).catch(err => {
        console.error('Failed to copy:', err);
        
        const textarea = document.createElement('textarea');
        textarea.value = code.trim();
        document.body.appendChild(textarea);
        textarea.select();
        try {
            document.execCommand('copy');
            button.classList.add('copied');
            setTimeout(() => {
                button.classList.remove('copied');
            }, 2000);
        } catch (e) {
            console.error('Fallback failed:', e);
        }
        document.body.removeChild(textarea);
    });
}
</script> 
<h3 id="adding-a-submodule">Adding a Submodule</h3>
<p>Similarly, when adding a submodule:</p>

<style>
.code-block-container {
    position: relative;
    margin: 1em 0;
    border: 1px solid #5E5E5E;
    border-radius: 1px;
    overflow: hidden;
}

.code-header {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 999;
}

.copy-button {
    display: inline-flex;
    align-items: center;
    background: #2d2d2d;
    color: white;
    border: 1px solid #404040;
    border-radius: 4px;
    padding: 4px 8px;
    font-size: 0.8em;
    cursor: pointer;
    transition: all 0.2s ease;
}

.copy-button:hover {
    background: #404040;
}

.copy-button svg {
    width: 14px;
    height: 14px;
    margin-right: 4px;
}

.tooltip {
    position: absolute;
    top: -30px;
    left: 50%;
    transform: translateX(-50%);
    background: black;
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    display: none;
}

.tooltip::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: 5px solid transparent;
    border-top-color: black;
}

.copy-button.copied .tooltip {
    display: block;
}

.code-content {
    margin: 0;
    padding: 0;
}

.code-content pre {
    margin: 0;
    padding: 10px;
}
</style>

<div class="code-block-container" data-lang="bash">
    <div class="code-header">
        <button class="copy-button" onclick="copyCodeBlock(this)" aria-label="Copy code">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M8 4v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V7.242a2 2 0 0 0-.602-1.43L16.083 2.57A2 2 0 0 0 14.685 2H10a2 2 0 0 0-2 2z"/>
                <path d="M16 18v2a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h2"/>
            </svg>
            <span>Copy</span>
            <div class="tooltip">Copied!</div>
        </button>
    </div>
    <div class="code-content">
        <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-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:#ff79c6">=</span><span style="color:#f1fa8c">&#34;ssh -i /path/to/private_key_file&#34;</span> submodule add git@github.com:username/submodule.git</span></span></code></pre></div>
    </div>
</div>

<script>
function copyCodeBlock(button) {
    const container = button.closest('.code-block-container');
    const codeContent = container.querySelector('.code-content pre');
    const code = codeContent.textContent;
    
    navigator.clipboard.writeText(code.trim()).then(() => {
        button.classList.add('copied');
        setTimeout(() => {
            button.classList.remove('copied');
        }, 2000);
    }).catch(err => {
        console.error('Failed to copy:', err);
        
        const textarea = document.createElement('textarea');
        textarea.value = code.trim();
        document.body.appendChild(textarea);
        textarea.select();
        try {
            document.execCommand('copy');
            button.classList.add('copied');
            setTimeout(() => {
                button.classList.remove('copied');
            }, 2000);
        } catch (e) {
            console.error('Fallback failed:', e);
        }
        document.body.removeChild(textarea);
    });
}
</script> 
<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:

<style>
.code-block-container {
    position: relative;
    margin: 1em 0;
    border: 1px solid #5E5E5E;
    border-radius: 1px;
    overflow: hidden;
}

.code-header {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 999;
}

.copy-button {
    display: inline-flex;
    align-items: center;
    background: #2d2d2d;
    color: white;
    border: 1px solid #404040;
    border-radius: 4px;
    padding: 4px 8px;
    font-size: 0.8em;
    cursor: pointer;
    transition: all 0.2s ease;
}

.copy-button:hover {
    background: #404040;
}

.copy-button svg {
    width: 14px;
    height: 14px;
    margin-right: 4px;
}

.tooltip {
    position: absolute;
    top: -30px;
    left: 50%;
    transform: translateX(-50%);
    background: black;
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    display: none;
}

.tooltip::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: 5px solid transparent;
    border-top-color: black;
}

.copy-button.copied .tooltip {
    display: block;
}

.code-content {
    margin: 0;
    padding: 0;
}

.code-content pre {
    margin: 0;
    padding: 10px;
}
</style>

<div class="code-block-container" data-lang="bash">
    <div class="code-header">
        <button class="copy-button" onclick="copyCodeBlock(this)" aria-label="Copy code">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M8 4v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V7.242a2 2 0 0 0-.602-1.43L16.083 2.57A2 2 0 0 0 14.685 2H10a2 2 0 0 0-2 2z"/>
                <path d="M16 18v2a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h2"/>
            </svg>
            <span>Copy</span>
            <div class="tooltip">Copied!</div>
        </button>
    </div>
    <div class="code-content">
        <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-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><span style="display:flex;"><span>   </span></span></code></pre></div>
    </div>
</div>

<script>
function copyCodeBlock(button) {
    const container = button.closest('.code-block-container');
    const codeContent = container.querySelector('.code-content pre');
    const code = codeContent.textContent;
    
    navigator.clipboard.writeText(code.trim()).then(() => {
        button.classList.add('copied');
        setTimeout(() => {
            button.classList.remove('copied');
        }, 2000);
    }).catch(err => {
        console.error('Failed to copy:', err);
        
        const textarea = document.createElement('textarea');
        textarea.value = code.trim();
        document.body.appendChild(textarea);
        textarea.select();
        try {
            document.execCommand('copy');
            button.classList.add('copied');
            setTimeout(() => {
                button.classList.remove('copied');
            }, 2000);
        } catch (e) {
            console.error('Fallback failed:', e);
        }
        document.body.removeChild(textarea);
    });
}
</script> 
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:

<style>
.code-block-container {
    position: relative;
    margin: 1em 0;
    border: 1px solid #5E5E5E;
    border-radius: 1px;
    overflow: hidden;
}

.code-header {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 999;
}

.copy-button {
    display: inline-flex;
    align-items: center;
    background: #2d2d2d;
    color: white;
    border: 1px solid #404040;
    border-radius: 4px;
    padding: 4px 8px;
    font-size: 0.8em;
    cursor: pointer;
    transition: all 0.2s ease;
}

.copy-button:hover {
    background: #404040;
}

.copy-button svg {
    width: 14px;
    height: 14px;
    margin-right: 4px;
}

.tooltip {
    position: absolute;
    top: -30px;
    left: 50%;
    transform: translateX(-50%);
    background: black;
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    display: none;
}

.tooltip::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: 5px solid transparent;
    border-top-color: black;
}

.copy-button.copied .tooltip {
    display: block;
}

.code-content {
    margin: 0;
    padding: 0;
}

.code-content pre {
    margin: 0;
    padding: 10px;
}
</style>

<div class="code-block-container" data-lang="bash">
    <div class="code-header">
        <button class="copy-button" onclick="copyCodeBlock(this)" aria-label="Copy code">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M8 4v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V7.242a2 2 0 0 0-.602-1.43L16.083 2.57A2 2 0 0 0 14.685 2H10a2 2 0 0 0-2 2z"/>
                <path d="M16 18v2a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h2"/>
            </svg>
            <span>Copy</span>
            <div class="tooltip">Copied!</div>
        </button>
    </div>
    <div class="code-content">
        <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>   <span style="color:#8be9fd;font-style:italic">eval</span> <span style="color:#f1fa8c">&#34;</span><span style="color:#ff79c6">$(</span>ssh-agent -s<span style="color:#ff79c6">)</span><span style="color:#f1fa8c">&#34;</span>
</span></span><span style="display:flex;"><span>   ssh-add ~/.ssh/your_private_key_file
</span></span><span style="display:flex;"><span>   </span></span></code></pre></div>
    </div>
</div>

<script>
function copyCodeBlock(button) {
    const container = button.closest('.code-block-container');
    const codeContent = container.querySelector('.code-content pre');
    const code = codeContent.textContent;
    
    navigator.clipboard.writeText(code.trim()).then(() => {
        button.classList.add('copied');
        setTimeout(() => {
            button.classList.remove('copied');
        }, 2000);
    }).catch(err => {
        console.error('Failed to copy:', err);
        
        const textarea = document.createElement('textarea');
        textarea.value = code.trim();
        document.body.appendChild(textarea);
        textarea.select();
        try {
            document.execCommand('copy');
            button.classList.add('copied');
            setTimeout(() => {
                button.classList.remove('copied');
            }, 2000);
        } catch (e) {
            console.error('Fallback failed:', e);
        }
        document.body.removeChild(textarea);
    });
}
</script> </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>

<style>
.code-block-container {
    position: relative;
    margin: 1em 0;
    border: 1px solid #5E5E5E;
    border-radius: 1px;
    overflow: hidden;
}

.code-header {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 999;
}

.copy-button {
    display: inline-flex;
    align-items: center;
    background: #2d2d2d;
    color: white;
    border: 1px solid #404040;
    border-radius: 4px;
    padding: 4px 8px;
    font-size: 0.8em;
    cursor: pointer;
    transition: all 0.2s ease;
}

.copy-button:hover {
    background: #404040;
}

.copy-button svg {
    width: 14px;
    height: 14px;
    margin-right: 4px;
}

.tooltip {
    position: absolute;
    top: -30px;
    left: 50%;
    transform: translateX(-50%);
    background: black;
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    display: none;
}

.tooltip::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: 5px solid transparent;
    border-top-color: black;
}

.copy-button.copied .tooltip {
    display: block;
}

.code-content {
    margin: 0;
    padding: 0;
}

.code-content pre {
    margin: 0;
    padding: 10px;
}
</style>

<div class="code-block-container" data-lang="ini">
    <div class="code-header">
        <button class="copy-button" onclick="copyCodeBlock(this)" aria-label="Copy code">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M8 4v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V7.242a2 2 0 0 0-.602-1.43L16.083 2.57A2 2 0 0 0 14.685 2H10a2 2 0 0 0-2 2z"/>
                <path d="M16 18v2a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h2"/>
            </svg>
            <span>Copy</span>
            <div class="tooltip">Copied!</div>
        </button>
    </div>
    <div class="code-content">
        <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#282a36;-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:#ff79c6">[core]</span>
</span></span><span style="display:flex;"><span>       <span style="color:#50fa7b">sshCommand</span> <span style="color:#ff79c6">=</span> <span style="color:#f1fa8c">ssh -i /path/to/private_key_file
</span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c">   </span></span></span></code></pre></div>
    </div>
</div>

<script>
function copyCodeBlock(button) {
    const container = button.closest('.code-block-container');
    const codeContent = container.querySelector('.code-content pre');
    const code = codeContent.textContent;
    
    navigator.clipboard.writeText(code.trim()).then(() => {
        button.classList.add('copied');
        setTimeout(() => {
            button.classList.remove('copied');
        }, 2000);
    }).catch(err => {
        console.error('Failed to copy:', err);
        
        const textarea = document.createElement('textarea');
        textarea.value = code.trim();
        document.body.appendChild(textarea);
        textarea.select();
        try {
            document.execCommand('copy');
            button.classList.add('copied');
            setTimeout(() => {
                button.classList.remove('copied');
            }, 2000);
        } catch (e) {
            console.error('Fallback failed:', e);
        }
        document.body.removeChild(textarea);
    });
}
</script> 
</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>
    </div>
</div>

            </main>
        </div>


        <footer class="footer">
    
        <span>CC 2025, Built with <a href="https://gohugo.io" class="footerLink">Hugo</a> | Analytics by <a href="https://umami.codeskraps.com/" class="footerLink">Self-hosted Umami</a></span>
    
</footer>
    </div>

</body>

</html>