Is there a simple way to export a single file from different git branch (local or remote) without checking out that branch?
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With