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

后端 未结 30 2931
暖寄归人
暖寄归人 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 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

提交回复
热议问题