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: Source code management— Tagged with: git

You probably knew that a branch can be "checked out" with the command git checkout <branch-name>, but interestingly enough, 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 into 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 feature branch with the following command:

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

Now if you check the status, 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

Checking out 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.

  • © 2022 Antti Hiljá
  • About
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • I love u!