Skip to content

Instantly share code, notes, and snippets.

@LeonardoCardoso
Last active December 22, 2024 21:31
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 $@.zip HEAD
}
#... gitzip ZIPPED_FILE_NAME
@ConCaRo
Copy link

ConCaRo commented Jul 10, 2019

#if you want the file to have the name of the current folder if you're lazy about typing the zipfile name
git archive HEAD -o ${PWD##*/}.zip

This works well. Thanks

@kamok
Copy link

kamok commented Sep 16, 2019

Amazing alias. Thanks~

@josephg
Copy link

josephg commented Oct 25, 2019

Careful - this only zips what you've checked in to git. Any changes you haven't committed won't appear in the archive.

@josephg
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 stuff.zip HEAD
sephbook:g josephg$ unzip -l stuff.zip 
Archive:  stuff.zip
aa502e5d1bef684d9af8b7bbd95fdf04f09be1be
  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.

@ThinkDigitalSoftware
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.

@adrian-green
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:
Not:

[
  file1
  file2
  ...
]

But:

[
  repo-folder
    [
      file1
      file2
      ...
     ]
]

Use something like this -

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

@mohssineAboutaj
Copy link

Thank you very mutch, it's very helped

@sadernalwis
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

@LeonardoCardoso
Copy link
Author

@sadernalwis That's a good idea.

@zoutepopcorn
Copy link

zoutepopcorn commented Oct 15, 2020

I use one time

git config --global alias.zip 'archive HEAD -o' 

and then when you want..

git zip test.zip 

Ah sorry repost, sorry..

@ddieppa
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

@htho
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: https://superuser.com/questions/28162/how-do-i-use-7-zip-to-backup-files-but-exclude-some-directories

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