Mr Question
Mr Question

Reputation: 1235

Git: how to get all the files changed and new files in a folder or zip?

As my question says, after changing files and adding new files in my repository, I normally commit files with git, but sometimes I need all the modified / changed files copied to a folder for organizing-myself reasons.

Any option?

Upvotes: 110

Views: 76954

Answers (12)

Dan Dascalescu
Dan Dascalescu

Reputation: 152146

Note that the git ls-files --modified command listed in other answers doesn't include new files.

I've been using the command below to back new and modified files in case my IDE screwed up when syncing from remote branches and its "Smart checkout" feature wasn't that smart:

cp --parents $(git status -s | egrep "^A|^ M" | cut -c 4-) ../modified-files # Linux
rsync -R $(git status -s | egrep "^A|^ M" | cut -c 4-) ../modified-files # macOS

Let's look at how we obtain the list of new and modified files:

git status -s | grep "^A\|^ M" | cut -c 4-
  • -s stands for --short and it will produce an output like this:

     M .gitignore
    A  _includes/code/howto/manage-data.create.py
    A  _includes/code/howto/manage-data.create.ts
     M _includes/code/howto/search.generative.ts
     M _includes/code/howto/search.hybrid.ts
    AM developers/weaviate/manage-data/create.mdx
    ?? _includes/code/howto/jeopardy_1k.csv
    
  • grep "^A\|^ M" filters for Added or Modified files

  • cut -c 4- cuts those first 3 characters and returns the filename starting from the 4th character

The other options are:

Upvotes: 4

Cascabel
Cascabel

Reputation: 497062

Assuming you mean you haven't yet committed, and want to package up all of the files that currently have local modifications (but not completely new files), you can get the list of modified files with git ls-files --modified. If you want the files which were changed by the last commit, you could use git diff --name-only HEAD^. Where you go from there is up to you. Examples:

zip modified-files.zip $(git ls-files --modified)
cp --parents $(git ls-files --modified) ../modified-files # Linux
rsync -R $(git ls-files --modified) ../modified-files # macOS

Note that this is using the versions of files in the working tree currently.

If you have spaces in filenames, you'll have to go to a little more trouble.

(Of course, depending on what you're really trying to do, you might be looking for git stash, which stashes away all modified files and leaves you with a clean working tree, or you could simply want to make a temporary branch to commit to.)

Upvotes: 153

ahmed
ahmed

Reputation: 1

You can export your last modified files via git ls-files --modified Just create a .sh file with the following code And execute it.

#!/bin/sh
FILES=`git ls-files --modified`
export_dir="place your export dir"
for x in $FILES
do
   prev_dir=$PWD
   folder=$(dirname $x)
   echo "Exporting to..." $export_dir/$x
   cp $prev_dir/$x $export_dir/$x
done

Upvotes: 0

Inam Ul Huq
Inam Ul Huq

Reputation: 752

Gathered from different solutions, here's what I used:

git archive --format=zip HEAD `git diff --name-only [from-commit-hash]` > changes.zip

It'll zip changes from any particular commit.

like:

git archive --format=zip HEAD `git diff --name-only 4a944407aec42222761cdc23c3e0b89a0c1e26a1` > changes.zip

Upvotes: 1

Shazni Shiraz
Shazni Shiraz

Reputation: 119

Had the same requirement. Got the input from the first answer and created a tool (Windows Only) for myself. Which copies all the added/ modified files to a backup folder.

Tool Overview

Git-Uncommited-Files-Backup-Tool-Windows

Just wrote this quick for fun. Just posted incase it'd help someone. (Highly welcomed for modifications).

Also, you can schedule backups using the Task Scheduler.

Upvotes: 1

Rajaram Shelar
Rajaram Shelar

Reputation: 7877

If you have TortoiseGit, even before committing also you can export all the changed files to folder (which contains files under proper directory structure). Just perform following steps.

  1. Right click on folder which you want to see changes
  2. Select Commit to
  3. Once files are visible in box, select all files
  4. Right click - > Export selection to...
  5. Select folder where you want to put these files.
  6. Done.

Upvotes: 1

BRT
BRT

Reputation: 52

Assuming you don't have deleted or renamed files, this should do the trick

Copy:

cp --parents $(git status -s | egrep "M|A|AM" | rev | cut -d" " -f1 | rev) destination_folder

Zip:

zip modified.zip $(git status -s | egrep "M|A|AM" | rev | cut -d" " -f1 | rev)

Upvotes: 2

Saifullah khan
Saifullah khan

Reputation: 778

Zip the modified and newly created files in the git repository

zip mychanges.zip $({ (git ls-files --others --exclude-standard) ; (git ls-files --modified)})

Upvotes: 10

Sven Marnach
Sven Marnach

Reputation: 602035

To do exactly what you requested (assuming you already committed and want to create an archive of the files changed by the last commit), you could do:

git archive --format=zip HEAD `git diff HEAD^ HEAD --name-only` > a.zip

If you have removed files in a commit, to prevent a pathspec error use --diff-filter=d:

git archive --format=zip HEAD `git diff --diff-filter=d HEAD^ HEAD --name-only` > a.zip

But maybe you actually want to create a patch using:

git diff HEAD^ HEAD > a.patch

and apply this patch where you need it using:

patch -p1 < a.patch

Of course, applying a patch only works if your target directory already contains the old version of your repository.

Upvotes: 81

ChauhanTs
ChauhanTs

Reputation: 469

Here is a script which can make this process a lot easier, it will copy all changed file to used defined directory and also maintain the directory structure of code base.

run: sh scr.sh

================================================

#!/bin/sh
FILES=`git ls-files --modified`
for x in $FILES
do
        prev_dir=$PWD
        echo "MY Dir = $prev_dir"
        mkdir -p $1/$x
        cd $1/$x
        cd ../
        rm -r *
        cp $prev_dir/$x ./.
        cd $prev_dir
done

================================================

Upvotes: 0

user3448451
user3448451

Reputation: 31

mkdir -p /c/temp/blah && cp $(git diff <your commit hash> --name-only) /c/temp/blah

I'm using Git Bash on windows.

Upvotes: 1

Aftershock
Aftershock

Reputation: 5351

If you use TortoiseGIt, it provides this too.
Choose the folder, in explorer
Right click,Choose menu, TortoiseGit-> Show Log.

Select working directory and the last commiitted version.
Right click. Compare revisions. Select files you want to save/export.
Right Click. Export to folder. Done.

Upvotes: 22

Related Questions