Do a “git export” (like “svn export”)?

后端 未结 30 2871
暖寄归人
暖寄归人 2020-11-21 22:33

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 thr

相关标签:
30条回答
  • 2020-11-21 22:56

    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

    0 讨论(0)
  • 2020-11-21 22:58

    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.

    0 讨论(0)
  • 2020-11-21 22:58

    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].

    0 讨论(0)
  • 2020-11-21 23:01

    If you're not excluding files with .gitattributes export-ignore then try git checkout

    mkdir /path/to/checkout/
    git --git-dir=/path/to/repo/.git --work-tree=/path/to/checkout/ checkout -f -q
    

    -f
    When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.

    and

    -q
    Avoid verbose

    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)

    mkdir /path/to/checkout/
    git --git-dir=/path/to/repo/.git --work-tree=/path/to/checkout/ checkout 2ef2e1f2de5f3d4f5e87df7d8 -f -q -- ./
    

    The /path/to/checkout/ must be empty, Git will not delete any file, but will overwrite files with the same name without any warning

    UPDATE: 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 end

    The double dash -- tells git that everything after the dashes are paths or files, and also in this case tells git checkout to not change the HEAD

    Examples:

    This command will get just the libs directory and also the readme.txt file from that exactly commit

    git --git-dir=/path/to/repo/.git --work-tree=/path/to/checkout/ checkout fef2e1f2de5f3d4f5e87df7d8 -f -q -- ./libs ./docs/readme.txt
    

    This will create(overwrite) my_file_2_behind_HEAD.txt two commits behind the head HEAD^2

    git --git-dir=/path/to/repo/.git --work-tree=/path/to/checkout/ checkout HEAD^2 -f -q -- ./my_file_2_behind_HEAD.txt
    

    To get the export of another branch

    git --git-dir=/path/to/repo/.git --work-tree=/path/to/checkout/ checkout myotherbranch -f -q -- ./
    

    Notice that ./ is relative to the root of the repository

    0 讨论(0)
  • 2020-11-21 23:01

    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.

    0 讨论(0)
  • 2020-11-21 23:01

    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.

    0 讨论(0)
提交回复
热议问题