git – get all file from last commit

in collaboration project, you can create new branch local, change, commit, push the branch, and request merge to master. and the owner project review a change and merge.

but sometime in project, merge the source can be harm (maybe if you not to expert in git, or too many branch to merge), alternative we can use old way, send zip source code, the owner check with tools like meld or simmiliar software.

in your local, collecting all file from your last commit can be frustrated, copy file, create folder, paste there, repeat all the file. and dont forget to check, maybe you forgot the some file and folder structure.

lucky in git, we can do it with one line code and output as zip file.

# go to source folder
cd workspace/myproject_collab

# go to branch
git checkout dashboard

# pull all file in last commit 
git archive --format=zip HEAD `git diff HEAD^ HEAD --name-only` > branch_dashboard.zip

# want get 2 last commit
git archive --format=zip HEAD `git diff HEAD~2 HEAD --name-only` > last_2_commit.zip

# if you have deleted file in commit, to prevent error
git archive --format=zip HEAD `git diff --diff-filter=d HEAD^ HEAD --name-only` > some_file_delete.zip

# if you not commit, and just want get change in local, using git ls-files
zip modified-files.zip $(git ls-files --modified) cp $(git ls-files --modified) ../modified-files

# if you want create patch
git diff HEAD^ HEAD > this.patch

# and apply with
patch -p1 this.patch

Leave a comment