1<!DOCTYPE html> 2<html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <link href="http://gmpg.org/xfn/11" rel="profile"> 6 7 <!-- Enable responsiveness on mobile devices--> 8 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 9 10 <meta name="theme-color" content=#1371DD> 11 <meta name="keywords" content="HTML,CSS,XML,JavaScript,Android,Apps,Photography,midhunhk,Midhun,Midhun Harikumar,Message Counter,Open Source"> 12 <meta name="author" content="Midhun Harikumar"> 13 <meta property="og:type" content="Article"> 14 15 <!-- SEO --> 16 <meta name="twitter:site" content="@midhunhk" /> 17 <meta name="twitter:creator" content="@midhunhk" /> 18 <meta property="og:url" content="/dev/2025/01/08/git-checkout-file/"/> 19 <meta name="publish_date" property="og:publish_date" content='Jan 8, 2025'> 20 21 <meta name="twitter:card" content="summary" /> 22 23 24 <meta name="twitter:title" content="Copy file from another branch using Git" /> 25 <meta property="og:title" content="Copy file from another branch using Git"/> 26 27 28 <title> 29 30 Copy file from another branch using Git · Midhun Harikumar 31 32 </title> 33 34 <!-- CSS --> 35 <link rel="stylesheet" href="/assets/css/app.css"> 36 <link rel="stylesheet" href="/assets/css/custom.css"> 37 38 <!-- Icons --> 39 <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"> 40 <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png"> 41 <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"> 42 <link rel="manifest" href="/site.webmanifest"> 43 44 <!-- Atom Feed --> 45 <link rel="alternate" type="application/atom+xml" title="Midhun Harikumar" href="/atom.xml"> 46 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5/css/all.min.css"> 47 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"> 48 49 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma-social@1/bin/bulma-social.min.css"> 50 51 52
52<script src="https://kit.fontawesome.com/c01311720b.js" cookie-consent="strictly-necessary" crossorigin="anonymous"></script>
52 53 <!-- Connecting to adsense, For reviewing the site --> 54
54<script cookie-consent="targeting" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8380411311279528" 55 crossorigin="anonymous"></script>
55 56 </head> 57 <body> 58 <nav class="navbar is-primary" role="navigation" aria-label="main navigation"> 59 <div class="container"> 60 <div class="navbar-brand"> 61 <a class="navbar-item" href="https://midhunhk.com"> 62 <h2 class="is-size-4 is-family-secondary">M</h2> 63 </a> 64 65 <a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample"> 66 <span aria-hidden="true"></span> 67 <span aria-hidden="true"></span> 68 <span aria-hidden="true"></span> 69 </a> 70 </div> 71 72 <div id="navbarBasicExample" class="navbar-menu"> 73 <div class="navbar-start"> 74 <a class="navbar-item" href="https://midhunhk.com">Home</a> 75 <a class="navbar-item" href="https://midhunhk.com/blog">Blog</a> 76 <a class="navbar-item" href="https://midhunhk.com/about">About</a> 77 78 <div class="navbar-item has-dropdown is-hoverable"> 79 <a class="navbar-link"> 80 More 81 </a> 82 83 <div class="navbar-dropdown"> 84 <a class="navbar-item" href="https://midhunhk.com/tags">Tags</a> 85 <a class="navbar-item" href="https://midhunhk.com/categories">Categories</a> 86 <a class="navbar-item" href="https://github.com/midhunhk">Github</a> 87 </div> 88 </div> 89 </div> 90 91 </div> 92 93 </nav> 94 95
95<script> 96 document.addEventListener('DOMContentLoaded', () => { 97 98 // Get all "navbar-burger" elements 99 const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0); 100 101 // Check if there are any navbar burgers 102 if ($navbarBurgers.length > 0) { 103 104 // Add a click event on each of them 105 $navbarBurgers.forEach( el => { 106 el.addEventListener('click', () => { 107 108 // Get the target from the "data-target" attribute 109 const target = el.dataset.target; 110 const $target = document.getElementById(target); 111 112 // Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu" 113 el.classList.toggle('is-active'); 114 $target.classList.toggle('is-active'); 115 116 }); 117 }); 118 } 119 120 }); 121 </script>
121 122 123 124 125 <section class="hero is-primary is-medium " > 126 <div class="hero-body "> 127 <div class="container "> 128 <p class="is-size-3 py-0">Copy file from another branch using Git</p> 129 130 131 </div> 132 </div> 133</section> 134 135 136 137 <section class="section"> 138 <div class="container"> 139 <div class="columns"> 140 141 <div class="column is-12"> 142 <div class="post-content"> 143 <div class="columns reverse-row-order"> 144 <div class="column is-4"> 145 <p> 146 <span class="has-text-weight-bold is-uppercase">Published on </span> 147 <time datetime="2025-01-08T00:00:00+00:00"> 148 08 Jan 2025 149 </time> 150 </p> 151 152 153 154 <div class="tags mt-4"> 155 156 <a class="tag is-primary " 157 href="https://midhunhk.com/tags#development"> 158 development 159</a> 160 161 <a class="tag is-primary " 162 href="https://midhunhk.com/tags#git"> 163 git 164</a> 165 166 </div> 167 168 <p class="has-text-weight-bold is-uppercase">Share this article</p> 169 170<div class="buttons "> 171 <button class="button is-large" onclick="window.open('https://www.facebook.com/share.php?u=https://midhunhk.com/dev/2025/01/08/git-checkout-file/');"> 172 <span class="icon is-medium"> 173 <i class="fab fa-facebook fa-lg"></i> 174 </span> 175 </button> 176 <button class="button is-large" onclick="window.open('https://twitter.com/intent/tweet?text=https://midhunhk.com/dev/2025/01/08/git-checkout-file/');"> 177 <span class="icon is-medium"> 178 <i class="fab fa-twitter fa-lg"></i> 179 </span> 180 </button> 181 <button class="button is-large is-outlined" onclick="window.open('https://www.linkedin.com/shareArticle?mini=true&url=https://midhunhk.com/dev/2025/01/08/git-checkout-file/&title=Copy+file+from+another+branch+using+Git&summary=&utm_medium=social_share&utm_source=linkedin');"> 182 <span class="icon is-medium"> 183 <i class="fab fa-linkedin fa-lg"></i> 184 </span> 185 </button> 186 <button class="button is-large is-hidden-mobile" onclick="window.open('https://reddit.com/submit?url=https://midhunhk.com/dev/2025/01/08/git-checkout-file/');"> 187 <span class="icon is-medium"> 188 <i class="fab fa-reddit fa-lg"></i> 189 </span> 190 </button> 191</div> 192 <div class="mt-4 is-hidden-mobile"> 193 <!-- Recent Posts are hidden on mobile screen size --> 194 <p class="has-text-weight-bold is-uppercase">Recent Posts</p> 195 196<ul> 197 198 <li> 199 <a href="/personal/2024/12/31/year-2024-in-review/">Year 2024 in Review</a> 200 </li> 201 202 <li> 203 <a href="/learning/2024/08/07/quantum-computing/">Quantum Computing</a> 204 </li> 205 206 <li> 207 <a href="/dev/2024/07/12/duplicate-google-play-core/">Duplicate class StateUpdatedListener found</a> 208 </li> 209 210 <li> 211 <a href="/learning/2024/07/04/beyond-automation/">Beyond Automation - Leveraging AI for Strategic Advantage</a> 212 </li> 213 214</ul> 215 </div> 216 </div> 217 218 <div class="column is-8"> 219 <article class="blog-article-content has-background-white p-5"> 220 <p>Every so often I find myself doing a search for finding the right command to copy a file from another branch to the current working branch in my Git workflows. 221It might be to go back to a working version if I messed it up, or to revert all my changes if I was working on something temporary.</p> 222 223<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git checkout <source-branch> <span class="nt">--</span> <file-path> 224</code></pre></div></div> 225<p>where</p> 226<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><source-branch>: The name of the branch where the file currently exists. 227<file-path>: The path to the file you want to copy (relative to the repository root). 228</code></pre></div></div> 229<p>This command will copy the file from the specified branch into your current branchâs working directory.</p> 230 231<h2 id="example">Example</h2> 232<p>Assuming we are on the main branch. 233And we want to copy a file <code class="language-plaintext highlighter-rouge">src/config/settings.json</code> from the branch feature-branch.</p> 234 235<p>Run:</p> 236<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git checkout feature-branch <span class="nt">--</span> src/config/settings.json 237</code></pre></div></div> 238<p>The file <code class="language-plaintext highlighter-rouge">src/config/settings.json</code> from feature-branch will now be in the working directory on main.</p> 239 240 </article> 241 242 <div class="my-6">
243 <span class="tag is-success mb-2">Advertisement</span> 244 <!-- Google Ads from advertisements include --> 245 246<!-- midhunhk_com_in_article_01 8918794049 -->
247<script cookie-consent="targeting" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8380411311279528" 248 crossorigin="anonymous"></script>
248 249<ins class="adsbygoogle" 250 style="display:block; text-align:center;" 251 data-ad-layout="in-article" 252 data-ad-format="fluid" 253 data-ad-client="ca-pub-8380411311279528" 254 data-ad-slot="8918794049"></ins>
255<script type="text/plain" cookie-consent="targeting"> 256 (adsbygoogle = window.adsbygoogle || []).push({}); 257</script>
257 258 </div> 259 </div> 260</div> 261 262<!-- Cookie Consent by https://www.CookieConsent.com -->
263<script type="text/javascript" src="//www.cookieconsent.com/releases/4.0.0/cookie-consent.js" charset="UTF-8"></script>
vendor: 1 bytes, line 263
263
264<script type="text/javascript" charset="UTF-8"> 265document.addEventListener('DOMContentLoaded', function () { 266cookieconsent.run({"notice_banner_type":"simple","consent_type":"implied","palette":"dark","language":"en","page_load_consent_levels":["strictly-necessary","functionality","tracking","targeting"],"notice_banner_reject_button_hide":false,"preferences_center_close_button_hide":false,"website_name":"https://www.midhunhk.com/","website_privacy_policy_url":"https://ae-app-labs.github.io/privacy-policy/midhunhk-blog/"}); 267}); 268</script>
268 269 270<!-- Analytics -->
vendor: 108 bytes, lines 270-271
270 271<script type="text/plain" cookie-consent="tracking" async src="https://www.googletagmanager.com/gtag/js?id=
271G-BERWJ12310
vendor: 12 bytes, line 271
271"></script>
272<script type="text/plain" cookie-consent="tracking"> 273 window['ga-disable-G-BERWJ12310'] = window.doNotTrack === "1" || navigator.doNotTrack === "1" || navigator.doNotTrack === "yes" || navigator.msDoNotTrack === "1"; 274
vendor: 133 bytes, lines 274-277
274window.dataLayer = window.dataLayer || []; 275 function gtag(){dataLayer.push(arguments);} 276 gtag('js', new Date()); 277 gtag('config', '
277G-BERWJ12310
vendor: 4 bytes, line 277
277');
278</script>
278 279 280<!-- end of Analytics--> 281 282<!-- Adsense --> 283 <!-- Loaded in each post page with 'advertisements.html' include --> 284<!-- end of Adsense--> 285 286<noscript>ePrivacy and GPDR Cookie Consent by <a href="https://www.CookieConsent.com/" rel="nofollow noopener">Cookie Consent</a></noscript> 287<!-- End Cookie Consent by https://www.CookieConsent.com --> 288 289 290 291 292<div id="disqus_thread"></div>
293<script> 294 295/** 296* RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS. 297* LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/ 298/* 299var disqus_config = function () { 300this.page.url = 'http://midhunhk.github.com/dev/2025/01/08/git-checkout-file/'; // Replace PAGE_URL with your page's canonical URL variable 301this.page.identifier = '/dev/2025/01/08/git-checkout-file/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable 302}; 303*/ 304(function() { // DON'T EDIT BELOW THIS LINE 305var d = document, s = d.createElement('script'); 306s.src = 'https://midhunhk.disqus.com/embed.js'; 307s.setAttribute('data-timestamp', +new Date()); 308(d.head || d.body).appendChild(s); 309})(); 310</script>
310 311<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> 312 313 314 315 316 </div> 317 </div> 318 319 </div> 320 </div> 321 </section> 322 323 <section class="section has-background-link-dark has-text-white-ter"> 324 <div class="container has-text-centered"> 325 <p class="is-size-6"> 326 <a href="https://www.midhunhk.com/" class="has-text-success">Home</a> 327 | <a href="https://ae-app-labs.github.io/privacy-policy/midhunhk-blog" target="_blank" class="has-text-success">Privacy Policy</a> 328 | <a href="#" onclick="return false" id="open_preferences_center" class="has-text-success">Cookie Preferences</a> 329 | <a href="https://ae-app-labs.github.io" target="_blank" class="has-text-success">Ae App Labs</a> 330 </p> 331 <p class="is-size-6 has-text-centered pt-0"> 332 © <time datetime="2026-08-11T03:36:19+00:00">2026</time> 333 Midhun Harikumar. All Rights Reserved. 334 </p> 335 </div> 336</section> 337 <!-- Cookie Consent by https://www.CookieConsent.com -->
338<script type="text/javascript" src="//www.cookieconsent.com/releases/4.0.0/cookie-consent.js" charset="UTF-8"></script>
vendor: 1 bytes, line 338
338
339<script type="text/javascript" charset="UTF-8"> 340document.addEventListener('DOMContentLoaded', function () { 341cookieconsent.run({"notice_banner_type":"simple","consent_type":"implied","palette":"dark","language":"en","page_load_consent_levels":["strictly-necessary","functionality","tracking","targeting"],"notice_banner_reject_button_hide":false,"preferences_center_close_button_hide":false,"website_name":"https://www.midhunhk.com/","website_privacy_policy_url":"https://ae-app-labs.github.io/privacy-policy/midhunhk-blog/"}); 342}); 343</script>
343 344 345<!-- Analytics -->
vendor: 108 bytes, lines 345-346
345 346<script type="text/plain" cookie-consent="tracking" async src="https://www.googletagmanager.com/gtag/js?id=
346G-BERWJ12310
vendor: 12 bytes, line 346
346"></script>
347<script type="text/plain" cookie-consent="tracking"> 348 window['ga-disable-G-BERWJ12310'] = window.doNotTrack === "1" || navigator.doNotTrack === "1" || navigator.doNotTrack === "yes" || navigator.msDoNotTrack === "1"; 349
vendor: 133 bytes, lines 349-352
349window.dataLayer = window.dataLayer || []; 350 function gtag(){dataLayer.push(arguments);} 351 gtag('js', new Date()); 352 gtag('config', '
352G-BERWJ12310
vendor: 4 bytes, line 352
352');
353</script>
353 354 355<!-- end of Analytics--> 356 357<!-- Adsense --> 358 <!-- Loaded in each post page with 'advertisements.html' include --> 359<!-- end of Adsense--> 360 361<noscript>ePrivacy and GPDR Cookie Consent by <a href="https://www.CookieConsent.com/" rel="nofollow noopener">Cookie Consent</a></noscript> 362<!-- End Cookie Consent by https://www.CookieConsent.com --> 363 </body> 364</html>
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.