Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy all files changed in last commit

Tags:

git

How can I export all files that were changed in the last commit ?

Can I get only the list of last committed files is separate folder ?

like image 632
mjdevloper Avatar asked Jul 10 '15 12:07

mjdevloper


2 Answers

  1. Create a file with name git-copy.sh with the following content:

    #!/bin/bash
    # Target directory
    TARGET=$3
    echo "Finding and copying files and folders to $TARGET"
    for i in $(git diff --name-only $1 $2)
        do
            # First create the target directory, if it doesn't exist.
            mkdir -p "$TARGET/$(dirname $i)"
             # Then copy over the file.
            cp "$i" "$TARGET/$i"
        done
    echo "Files copied to target directory";
    
  2. Run the script as a command from the root of your git project:

    ./git-copy.sh git-hash-1 git-hash-2 path/to/destination/folder
    

It will copy all the files with same directory structure to the destination folder.

like image 193
ernitinjain Avatar answered Oct 01 '22 00:10

ernitinjain


Here is a small bash(Unix) script that I wrote that will copy files for a given commit hash with the folder structure:

ARRAY=($(git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT $1))
PWD=$(pwd)

if [ -d "$2" ]; then

    for i in "${ARRAY[@]}"
    do
        : 
        cp --parents "$PWD/$i" $2
    done
else
    echo "Chosen destination folder does not exist."
fi

Create a file named '~/Scripts/copy-commit.sh' then give it execution privileges:

chmod a+x ~/Scripts/copy-commit.sh

Then, from the root of the git repository:

~/Scripts/copy-commit.sh COMMIT_KEY ~/Existing/Destination/Folder/

To get the last commit hash:

git rev-parse HEAD
like image 39
Patrick.SE Avatar answered Oct 01 '22 01:10

Patrick.SE