davidosomething.com

blog

post

BASH script to copy files from a git commit

  • share this page

    • share this article:

This is a small BASH script I wrote to create a folder of changed files for web deployment. This is useful for a small project that doesn’t need a complicated deployment system. It should live in a folder within the project. There should be folders called “deploy” and “filelists” in the folder as well. Of course, you can edit the script to change those requirements.

The script takes a single argument — a git hash or revision name (e.g., HEAD). It gets the git commit log for that commit and stores the logged filenames into a new file tagged with the date. That files list is then read and each folder mentioned in the list is created in a “deploy” folder. The corresponding file (assumed to be a directory level lower) is copied into the appropriate folder in the deploy folder.

Usage:

./getfiles.sh HEAD

Script:

#!/bin/bash
if [ $# -ne 1 ]
then
  echo "Usage: `basename $0` {GIT REVISION SHA}"
  exit $E_BADARGS
fi

filelist="files-$(date +"%s").txt"
git show --pretty="format:" --name-only $1 > $filelist
for file in $(<$filelist); do
  mkdir -p "deploy/$(dirname $file)"
  cp "../$file" "deploy/$file"
done
mv $filelist filelists/

ls -GpR deploy
echo "DONE: deploy/ directory populated with files from commit $1"