Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

export a file from a different git branch

Tags:

git

Is there a simple way to export a single file from different git branch (local or remote) without checking out that branch?

like image 495
Erdem Avatar asked May 16 '11 13:05

Erdem


People also ask

How do I export a file from git?

There is no "git export" command, so instead you use the "git archive" command. By default, "git archive" produces its output in a tar format, so all you have to do is pipe that output into gzip or bzip2 or other.

How do I merge files from one branch to another in git?

We can use git checkout for far more than simply changing branches. If we supply it with a branch name and a file, we can replace a corrupted or broken file. Instead, if we want to pass some of the changed content we can use the --patch flag to manually merge an individual file.

How do I git pull from a branch?

In case you are using the Tower Git client, pulling from a remote is very easy: simply drag the remote branch and drop it onto your current HEAD in the sidebar - or click the "Pull" button in the toolbar.


2 Answers

You can do the following:

 git show experiment:docs/README.txt > /tmp/exported-README.txt

... for a local branch experiment. For a branch that's in the repository you're referring to with the remote origin, you can do the following, similarly:

 git fetch origin
 git show origin/other-experiment:docs/README.txt > /tmp/exported-README-remote.txt
like image 148
Mark Longair Avatar answered Oct 14 '22 01:10

Mark Longair


Yup

git show remote/branchname:path/to/file

If you want to save it directly, this might come in handy:

git_showfile () 
{ 
    if [ $# -lt 1 ]; then
        return 255;
    fi;
    local fspec="$1";
    shift;
    local fname="$(basename "$fspec")";
    local fpath="$(dirname "$fspec")";
    local revision=HEAD;
    if [ $# -ge 1 ]; then
        revision="$1";
    fi;
    if [ -e "$fspec" ]; then
        echo not overwriting existing file;
    else
        mkdir -pv "$fpath" && git show "$revision:$fspec" > "$fspec";
    fi
}

Edit: ... which you would use as follows

git_showfile path/to/file 

or

git_showfile path/to/file 237f723edcb89

etc.

like image 3
sehe Avatar answered Oct 14 '22 00:10

sehe