开发者

Git archive of repository with uncommitted changes

开发者 https://www.devze.com 2022-12-29 07:01 出处:网络
How can I create an archive of the current repository including local unc开发者_如何学运维ommitted changes using git archive?I know this is old, but I think I found a solution.

How can I create an archive of the current repository including local unc开发者_如何学运维ommitted changes using git archive?


I know this is old, but I think I found a solution.

Run:

stashName=`git stash create`;
git archive <options> $stashName

Since git wants a solid commit to make an archive, we can make a 'one off' commit using git stash. The create command just creates the stash commit (doesn't reset your working directory or push it to the stash stack) and returns the hash for it.

If you are worried about the space from the dangling commit, you can clean it up with a git gc --prune=now. Otherwise, just wait 2 weeks and it'll disappear.


Improving nevsan's answer for my purpose - to archive latest code in any case (committed or not):

uploadStash=`git stash create`; git archive -o code_outgoing.zip ${uploadStash:-HEAD}


Another solution with git ls-files :

git ls-files -z | xargs -0 tar -czvf archive.tar.gz


You don't have to use git stash anymore to create one artificial commit which would include your tracked change, for git archive to operate on.

With Git 2.29 (Q4 2020), "git archive"(man) learns the "--add-file" option to include untracked files into a snapshot from a tree-ish.

See commit df368fa, commit 2947a79, commit 200589a (19 Sep 2020) by René Scharfe (rscharfe).
(Merged by Junio C Hamano -- gitster -- in commit f6b06b4, 05 Oct 2020)

archive: add --add-file

Signed-off-by: René Scharfe

Allow users to append non-tracked files.
This simplifies the generation of source packages with a few extra files, e.g. containing version information.
They get the same access times and user information as tracked files.

git archive now includes in its man page:

--add-file=<file>

Add a non-tracked file to the archive.
Can be repeated to add multiple files.
The path of the file in the archive is built by concatenating the value for --prefix (if any) and the basename of <file>.


I also like the Git 2.30 alternative to tar.


Before Git 2.37 (Q3 2022), "git archive --add-file=<path>"(man) picked up the raw permission bits from the path and propagated to zip output in some cases, without normalization, which has been corrected (tar output did not have this issue).

See commit 6a61661 (12 May 2022) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 6cd6906, 23 May 2022)

archive: do not let on-disk mode leak to zip archives

When the "--add-file" option is used to add the contents from an untracked file to the archive, the permission mode bits for these files are sent to the archive-backend specific "write_entry()" method as-is.
We normalize the mode bits for tracked files way before we pass them to the write_entry() method; we should do the same here.

This is not strictly needed for "tar" archive-backend, as it has its own code to further clean them up, but "zip" archive-backend is not so well prepared.


This creates a zip archive containing all working tree files except those that are ignored by git:

git ls-files --others --exclude-standard --cached  | zip --names-stdin archive.zip


If you haven't committed the changes, then git archive won't help you. If you just want a snapshot of your working area, tar is probably your best bet.


I'm using this command to export specify version to now (including uncommit changes file) changes by one line

git archive -o /path/to/save/temp.zip $(git stash create) $(git diff --name-only xxx_version_to_start)


git ls-files is preferred cause it doesn't need to create any commit object.

git ls-files --recurse-submodules -z | tar --null -T - -czvf output.tar.gz
# git ls-files
#   --recurse-submocules
#   -z, split output lines with null
# tar
#  -T -, read files from stdin


a small bash script based on @fhucho's answer:

#backup.sh
#remove final "/"
repo=${1%"/"} 
#create $repo-backup.zip
cd $repo
git ls-files --others --exclude-standard --cached  | zip --names-stdin ../$repo-backup.zip
cd ..
ls -l $repo-backup.zip

usage:

. backup.sh myrepo
0

精彩评论

暂无评论...
验证码 换一张
取 消