Skip to content

Instantly share code, notes, and snippets.

Last active October 9, 2023 22:38
Show Gist options
  • Save LeonardoCardoso/6c083b90a8c327d8c82f to your computer and use it in GitHub Desktop.
Save LeonardoCardoso/6c083b90a8c327d8c82f to your computer and use it in GitHub Desktop.
Zip folder ignoring files listed on .gitignore
function gitzip() {
git archive -o $ HEAD
#... gitzip ZIPPED_FILE_NAME
Copy link

josephg commented Oct 26, 2019

Err, HEAD includes local commits, but not files / changes in the working directory that haven't been committed locally.

sephbook:g josephg$ git init
Initialized empty Git repository in /Users/josephg/temp/g/.git/
sephbook:g josephg$ echo 'hi there' > foo.txt
sephbook:g josephg$ git add foo.txt 
sephbook:g josephg$ git commit -m 'Added foo'
[master (root-commit) aa502e5] Added foo
 1 file changed, 1 insertion(+)
 create mode 100644 foo.txt
sephbook:g josephg$ echo 'changes' > bar.txt # <------ Will this make it into the zip file??
sephbook:g josephg$ git archive -o HEAD
sephbook:g josephg$ unzip -l 
  Length      Date    Time    Name
---------  ---------- -----   ----
        9  10-26-2019 22:12   foo.txt # <------ Nope! Only foo.txt!
---------                     -------
        9                     1 file

Notice that foo.txt is in the archive, but bar.txt is missing because it hasn't been committed.

Copy link

Good point. I actually want to accomplish exactly what the title says. I'm using a .gitignore file simply because it has a preselected list of items to ignore. There would be no commits in this repo.

Copy link

If you require the files in the archive to be nested within the repo folder, and not in the root of the zip ie:




Use something like this -

git archive --prefix ${PWD##*/}/ HEAD -o ../${PWD##*/}.zip

Copy link

Thank you very mutch, it's very helped

Copy link

if you find it useful to include the time of the backup/archive:

sudo git archive --prefix ${PWD##*/}/ HEAD -o ../${PWD##*/}-$(date "+%Y.%m.%d-%H.%M.%S").zip

Copy link

@sadernalwis That's a good idea.

Copy link

zoutepopcorn commented Oct 15, 2020

I use one time

git config --global 'archive HEAD -o' 

and then when you want..

git zip 

Ah sorry repost, sorry..

Copy link

ddieppa commented Dec 9, 2020

used this one for windows Powershell:

git archive HEAD -o ../$(Split-Path -Path ${PWD} -Leaf)-$(Get-Date -UFormat "%Y.%m.%d-%H.%M").zip

Copy link

htho commented Sep 2, 2021

I got here when i looked for a command do backup the whole (local) repository including .git.

Here it is:

7z.exe a -bd ../archive.7z * '-xr@.\.gitignore'

Based on this answer:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment