Archive only changed files from Git repository

Git is a version control system and manages the changed history of files. Thanks to this feature, anyone cloned same repository can merge changes made by other users.

How to do when you want to send the only changes files to other? If the user can access to the git repository, you can request to pull latest files from repository by oneself. But, how about the user doesn’t have the account to the repository?

Git has a feature that can be used in this situation as well.


Features used

To extract differences between commits in Git repository, combine the following two functions.

To achive only files that have changed between commits in Git repository, combine the following two functions.

  • Function to list files that have changed between specified commits.
  • Function to archive only specified files.

List files that have changed between specified commits

To list files that have changed between specified commits, execute following command in the local repository.

git diff --name-only COMMIT1 COMMIT2 >> OutFile

This command write the list of files have changed between from COMMIT1 to COMMIT2 into OutFile.

COMMIT1 and COMMIT2 are specified commit hash. For example, in SourceTree, select the commit and the hash will be below file list.

Commit Hash in SourceTree
Commit Hash in SourceTree

You can specify the hash with abbreviated form too.

In SourceTree, right click the commit in the history view and select the “Copy SHA-1 to Clipboard” to copy the hash. You can paste it to the terminal. It is easier than enter by your hand.

Copy the commit hash
Copy the commit hash

If you want to specify the latest commit, enter HEAD as hash. For example, following command outputs the list of files changed between from 0123456 to latest into ../diff.txt.

git diff --name-only HEAD 0123456 >> ../diff.txt

Archive only specified files

To archive the specified files from Git repository, do following.

git archive HEAD `cat FILE` -o

This command copy the files are listed in ../diff.txt and archive them to

For example, to archive the files are listed in ../diff.txt to ../, do following.

git archive HEAD `cat ../diff.txt` -o ../

When the file is removed

If the change is to delete, rename or move a file, and the file is not already in the latest version of the repository, the following error message will appear.

fatal: pathspec '' did not match any files

Above example is a error that is not found. You can remove it from the list which will be passed to git archive, because the file is not already available and it is not needed to archive.

The file created by git diff is a text file, each lines of it is one file. Look up the file which is not found and delete its line.

After deleted, execute git archive again. If the other files are all avaiable, the archive will be created. If the other file is not found then the same error will be occurred. Delete the error file line and execute again.

Files are too many

When the changed files are too many and git archive can’t handle it, the following error is displayed.

warning: inexact rename detection was skipped due to too many files.
warning: you may want to set your diff.renameLimit variable to at least 13024 and retry the command.

The 13024 is an actual number of lines. Follow instructions and change diff.renameLimit value, then git diff can to be executed. To change the value, do following.

git config diff.renameLimit 13024

If you get other errors, such as using an older version of Git, and changing diff.renameLimit does not work, you can split the diff.txt file into multiple files and run it in multiple archives. If splitting into multiple archives seems to be a problem, expand the archives, combine them into a single folder, and zip them again.

Let's share this post !

Author of this article

Akira Hayashiのアバター Akira Hayashi Representative, Software Engineer

I am an application developer loves programming. This blog is a tech blog, its articles are learning notes. In my work, I mainly focus on desktop and mobile application development, but I also write technical books and teach seminars. The websites of my work and books are here -> RK Kaihatsu.