Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, December 1, 2016

Do a

Do a "git export" (like "svn export")?


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:

  1. git clone followed by removing the .git repository directory.
  2. 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.
  3. git-export is a third party script that essentially does a git clone into a temporary location followed by rsync --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.

Answer by jperras for Do a "git export" (like "svn export")?


From the Git Manual:

Using git-checkout-index to "export an entire tree"

The prefix ability basically makes it trivial to use git-checkout-index as an "export as tree" function. Just read the desired tree into the index, and do:

$ git checkout-index --prefix=git-export-dir/ -a

Answer by Greg Hewgill for Do a "git export" (like "svn export")?


I found out what option 2 means. From a repository, you can do:

git checkout-index -a -f --prefix=/destination/path/  

The slash at the end of the path is important, otherwise it will result in the files being in /destination with a prefix of 'path'.

Since in a normal situation the index contains the contents of the repository, there is nothing special to do to "read the desired tree into the index". It's already there.

The -a flag is required to check out all files in the index (I'm not sure what it means to omit this flag in this situation, since it doesn't do what I want). The -f flag forces overwriting any existing files in the output, which this command doesn't normally do.

This appears to be the sort of "git export" I was looking for.

Answer by skiphoppy for Do a "git export" (like "svn export")?


My preference would actually be to have a dist target in your Makefile (or other build system) that exports a distributable archive of your code (.tar.bz2, .zip, .jar, or whatever is appropriate). If you happen to be using GNU autotools or Perl's MakeMaker systems, I think this exists for you automatically. If not, I highly recommend adding it.

ETA (2012-09-06): Wow, harsh downvotes. I still believe it is better to build your distributions with your build tools rather than your source code control tool. I believe in building artifacts with build tools. In my current job, our main product is built with an ant target. We are in the midst of switching source code control systems, and the presence of this ant target means one less hassle in migration.

Answer by Charles Bailey for Do a "git export" (like "svn export")?


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.

git archive master | tar -x -C /somewhere/else  

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.

git archive master | bzip2 >source-tree.tar.bz2  

ZIP archive:

git archive --format zip --output /full/path/to/zipfile.zip master   

git help archive for more details, it's quite flexible.


Note: If you are interested in exporting the index, the command is

git checkout-index -a -f --prefix=/destination/path/  

