Git : copy all changed files between two commits with directory structure

April 13th, 2012 § 4 comments

While working on a project, I was unable to configure git on the shared host. It was a nightmare to upload only the changed files after a bug fix. Keeping track manually on all the changed files got failed. So I started searching for a easy solution and I end up with a script which I can copy the changed files between two commits with directory structure.

In this I only need to keep track of the last uploaded commit, which seems to be easy for me. You just need to save the below script to git-copy.sh file and keep in the root folder of the project. Don’t forget to give execute permission for the file.

How to Use

This script accepts three parameters.

  • The Hash of First commit
  • The Hash of second commit
  • The destination folder, where we need to copy the files.

Syntax to execute :

./git-copy.sh hash1 hash2 path/to/destination/folder

Example :

Revath S Kumar [RSK]

- Rubyist / JavaScripter - Yeoman Team member - Open source enthusiast - Blogger @ PHPRepo and http://blog.revathskumar.com - Coder @ Whatznear - Google fanboy - jQuery lover

More Posts - Website - Twitter - Facebook - LinkedIn - Pinterest - Google Plus - YouTube

Tagged

  • Mario Vercellotti

    This will save me quite some time. Thank you so much for sharing!

  • Colin McEnearney

    Thanks for posting!

    I am trying to do the same thing and your example inspired me to dig some more. You can also use the -n and -skip options for `git log` to get the two commits programmatically:

    MOST_RECENT=$(git log -n 1 –pretty=format:’%h’)
    PREV=$(git log –skip=1 -n 1 –pretty=format:’%h’)

    and then get the file list with

    git diff –name-only $MOST_RECENT $PREV

    and then the script only needs to take the target dir as an arg! If it’s always the same dir for this project, wrap it in a rake task or alias or whatever 🙂

  • Hitesh Pachpor

    This is excellent! Thanks a lot!