All Articles

Copy Your Modified Files in Git with 1 line

For one reason or another, I occasionally find myself in a place where I want to copy off all my modified files in my local git repository and start over. This is usually due to painful merges where I just want a fresh start. This is the one-liner you can use to do just that:

git status --porcelain=v1 | awk {'print $2'} | xargs -I {} cp -r {} ../dir_with_changed_files

Let’s break down each part of this line in detail.

git status --porcelain=v1

You may be familiar with git status, adding the --porcelain=v1 flag just changes the output from this:

git status image

to this:

git status porcelain image

awk {'print $2'}

This awk command will print the 2nd column of standard out (i.e. what is printed on the screen). In our case, this will print out the column with the file names from the previous git status --porcelain=v1 command.

xargs -I {} cp -r {} ../dir_with_changed_files

This portion of the command utilizes the xargs tool which allows iteration over things coming from standard input. In this case, standard input for the xargs command is the standard output from the previous awk command. Which take the list of files and copies it to the ../dir_with_changed_files directory.

Published May 14, 2019

I love coffee, coding and writing.