(See Greg's answer for more details)

Answer by Daniel Schierbeck for Do a "git export" (like "svn export")?


I've written a simple wrapper around git-checkout-index that you can use like this:

git export ~/the/destination/dir  

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

Answer by Aleksandr Somov for Do a "git export" (like "svn export")?


git archive also works with remote repository.

git archive --format=tar \  --remote=ssh://remote_server/remote_repository master | tar -xf -  

To export particular path inside the repo add as many paths as you wish as last argument to git, e.g.:

git archive --format=tar \  --remote=ssh://remote_server/remote_repository master path1/ path2/ | tar -xv  

Answer by kostmo for Do a "git export" (like "svn export")?


It appears that this is less of an issue with Git than SVN. Git only puts a .git folder in the repository root, whereas SVN puts a .svn folder in every subdirectory. So "svn export" avoids recursive command-line magic, whereas with Git recursion is not necessary.

Answer by troelskn for Do a "git export" (like "svn export")?


I needed this for a deploy script and I couldn't use any of the above mentioned approaches. Instead I figured out a different solution:

#!/bin/sh  [ $# -eq 2 ] || echo "USAGE $0 REPOSITORY DESTINATION" && exit 1  REPOSITORY=$1  DESTINATION=$2  TMPNAME="/tmp/$(basename $REPOSITORY).$$"  git clone $REPOSITORY $TMPNAME  rm -rf $TMPNAME/.git  mkdir -p $DESTINATION  cp -r $TMPNAME/* $DESTINATION  rm -rf $TMPNAME  

Answer by RkG for Do a "git export" (like "svn export")?


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.

gitss() {      URL=[url:path]        TMPFILE="`/bin/tempfile`"      if [ "$1" = "" ]; then          echo -e "Use: gitss repo [tree/commit]\n"          return      fi      if [ "$2" = "" ]; then          TREEISH="HEAD"      else          TREEISH="$2"      fi      echo "Getting $1/$TREEISH..."      git archive --format=zip --remote=$URL/$1 $TREEISH > $TMPFILE && unzip $TMPFILE && echo -e "\nDone\n"      rm $TMPFILE  }  

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

ss = !env GIT_TMPFILE="`/bin/tempfile`" sh -c 'git archive --format=zip --remote=[url:path]/$1 $2 \ > $GIT_TMPFILE && unzip $GIT_TMPFILE && rm $GIT_TMPFILE' -  

Answer by dkinzer for Do a "git export" (like "svn export")?


I just want to point out that in the case that you are

  1. exporting a sub folder of the repository (that's how I used to use SVN export feature)
  2. are OK with copying everything from that folder to the deployment destination
  3. and since you already have a copy of the entire repository in place.

Then you can just use cp foo [destination] instead of the mentioned git-archive master foo | -x -C [destination].

Answer by Harmon for Do a "git export" (like "svn export")?


This will copy all contents, minus the .dot files. I use this to export git cloned projects into my web app's git repo without the .git stuff.

cp -R ./path-to-git-repo /path/to/destination/

Plain old bash works just great :)

Answer by slatvick for Do a "git export" (like "svn export")?


I use git-submodules extensively. This one works for me:

rsync -a ./FROM/ ./TO --exclude='.*'  

Answer by tocororo for Do a "git export" (like "svn export")?


Bash-implementation of git-export.

I have segmented the .empty file creation and removal processes on their own function, with the purpose of re-using them in the 'git-archive' implementation (will be posted later on).

I have also added the '.gitattributes' file to the process in order to remove un-wanted files from the target export folder. Included verbosity to the process while making the 'git-export' function more efficient.

EMPTY_FILE=".empty";

function create_empty () {  ## Processing path (target-dir):      TRG_PATH="${1}";  ## Component(s):      EXCLUDE_DIR=".git";  echo -en "\nAdding '${EMPTY_FILE}' files to empty folder(s): ...";      find ${TRG_PATH} -not -path "*/${EXCLUDE_DIR}/*" -type d -empty -exec touch {}/${EMPTY_FILE} \;  #echo "done.";  ## Purging SRC/TRG_DIRs variable(s):      unset TRG_PATH EMPTY_FILE EXCLUDE_DIR;      return 0;    }    declare -a GIT_EXCLUDE;  function load_exclude () {      SRC_PATH="${1}";      ITEMS=0; while read LINE; do  #      echo -e "Line [${ITEMS}]: '${LINE%%\ *}'";        GIT_EXCLUDE[((ITEMS++))]=${LINE%%\ *};      done < ${SRC_PATH}/.gitattributes;      GIT_EXCLUDE[${ITEMS}]="${EMPTY_FILE}";  ## Purging variable(s):      unset SRC_PATH ITEMS;      return 0;    }    function purge_empty () {  ## Processing path (Source/Target-dir):      SRC_PATH="${1}";      TRG_PATH="${2}";  echo -e "\nPurging Git-Specific component(s): ... ";      find ${SRC_PATH} -type f -name ${EMPTY_FILE} -exec /bin/rm '{}' \;      for xRULE in ${GIT_EXCLUDE[@]}; do  echo -en "    '${TRG_PATH}/{${xRULE}}' files ... ";        find ${TRG_PATH} -type f -name "${xRULE}" -exec /bin/rm -rf '{}' \;  echo "done.'";      done;  echo -e "done.\n"  ## Purging SRC/TRG_PATHs variable(s):      unset SRC_PATH; unset TRG_PATH;      return 0;    }    function git-export () {      TRG_DIR="${1}"; SRC_DIR="${2}";      if [ -z "${SRC_DIR}" ]; then SRC_DIR="${PWD}"; fi      load_exclude "${SRC_DIR}";  ## Dynamically added '.empty' files to the Git-Structure:      create_empty "${SRC_DIR}";      GIT_COMMIT="Including '${EMPTY_FILE}' files into Git-Index container."; #echo -e "\n${GIT_COMMIT}";      git add .; git commit --quiet --all --verbose --message "${GIT_COMMIT}";      if [ "${?}" -eq 0 ]; then echo " done."; fi      /bin/rm -rf ${TRG_DIR} && mkdir -p "${TRG_DIR}";  echo -en "\nChecking-Out Index component(s): ... ";      git checkout-index --prefix=${TRG_DIR}/ -q -f -a  ## Reset: --mixed = reset HEAD and index:      if [ "${?}" -eq 0 ]; then  echo "done."; echo -en "Resetting HEAD and Index: ... ";          git reset --soft HEAD^;          if [ "${?}" -eq 0 ]; then  echo "done.";  ## Purging Git-specific components and '.empty' files from Target-Dir:              purge_empty "${SRC_DIR}" "${TRG_DIR}"            else echo "failed.";          fi  ## Archiving exported-content:  echo -en "Archiving Checked-Out component(s): ... ";          if [ -f "${TRG_DIR}.tgz" ]; then /bin/rm ${TRG_DIR}.tgz; fi          cd ${TRG_DIR} && tar -czf ${TRG_DIR}.tgz ./; cd ${SRC_DIR}  echo "done.";  ## Listing *.tgz file attributes:  ## Warning: Un-TAR this file to a specific directory:          ls -al ${TRG_DIR}.tgz        else echo "failed.";      fi  ## Purgin all references to Un-Staged File(s):     git reset HEAD;  ## Purging SRC/TRG_DIRs variable(s):      unset SRC_DIR; unset TRG_DIR;      echo "";      return 0;    }  

Output:

$ git-export /tmp/rel-1.0.0

Adding '.empty' files to empty folder(s): ... done.

Checking-Out Index component(s): ... done.

Resetting HEAD and Index: ... done.

Purging Git-Specific component(s): ...

'/tmp/rel-1.0.0/{.buildpath}' files ... done.'

'/tmp/rel-1.0.0/{.project}' files ... done.'

'/tmp/rel-1.0.0/{.gitignore}' files ... done.'

'/tmp/rel-1.0.0/{.git}' files ... done.'

'/tmp/rel-1.0.0/{.gitattributes}' files ... done.'

'/tmp/rel-1.0.0/{*.mno}' files ... done.'

'/tmp/rel-1.0.0/{*~}' files ... done.'

'/tmp/rel-1.0.0/{.*~}' files ... done.'

'/tmp/rel-1.0.0/{*.swp}' files ... done.'

'/tmp/rel-1.0.0/{*.swo}' files ... done.'

'/tmp/rel-1.0.0/{.DS_Store}' files ... done.'

'/tmp/rel-1.0.0/{.settings}' files ... done.'

'/tmp/rel-1.0.0/{.empty}' files ... done.'

done.

Archiving Checked-Out component(s): ... done.

-rw-r--r-- 1 admin wheel 25445901 3 Nov 12:57 /tmp/rel-1.0.0.tgz

I have now incorporated the 'git archive' functionality into a single process that makes use of 'create_empty' function and other features.

function git-archive () {      PREFIX="${1}"; ## sudo mkdir -p ${PREFIX}      REPO_PATH="`echo "${2}"|awk -F: '{print $1}'`";      RELEASE="`echo "${2}"|awk -F: '{print $2}'`";      USER_PATH="${PWD}";  echo "$PREFIX $REPO_PATH $RELEASE $USER_PATH";  ## Dynamically added '.empty' files to the Git-Structure:      cd "${REPO_PATH}"; populate_empty .; echo -en "\n";  #    git archive --prefix=git-1.4.0/ -o git-1.4.0.tar.gz v1.4.0  # e.g.: git-archive /var/www/htdocs /repos/domain.name/website:rel-1.0.0 --explode      OUTPUT_FILE="${USER_PATH}/${RELEASE}.tar.gz";      git archive --verbose --prefix=${PREFIX}/ -o ${OUTPUT_FILE} ${RELEASE}      cd "${USER_PATH}";      if [[ "${3}" =~ [--explode] ]]; then        if [ -d "./${RELEASE}" ]; then /bin/rm -rf "./${RELEASE}"; fi        mkdir -p ./${RELEASE}; tar -xzf "${OUTPUT_FILE}" -C ./${RELEASE}      fi  ## Purging SRC/TRG_DIRs variable(s):      unset PREFIX REPO_PATH RELEASE USER_PATH OUTPUT_FILE;      return 0;    }  

Answer by Lars Schillingmann for Do a "git export" (like "svn export")?


I have hit this page frequently when looking for a way to export a git repository. My answer to this question considers three properties that svn export has by design compared to git, since svn follows a centralized repository approach:

  • It minimizes the traffic to a remote repository location by not exporting all revisions
  • It does not include meta information in the export directory
  • Exporting a certain branch using svn is accomplished by specifying the appropriate path

    git clone --depth 1 --branch master git://git.somewhere destination_path  rm -rf destination_path/.git  

When building a certain release it is useful to clone a stable branch as for example --branch stable or --branch release/0.9.

Answer by aredridel for Do a "git export" (like "svn export")?


The equivalent of

svn export . otherpath  

inside an existing repo is

git archive branchname | (cd otherpath; tar x)  

The equivalent of

svn export url otherpath  

is

git archive --remote=url branchname | (cd otherpath; tar x)  

Answer by Rob Jensen for Do a "git export" (like "svn export")?


If you want something that works with submodules this might be worth a go.

Note:

  • MASTER_DIR = a checkout with your submodules checked out also
  • DEST_DIR = where this export will end up
  • If you have rsync, I think you'd be able to do the same thing with even less ball ache.

Assumptions:

  • You need to run this from the parent directory of MASTER_DIR ( i.e from MASTER_DIR cd .. )
  • DEST_DIR is assumed to have been created. This is pretty easy to modify to include the creation of a DEST_DIR if you wanted to

cd MASTER_DIR && tar -zcvf ../DEST_DIR/export.tar.gz --exclude='.git*' . && cd ../DEST_DIR/ && tar xvfz export.tar.gz && rm export.tar.gz

Answer by Brandon for Do a "git export" (like "svn export")?


If you need submodules as well, this should do the trick: https://github.com/meitar/git-archive-all.sh/wiki

Answer by orkoden for Do a "git export" (like "svn export")?


You can archive a remote repo at any commit as zip file.

git archive --format=zip --output=archive.zip --remote=USERNAME@HOSTNAME:PROJECTNAME.git HASHOFGITCOMMIT  

Answer by ty. for Do a "git export" (like "svn export")?


As simple as clone then delete the .git folder:

git clone url_of_your_repo path_to_export && rm -rf path_to_export/.git

Answer by Anthony Hatzopoulos for Do a "git export" (like "svn export")?


enter image description here

A special case answer if the repository is hosted on GitHub

Just use svn export

As far as I know Github does not allow archive --remote. Although GitHub is svn compatible and they do have all git repos svn accessible so you could just use svn export like you normally would with a few adjustments to your GitHub url.

For example export entire repo. Notice how trunk replaces master (or whatever the project's HEAD branch is set to).

svn export https://github.com/username/repo-name/trunk/

And you can just export a file or even a certain path

svn export https://github.com/username/repo-name/trunk/src/lib/folder

Example with jQuery JavaScript Library

The HEAD branch or master branch will be available using trunk:

svn ls https://github.com/jquery/jquery/trunk  

The non-HEAD branches will be accessible under /branches/:

svn ls https://github.com/jquery/jquery/branches/2.1-stable  

All tags under /tags/ in the same fashion:

svn ls https://github.com/jquery/jquery/tags/2.1.3  

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.