clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

Git: checkout files or folders from another branch

Filed under: Webdev— Tagged with: checkout, git

You probably knew that a branch can be "checked out" with the git checkout branch-name, but interestingly, a single file or a whole folder can also be checked out from another branch.

Say, you want to pull a folder or a file from a feature branch to a master, the workflow would be as follows.

First, checkout the wanted branch:

$ git checkout master

Then checkout the needed file or folder from the other branch:

$ git checkout feature-branch -- src/js/some-file.js

Now Git shows a dirty tree:

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   src/js/some-file.js

Git checkout a folder is the same pretty much:

$ git checkout feature-branch -- src/js/

Then commit in normal fashion:

$ git commit -m "Get folder from another branch"

Comments would go here, but the commenting system isn’t ready yet, sorry. Tweet me @hiljaa if you want to make a correction etc.

  • © 2021 Antti Hiljá
  • About
  • Follow me in Twatter → @hiljaa
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • console.log('Smash the patriarchy!')
  • I love u!