index.html 27 KB

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