How to get just one file from another branch

I am using git and working on master branch. This branch has a file called app.js.

I have an experiment branch in which I made a bunch of changes and tons of commits. Now I want to bring all the changes done only to app.js from experiment to master branch.

  • Performing merge of two very different branches complicated by need to fast-forward merge-commit
  • How do you squash commits into one patch with git format-patch?
  • Git status reports untracked on tracked files
  • How do I point git HEAD to a particular ref using the jgit library?
  • gitignore doesn't ignore files in repository
  • Combining merges into a single merge
  • How do I do that?

    Once again I do not want a merge. I just want to bring all the changes in app.js from experiment branch to master branch.

  • How do I clone my git repo of sublime-text-2 settings to another computer
  • Egit: configure rebase as default pull strategy for master branch
  • How to organize development of Rails App and multiple Engines
  • Git submodule URL not including username?
  • How to use multiple ssh keys for multiple gitlab accounts with the same host
  • How to export a function, from tmux.conf
  • 4 Solutions collect form web for “How to get just one file from another branch”

    git checkout master               # first get back to master
    git checkout experiment -- app.js # then copy the version of app.js 
                                      # from branch "experiment"

    See also git how to undo changes of one file?

    As Jakub Narębski mentions in the comments:

    git show experiment:path/to/app.js > app.js

    works too, except that, as detailed in the SO question “How to retrieve a single file from specific revision in Git?”, you need to use the full path from the root directory of the repo.
    Hence the path/to/app.js used by Jakub in his example.

    As Frosty mentions in the comment:

    you will only get the most recent state of app.js

    But, for git checkout or git show, you can actually reference any revision you want, as illustrated in the SO question “git checkout revision of a file in git gui”:

    $ git show $REVISION:$FILENAME
    $ git checkout $REVISION -- $FILENAME

    would be the same is $FILENAME is a full path of a versioned file.

    $REVISION can be as shown in git rev-parse:

    experiment@{yesterday}:app.js # app.js as it was yesterday 
    experiment^:app.js            # app.js on the first commit parent
    experiment@{2}:app.js         # app.js two commits ago

    and so on.

    Everything is much simpler, use git checkout for that.

    Suppose you're on master branch, to get app.js from new-feature branch do:

    git checkout new-feature path/to/app.js
    // note that there is no leading slash in the path!

    This will bring you the contents of the desired file. You can, as always, use part of sha1 instead of new-feature branch name to get the file as it was in that particular commit.

    Supplemental to VonC’s and chhh’s answers.

    git show experiment:path/to/relative/app.js > app.js
    # If your current working directory is relative than just use
    git show experiment:app.js > app.js


    git checkout experiment -- app.js

    Or in my case I wanted all the files from another branch so:

    git checkout <brachname> -- .
    Git Baby is a git and github fan, let's start git clone.