2

I want to "pluck" (if that's a good word for it) just the unstaged files and copy them to a different directory. I don't want any other files in the repo... just these unstaged files.

Is there a git command or shell + git command to do this?


Updating question to respond to questions:

  • I want to keep the file structure and files
  • I am open to any combo of git and shell wizardry
Nathan
  • 7,627
  • 11
  • 46
  • 80

3 Answers3

8
git ls-files -m | tar Tc - | tar Cx /path/to/other/dir

and if windows wasn't a factor I'd do the tarpipe thing with just cpio -pd /path/to/other.

jthill
  • 55,082
  • 5
  • 77
  • 137
0
prefix=/xxx/
git status --porcelain | grep -e "^ M " | while read mark path
do
mkdir -p $prefix/$(dirname "$path")
cp -v "$path" $prefix/"$path"
done

prefix is the root folder of the backups.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53
-1

If you use -s for --short you can get the list of files in a nice (porcelain) way:

$ git status -s
 M dir1/file1.py
 M dir2/file2.py

So then it is just a matter of looping through this list using process substitution:

while IFS=" " read -r _ file; do
   echo "$file --"  # or whatever "mv" you want to do
done < <(git status -s)

This slurps the first column using the throw away variable _ and the rest is stored in $file.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598