4

I am trying to create an archive of the files in the latest 3 commits using the following command.

git archive -o archive.zip $(git diff --name-only HEAD~3)

But in the last commit i deleted a few files, this is showing in the diff output. so i am getting this error:

fatal: pathspec 'public/uploads/5839529ba9381.png' did not match any files

How do i ignore the files that are deleted in a commit during git archive? I've tried the --ignore-unmatch argument but its not working.

bazi
  • 1,758
  • 5
  • 27
  • 41

3 Answers3

1

I was in the same situation, Heres something that works.

tar vczf archive.tar.gz --ignore-failed-read `git diff --name-only hash1 hash2`

I too was looking for, git archive ignore deleted files. As I wanted to create an archive of files that were added/modified between two branches/tag/hash etc.

The solution is to use tar instead of git archive, and pass the output of git diff in the same manner, only benefit here is that you can actually ignore missing files (deleted between commits) using the flag --ignore-failed-read. Also one thing to note is that this flag should come after the archive-file name.

Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
  • I wonder if `--ignore-failed-read` could ignore too much, e. g. actual read error due to a file defect or a sharing violation? – zett42 Mar 18 '20 at 11:56
0

Essentially, git archive is used to backup status of a certain time point, not fit to backup modifications of time line, i.e, you can not save an action of delete files by git archive.

If you want to backup latest 3 commits, you can use git bundle command.

git bundle create last_three_commit.bundle HEAD~3..HEAD

or

git bundle create last_three_commit.bundle -3 HEAD    

Please run git bundle --help for more details.

gzh
  • 3,507
  • 2
  • 19
  • 23
0

This worked out for me:

git archive -o archive.zip $(git diff --name-only --no-renames --diff-filter=a hash1..hash2)

Where hash1 is the newer commit hash and hash2 is for the older commit.

The --no-renames gets the new names for renamed files, not the original names.