Last Updated: February 25, 2016
·
10.78K
· sequoia

Backup all modified files in git.

I had some issues with modified files that I couldn't easily but didn't want to fully discard my changes, so I wished to create backups of the modified versions.

git ls-files is a great little command that outputs sets of files (as git sees them) without all the decoration used to make git status more readable. I was attempting to parse the output of git status with sed when someone clued me in to git ls-files

View all modified files (one per line)

git ls-files --modified

Edit all untracked files in different tabs

git ls-files --others --exclude-standard | vim -p

Backup all modified files (to an existing ~/tmp directory):

git ls-files --modified | xargs -i cp {} ~/tmp

You can pipe the output to whatever, I just find myself piping to xargs a lot.

Edit: This tip was originally about adding all modified files using ls-files and xargs. Thanks to @mysz and @twolfson for pointing out git add -u achieves the same goal!

7 Responses
Add your response

git add -u ?

over 1 year ago ·

I use "git add -A". Seems simpler... or if "git add -u" does the same thing, it's also simpler.

over 1 year ago ·

I agree with the git add -u comments. It is an alias for git add --update. Here is a snippet from the man git-add page:

-u, --update

Only match <filepattern> against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed.

If no <filepattern> is given, default to "."; in other words, update all tracked files in the current directory and its subdirectories.

I personally prefer git add -A (--all) as well which is

git add -A = git add . && git add -u

This means it adds all modified files and untracked files.

-A, --all

Like -u, but match <filepattern> against files in the working tree in addition to the index. That means that it will find new files as well as staging modified content and removing files that are no longer in the working tree.
over 1 year ago ·

Thank you twolfsen, mysz! The tipper becomes the tipee. :) I'll rewrite this with (hopefully) more useful uses of git ls-files.

over 1 year ago ·

Let me know it the current edit contains any more glaring issues :)

over 1 year ago ·

git stash - automatically stashes modifications and gets you back to a clean working directory

git stash pop - applies the saved modifications on top of your clean working directory

See the man page for more.

over 1 year ago ·

I would use xargs -I instead since -i is deprecated. I would also copy using the --parents option to prevent overwriting files with the same name. To be on the safe side, I would add {} after xargs -I or it could result in "{}: No such file or directory" error on some systems:

git ls-files --modified | xargs -I {} cp --parents {} ~/temp

over 1 year ago ·