I've been wondering whether there is a good "git export" solution that creates a copy of a tree without the .git
repository directory. There are at least three methods I know of:
git clone
followed by removing the.git
repository directory.git checkout-index
alludes to this functionality but starts with "Just read the desired tree into the index..." which I'm not entirely sure how to do.git-export
is a third party script that essentially does agit clone
into a temporary location followed byrsync --exclude='.git'
into the final destination.
None of these solutions really strike me as being satisfactory. The closest one to svn export
might be option 1, because both those require the target directory to be empty first. But option 2 seems even better, assuming I can figure out what it means to read a tree into the index.
Probably the simplest way to achieve this is with
git archive
. If you really need just the expanded tree you can do something like this.Most of the time that I need to 'export' something from git, I want a compressed archive in any case so I do something like this.
ZIP archive:
git help archive
for more details, it's quite flexible.Be aware that even though the archive will not contain the .git directory, it will, however, contain other hidden git-specific files like .gitignore, .gitattributes, etc. If you don't want them in the archive, make sure you use the export-ignore attribute in a .gitattributes file and commit this before doing your archive. Read more...
Note: If you are interested in exporting the index, the command is
(See Greg's answer for more details)
I've written a simple wrapper around
git-checkout-index
that you can use like this:If the destination directory already exists, you'll need to add
-f
or--force
.Installation is simple; just drop the script somewhere in your
PATH
, and make sure it's executable.The github repository for
git-export
If you're not excluding files with
.gitattributes
export-ignore
then trygit checkout
and
Additionally you can get any Branch or Tag or from a specific Commit Revision like in SVN just adding the SHA1 (SHA1 in Git is the equivalent to the Revision Number in SVN)
The
/path/to/checkout/
must be empty, Git will not delete any file, but will overwrite files with the same name without any warningUPDATE: To avoid the beheaded problem or to leave intact the working repository when using checkout for export with tags, branches or SHA1, you need to add
-- ./
at the endThe double dash
--
tells git that everything after the dashes are paths or files, and also in this case tellsgit checkout
to not change theHEAD
Examples:
This command will get just the libs directory and also the
readme.txt
file from that exactly commitThis will create(overwrite)
my_file_2_behind_HEAD.txt
two commits behind the headHEAD^2
To get the export of another branch
Notice that
./
is relative to the root of the repositoryAs simple as clone then delete the .git folder:
git clone url_of_your_repo path_to_export && rm -rf path_to_export/.git
By far the easiest way i've seen to do it (and works on windows as well) is
git bundle
:git bundle create /some/bundle/path.bundle --all
See this answer for more details: How can I copy my git repository from my windows machine to a linux machine via usb drive?
Doing it the easy way, this is a function for .bash_profile, it directly unzips the archive on current location, configure first your usual [url:path]. NOTE: With this function you avoid the clone operation, it gets directly from the remote repo.
Alias for .gitconfig, same configuration required (TAKE CARE executing the command inside .git projects, it ALWAYS jumps to the base dir previously as said here, until this is fixed I personally prefer the function