index.html 28 KB

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