index.html 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. <!DOCTYPE html>
  2. <html lang="en-us">
  3. <head>
  4. <title>
  5. Configuring SSH Keys for Individual Git Commands | codeskraps
  6. </title>
  7. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  9. <meta name="description" content="Mobile development blog by Carles Sentis, sharing expertise in Android, iOS, and Kotlin Multiplatform development">
  10. <meta name="generator" content="Hugo 0.145.0">
  11. <link rel="canonical" href="https://codeskraps.com/posts/2024/git_ssh_command/" >
  12. <link href="/css/style.min.ef8e99489b0b85e34523800e588426b1b4c05e27adcb9d7193952cef205afe6d.css" rel="stylesheet">
  13. <script defer src="https://umami.codeskraps.com/script.js" data-website-id="a80a52da-0b7d-4faf-9dfc-ee6ca5a8421f"></script>
  14. <link rel="stylesheet" href="/css/code-copy.css">
  15. <script defer src="/js/code-copy.js"></script>
  16. </head>
  17. <body>
  18. <div class="flexWrapper">
  19. <header class="headerWrapper">
  20. <div class="header">
  21. <div>
  22. <a class="terminal" href="https://codeskraps.com/">
  23. <span>me@codeskraps.com ~ $</span>
  24. </a>
  25. </div>
  26. <input class="side-menu" type="checkbox" id="side-menu">
  27. <label class="hamb" for="side-menu"><span class="hamb-line"></span></label>
  28. <nav class="headerLinks">
  29. <ul>
  30. <li>
  31. <a href="https://codeskraps.com/posts/" title="" >
  32. ~/posts</a>
  33. </li>
  34. <li>
  35. <a href="https://codeskraps.com/projects/" title="" >
  36. ~/projects</a>
  37. </li>
  38. <li>
  39. <a href="https://codeskraps.com/about/" title="" >
  40. ~/about</a>
  41. </li>
  42. </ul>
  43. </nav>
  44. </div>
  45. </header>
  46. <div class="content">
  47. <main class="main">
  48. <div class="postWrapper">
  49. <h1>Configuring SSH Keys for Individual Git Commands</h1>
  50. <section class="postMetadata">
  51. <dl>
  52. <dt>tags</dt>
  53. <dd><span></span>
  54. <a href="/tags/git/">#Git</a><span></span>
  55. <a href="/tags/ssh/">#Ssh</a></dd>
  56. <dt>published</dt>
  57. <dd><time datetime="2024-04-17">April 17, 2024</time></dd>
  58. <dt>reading time</dt>
  59. <dd>3 minutes</dd>
  60. </dl>
  61. </section>
  62. <div>
  63. <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>
  64. <h2 id="the-problem">The Problem</h2>
  65. <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>
  66. <h2 id="the-solution">The Solution</h2>
  67. <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>
  68. <p>Here&rsquo;s the syntax:</p>
  69. <style>
  70. .code-block-container {
  71. position: relative;
  72. margin: 1em 0;
  73. border: 1px solid #5E5E5E;
  74. border-radius: 1px;
  75. overflow: hidden;
  76. }
  77. .code-header {
  78. position: absolute;
  79. top: 8px;
  80. right: 8px;
  81. z-index: 999;
  82. }
  83. .copy-button {
  84. display: inline-flex;
  85. align-items: center;
  86. background: #2d2d2d;
  87. color: white;
  88. border: 1px solid #404040;
  89. border-radius: 4px;
  90. padding: 4px 8px;
  91. font-size: 0.8em;
  92. cursor: pointer;
  93. transition: all 0.2s ease;
  94. }
  95. .copy-button:hover {
  96. background: #404040;
  97. }
  98. .copy-button svg {
  99. width: 14px;
  100. height: 14px;
  101. margin-right: 4px;
  102. }
  103. .tooltip {
  104. position: absolute;
  105. top: -30px;
  106. left: 50%;
  107. transform: translateX(-50%);
  108. background: black;
  109. color: white;
  110. padding: 4px 8px;
  111. border-radius: 4px;
  112. font-size: 12px;
  113. display: none;
  114. }
  115. .tooltip::after {
  116. content: "";
  117. position: absolute;
  118. top: 100%;
  119. left: 50%;
  120. transform: translateX(-50%);
  121. border: 5px solid transparent;
  122. border-top-color: black;
  123. }
  124. .copy-button.copied .tooltip {
  125. display: block;
  126. }
  127. .code-content {
  128. margin: 0;
  129. padding: 0;
  130. }
  131. .code-content pre {
  132. margin: 0;
  133. padding: 10px;
  134. }
  135. </style>
  136. <div class="code-block-container" data-lang="bash">
  137. <div class="code-header">
  138. <button class="copy-button" onclick="copyCodeBlock(this)" aria-label="Copy code">
  139. <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">
  140. <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"/>
  141. <path d="M16 18v2a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h2"/>
  142. </svg>
  143. <span>Copy</span>
  144. <div class="tooltip">Copied!</div>
  145. </button>
  146. </div>
  147. <div class="code-content">
  148. <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>
  149. </div>
  150. </div>
  151. <script>
  152. function copyCodeBlock(button) {
  153. const container = button.closest('.code-block-container');
  154. const codeContent = container.querySelector('.code-content pre');
  155. const code = codeContent.textContent;
  156. navigator.clipboard.writeText(code.trim()).then(() => {
  157. button.classList.add('copied');
  158. setTimeout(() => {
  159. button.classList.remove('copied');
  160. }, 2000);
  161. }).catch(err => {
  162. console.error('Failed to copy:', err);
  163. const textarea = document.createElement('textarea');
  164. textarea.value = code.trim();
  165. document.body.appendChild(textarea);
  166. textarea.select();
  167. try {
  168. document.execCommand('copy');
  169. button.classList.add('copied');
  170. setTimeout(() => {
  171. button.classList.remove('copied');
  172. }, 2000);
  173. } catch (e) {
  174. console.error('Fallback failed:', e);
  175. }
  176. document.body.removeChild(textarea);
  177. });
  178. }
  179. </script>
  180. <h2 id="examples">Examples</h2>
  181. <h3 id="cloning-a-repository">Cloning a Repository</h3>
  182. <p>To clone a repository using a specific SSH key:</p>
  183. <style>
  184. .code-block-container {
  185. position: relative;
  186. margin: 1em 0;
  187. border: 1px solid #5E5E5E;
  188. border-radius: 1px;
  189. overflow: hidden;
  190. }
  191. .code-header {
  192. position: absolute;
  193. top: 8px;
  194. right: 8px;
  195. z-index: 999;
  196. }
  197. .copy-button {
  198. display: inline-flex;
  199. align-items: center;
  200. background: #2d2d2d;
  201. color: white;
  202. border: 1px solid #404040;
  203. border-radius: 4px;
  204. padding: 4px 8px;
  205. font-size: 0.8em;
  206. cursor: pointer;
  207. transition: all 0.2s ease;
  208. }
  209. .copy-button:hover {
  210. background: #404040;
  211. }
  212. .copy-button svg {
  213. width: 14px;
  214. height: 14px;
  215. margin-right: 4px;
  216. }
  217. .tooltip {
  218. position: absolute;
  219. top: -30px;
  220. left: 50%;
  221. transform: translateX(-50%);
  222. background: black;
  223. color: white;
  224. padding: 4px 8px;
  225. border-radius: 4px;
  226. font-size: 12px;
  227. display: none;
  228. }
  229. .tooltip::after {
  230. content: "";
  231. position: absolute;
  232. top: 100%;
  233. left: 50%;
  234. transform: translateX(-50%);
  235. border: 5px solid transparent;
  236. border-top-color: black;
  237. }
  238. .copy-button.copied .tooltip {
  239. display: block;
  240. }
  241. .code-content {
  242. margin: 0;
  243. padding: 0;
  244. }
  245. .code-content pre {
  246. margin: 0;
  247. padding: 10px;
  248. }
  249. </style>
  250. <div class="code-block-container" data-lang="bash">
  251. <div class="code-header">
  252. <button class="copy-button" onclick="copyCodeBlock(this)" aria-label="Copy code">
  253. <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">
  254. <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"/>
  255. <path d="M16 18v2a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h2"/>
  256. </svg>
  257. <span>Copy</span>
  258. <div class="tooltip">Copied!</div>
  259. </button>
  260. </div>
  261. <div class="code-content">
  262. <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>
  263. </div>
  264. </div>
  265. <script>
  266. function copyCodeBlock(button) {
  267. const container = button.closest('.code-block-container');
  268. const codeContent = container.querySelector('.code-content pre');
  269. const code = codeContent.textContent;
  270. navigator.clipboard.writeText(code.trim()).then(() => {
  271. button.classList.add('copied');
  272. setTimeout(() => {
  273. button.classList.remove('copied');
  274. }, 2000);
  275. }).catch(err => {
  276. console.error('Failed to copy:', err);
  277. const textarea = document.createElement('textarea');
  278. textarea.value = code.trim();
  279. document.body.appendChild(textarea);
  280. textarea.select();
  281. try {
  282. document.execCommand('copy');
  283. button.classList.add('copied');
  284. setTimeout(() => {
  285. button.classList.remove('copied');
  286. }, 2000);
  287. } catch (e) {
  288. console.error('Fallback failed:', e);
  289. }
  290. document.body.removeChild(textarea);
  291. });
  292. }
  293. </script>
  294. <h3 id="adding-a-submodule">Adding a Submodule</h3>
  295. <p>Similarly, when adding a submodule:</p>
  296. <style>
  297. .code-block-container {
  298. position: relative;
  299. margin: 1em 0;
  300. border: 1px solid #5E5E5E;
  301. border-radius: 1px;
  302. overflow: hidden;
  303. }
  304. .code-header {
  305. position: absolute;
  306. top: 8px;
  307. right: 8px;
  308. z-index: 999;
  309. }
  310. .copy-button {
  311. display: inline-flex;
  312. align-items: center;
  313. background: #2d2d2d;
  314. color: white;
  315. border: 1px solid #404040;
  316. border-radius: 4px;
  317. padding: 4px 8px;
  318. font-size: 0.8em;
  319. cursor: pointer;
  320. transition: all 0.2s ease;
  321. }
  322. .copy-button:hover {
  323. background: #404040;
  324. }
  325. .copy-button svg {
  326. width: 14px;
  327. height: 14px;
  328. margin-right: 4px;
  329. }
  330. .tooltip {
  331. position: absolute;
  332. top: -30px;
  333. left: 50%;
  334. transform: translateX(-50%);
  335. background: black;
  336. color: white;
  337. padding: 4px 8px;
  338. border-radius: 4px;
  339. font-size: 12px;
  340. display: none;
  341. }
  342. .tooltip::after {
  343. content: "";
  344. position: absolute;
  345. top: 100%;
  346. left: 50%;
  347. transform: translateX(-50%);
  348. border: 5px solid transparent;
  349. border-top-color: black;
  350. }
  351. .copy-button.copied .tooltip {
  352. display: block;
  353. }
  354. .code-content {
  355. margin: 0;
  356. padding: 0;
  357. }
  358. .code-content pre {
  359. margin: 0;
  360. padding: 10px;
  361. }
  362. </style>
  363. <div class="code-block-container" data-lang="bash">
  364. <div class="code-header">
  365. <button class="copy-button" onclick="copyCodeBlock(this)" aria-label="Copy code">
  366. <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">
  367. <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"/>
  368. <path d="M16 18v2a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h2"/>
  369. </svg>
  370. <span>Copy</span>
  371. <div class="tooltip">Copied!</div>
  372. </button>
  373. </div>
  374. <div class="code-content">
  375. <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>
  376. </div>
  377. </div>
  378. <script>
  379. function copyCodeBlock(button) {
  380. const container = button.closest('.code-block-container');
  381. const codeContent = container.querySelector('.code-content pre');
  382. const code = codeContent.textContent;
  383. navigator.clipboard.writeText(code.trim()).then(() => {
  384. button.classList.add('copied');
  385. setTimeout(() => {
  386. button.classList.remove('copied');
  387. }, 2000);
  388. }).catch(err => {
  389. console.error('Failed to copy:', err);
  390. const textarea = document.createElement('textarea');
  391. textarea.value = code.trim();
  392. document.body.appendChild(textarea);
  393. textarea.select();
  394. try {
  395. document.execCommand('copy');
  396. button.classList.add('copied');
  397. setTimeout(() => {
  398. button.classList.remove('copied');
  399. }, 2000);
  400. } catch (e) {
  401. console.error('Fallback failed:', e);
  402. }
  403. document.body.removeChild(textarea);
  404. });
  405. }
  406. </script>
  407. <h2 id="making-ssh-keys-available">Making SSH Keys Available</h2>
  408. <p>Before using SSH keys with Git, it&rsquo;s important to check which keys are already available on your system:</p>
  409. <ol>
  410. <li>
  411. <p><strong>Check Available Keys</strong>: List the contents of your SSH directory:
  412. <style>
  413. .code-block-container {
  414. position: relative;
  415. margin: 1em 0;
  416. border: 1px solid #5E5E5E;
  417. border-radius: 1px;
  418. overflow: hidden;
  419. }
  420. .code-header {
  421. position: absolute;
  422. top: 8px;
  423. right: 8px;
  424. z-index: 999;
  425. }
  426. .copy-button {
  427. display: inline-flex;
  428. align-items: center;
  429. background: #2d2d2d;
  430. color: white;
  431. border: 1px solid #404040;
  432. border-radius: 4px;
  433. padding: 4px 8px;
  434. font-size: 0.8em;
  435. cursor: pointer;
  436. transition: all 0.2s ease;
  437. }
  438. .copy-button:hover {
  439. background: #404040;
  440. }
  441. .copy-button svg {
  442. width: 14px;
  443. height: 14px;
  444. margin-right: 4px;
  445. }
  446. .tooltip {
  447. position: absolute;
  448. top: -30px;
  449. left: 50%;
  450. transform: translateX(-50%);
  451. background: black;
  452. color: white;
  453. padding: 4px 8px;
  454. border-radius: 4px;
  455. font-size: 12px;
  456. display: none;
  457. }
  458. .tooltip::after {
  459. content: "";
  460. position: absolute;
  461. top: 100%;
  462. left: 50%;
  463. transform: translateX(-50%);
  464. border: 5px solid transparent;
  465. border-top-color: black;
  466. }
  467. .copy-button.copied .tooltip {
  468. display: block;
  469. }
  470. .code-content {
  471. margin: 0;
  472. padding: 0;
  473. }
  474. .code-content pre {
  475. margin: 0;
  476. padding: 10px;
  477. }
  478. </style>
  479. <div class="code-block-container" data-lang="bash">
  480. <div class="code-header">
  481. <button class="copy-button" onclick="copyCodeBlock(this)" aria-label="Copy code">
  482. <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">
  483. <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"/>
  484. <path d="M16 18v2a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h2"/>
  485. </svg>
  486. <span>Copy</span>
  487. <div class="tooltip">Copied!</div>
  488. </button>
  489. </div>
  490. <div class="code-content">
  491. <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
  492. </span></span><span style="display:flex;"><span> </span></span></code></pre></div>
  493. </div>
  494. </div>
  495. <script>
  496. function copyCodeBlock(button) {
  497. const container = button.closest('.code-block-container');
  498. const codeContent = container.querySelector('.code-content pre');
  499. const code = codeContent.textContent;
  500. navigator.clipboard.writeText(code.trim()).then(() => {
  501. button.classList.add('copied');
  502. setTimeout(() => {
  503. button.classList.remove('copied');
  504. }, 2000);
  505. }).catch(err => {
  506. console.error('Failed to copy:', err);
  507. const textarea = document.createElement('textarea');
  508. textarea.value = code.trim();
  509. document.body.appendChild(textarea);
  510. textarea.select();
  511. try {
  512. document.execCommand('copy');
  513. button.classList.add('copied');
  514. setTimeout(() => {
  515. button.classList.remove('copied');
  516. }, 2000);
  517. } catch (e) {
  518. console.error('Fallback failed:', e);
  519. }
  520. document.body.removeChild(textarea);
  521. });
  522. }
  523. </script>
  524. This command will show you all files in your SSH directory, including your key files (typically with extensions like .pub for public keys).</p>
  525. </li>
  526. <li>
  527. <p><strong>Common Key Names</strong>: Look for files like:</p>
  528. <ul>
  529. <li>id_rsa, id_rsa.pub</li>
  530. <li>id_ed25519, id_ed25519.pub</li>
  531. <li>id_ecdsa, id_ecdsa.pub</li>
  532. </ul>
  533. </li>
  534. <li>
  535. <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:
  536. <style>
  537. .code-block-container {
  538. position: relative;
  539. margin: 1em 0;
  540. border: 1px solid #5E5E5E;
  541. border-radius: 1px;
  542. overflow: hidden;
  543. }
  544. .code-header {
  545. position: absolute;
  546. top: 8px;
  547. right: 8px;
  548. z-index: 999;
  549. }
  550. .copy-button {
  551. display: inline-flex;
  552. align-items: center;
  553. background: #2d2d2d;
  554. color: white;
  555. border: 1px solid #404040;
  556. border-radius: 4px;
  557. padding: 4px 8px;
  558. font-size: 0.8em;
  559. cursor: pointer;
  560. transition: all 0.2s ease;
  561. }
  562. .copy-button:hover {
  563. background: #404040;
  564. }
  565. .copy-button svg {
  566. width: 14px;
  567. height: 14px;
  568. margin-right: 4px;
  569. }
  570. .tooltip {
  571. position: absolute;
  572. top: -30px;
  573. left: 50%;
  574. transform: translateX(-50%);
  575. background: black;
  576. color: white;
  577. padding: 4px 8px;
  578. border-radius: 4px;
  579. font-size: 12px;
  580. display: none;
  581. }
  582. .tooltip::after {
  583. content: "";
  584. position: absolute;
  585. top: 100%;
  586. left: 50%;
  587. transform: translateX(-50%);
  588. border: 5px solid transparent;
  589. border-top-color: black;
  590. }
  591. .copy-button.copied .tooltip {
  592. display: block;
  593. }
  594. .code-content {
  595. margin: 0;
  596. padding: 0;
  597. }
  598. .code-content pre {
  599. margin: 0;
  600. padding: 10px;
  601. }
  602. </style>
  603. <div class="code-block-container" data-lang="bash">
  604. <div class="code-header">
  605. <button class="copy-button" onclick="copyCodeBlock(this)" aria-label="Copy code">
  606. <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">
  607. <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"/>
  608. <path d="M16 18v2a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h2"/>
  609. </svg>
  610. <span>Copy</span>
  611. <div class="tooltip">Copied!</div>
  612. </button>
  613. </div>
  614. <div class="code-content">
  615. <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>
  616. </span></span><span style="display:flex;"><span> ssh-add ~/.ssh/your_private_key_file
  617. </span></span><span style="display:flex;"><span> </span></span></code></pre></div>
  618. </div>
  619. </div>
  620. <script>
  621. function copyCodeBlock(button) {
  622. const container = button.closest('.code-block-container');
  623. const codeContent = container.querySelector('.code-content pre');
  624. const code = codeContent.textContent;
  625. navigator.clipboard.writeText(code.trim()).then(() => {
  626. button.classList.add('copied');
  627. setTimeout(() => {
  628. button.classList.remove('copied');
  629. }, 2000);
  630. }).catch(err => {
  631. console.error('Failed to copy:', err);
  632. const textarea = document.createElement('textarea');
  633. textarea.value = code.trim();
  634. document.body.appendChild(textarea);
  635. textarea.select();
  636. try {
  637. document.execCommand('copy');
  638. button.classList.add('copied');
  639. setTimeout(() => {
  640. button.classList.remove('copied');
  641. }, 2000);
  642. } catch (e) {
  643. console.error('Fallback failed:', e);
  644. }
  645. document.body.removeChild(textarea);
  646. });
  647. }
  648. </script> </p>
  649. </li>
  650. <li>
  651. <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>
  652. </li>
  653. </ol>
  654. <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>
  655. <h2 id="adding-sshcommand-to-gitconfig">Adding sshCommand to .git/config</h2>
  656. <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>
  657. <ol>
  658. <li>
  659. <p>Navigate to your repository&rsquo;s root directory.</p>
  660. </li>
  661. <li>
  662. <p>Open the <code>.git/config</code> file in a text editor.</p>
  663. </li>
  664. <li>
  665. <p>Add the following lines under the <code>[core]</code> section (or create it if it doesn&rsquo;t exist):</p>
  666. <style>
  667. .code-block-container {
  668. position: relative;
  669. margin: 1em 0;
  670. border: 1px solid #5E5E5E;
  671. border-radius: 1px;
  672. overflow: hidden;
  673. }
  674. .code-header {
  675. position: absolute;
  676. top: 8px;
  677. right: 8px;
  678. z-index: 999;
  679. }
  680. .copy-button {
  681. display: inline-flex;
  682. align-items: center;
  683. background: #2d2d2d;
  684. color: white;
  685. border: 1px solid #404040;
  686. border-radius: 4px;
  687. padding: 4px 8px;
  688. font-size: 0.8em;
  689. cursor: pointer;
  690. transition: all 0.2s ease;
  691. }
  692. .copy-button:hover {
  693. background: #404040;
  694. }
  695. .copy-button svg {
  696. width: 14px;
  697. height: 14px;
  698. margin-right: 4px;
  699. }
  700. .tooltip {
  701. position: absolute;
  702. top: -30px;
  703. left: 50%;
  704. transform: translateX(-50%);
  705. background: black;
  706. color: white;
  707. padding: 4px 8px;
  708. border-radius: 4px;
  709. font-size: 12px;
  710. display: none;
  711. }
  712. .tooltip::after {
  713. content: "";
  714. position: absolute;
  715. top: 100%;
  716. left: 50%;
  717. transform: translateX(-50%);
  718. border: 5px solid transparent;
  719. border-top-color: black;
  720. }
  721. .copy-button.copied .tooltip {
  722. display: block;
  723. }
  724. .code-content {
  725. margin: 0;
  726. padding: 0;
  727. }
  728. .code-content pre {
  729. margin: 0;
  730. padding: 10px;
  731. }
  732. </style>
  733. <div class="code-block-container" data-lang="ini">
  734. <div class="code-header">
  735. <button class="copy-button" onclick="copyCodeBlock(this)" aria-label="Copy code">
  736. <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">
  737. <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"/>
  738. <path d="M16 18v2a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h2"/>
  739. </svg>
  740. <span>Copy</span>
  741. <div class="tooltip">Copied!</div>
  742. </button>
  743. </div>
  744. <div class="code-content">
  745. <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>
  746. </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
  747. </span></span></span><span style="display:flex;"><span><span style="color:#f1fa8c"> </span></span></span></code></pre></div>
  748. </div>
  749. </div>
  750. <script>
  751. function copyCodeBlock(button) {
  752. const container = button.closest('.code-block-container');
  753. const codeContent = container.querySelector('.code-content pre');
  754. const code = codeContent.textContent;
  755. navigator.clipboard.writeText(code.trim()).then(() => {
  756. button.classList.add('copied');
  757. setTimeout(() => {
  758. button.classList.remove('copied');
  759. }, 2000);
  760. }).catch(err => {
  761. console.error('Failed to copy:', err);
  762. const textarea = document.createElement('textarea');
  763. textarea.value = code.trim();
  764. document.body.appendChild(textarea);
  765. textarea.select();
  766. try {
  767. document.execCommand('copy');
  768. button.classList.add('copied');
  769. setTimeout(() => {
  770. button.classList.remove('copied');
  771. }, 2000);
  772. } catch (e) {
  773. console.error('Fallback failed:', e);
  774. }
  775. document.body.removeChild(textarea);
  776. });
  777. }
  778. </script>
  779. </li>
  780. <li>
  781. <p>Save and close the file.</p>
  782. </li>
  783. </ol>
  784. <p>Now, all Git commands for this repository will use the specified SSH key without needing the <code>-c</code> flag.</p>
  785. <h2 id="why-this-matters">Why This Matters</h2>
  786. <p>This technique is invaluable in several scenarios:</p>
  787. <ol>
  788. <li>
  789. <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>
  790. </li>
  791. <li>
  792. <p><strong>Temporary Access</strong>: When you need to grant someone temporary access to clone a repo without modifying global SSH settings.</p>
  793. </li>
  794. <li>
  795. <p><strong>Automation</strong>: In CI/CD pipelines or scripts where you need to clone repositories with different authentication methods.</p>
  796. </li>
  797. <li>
  798. <p><strong>Testing</strong>: When troubleshooting SSH issues, this allows you to test different keys without changing your global configuration.</p>
  799. </li>
  800. </ol>
  801. <h2 id="conclusion">Conclusion</h2>
  802. <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>
  803. <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>
  804. </div>
  805. </div>
  806. </main>
  807. </div>
  808. <footer class="footer">
  809. <span>CC 2025, Built with <a href="https://gohugo.io" class="footerLink">Hugo</a></span>
  810. </footer>
  811. </div>
  812. </body>
  813. </html>