Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make quick backup of untracked files which I want to delete by git clean?

I have a lot of untracked files. I am pretty sure that most of them I can delete, however... you know, backup could be helpful ;)

What you are doing in similar situation?

like image 879
noisy Avatar asked Mar 05 '11 17:03

noisy


People also ask

Does git clean remove untracked files?

By default, git clean will only remove untracked files that are not ignored. Any file that matches a pattern in your . gitignore or other ignore files will not be removed. If you want to remove those files too, you can add a -x to the clean command.

Which command removes untracked files?

You can use the git clean command to remove untracked files. The -fd command removes untracked directories and the git clean -fx command removes ignored and non-ignored files. You can remove untracked files using a . gitignore file.


2 Answers

The following command will create a tar archive in your home directory of all of the untracked (and not ignored) files in your directory:

git ls-files --others --exclude-standard -z | xargs -0 tar rvf ~/backup-untracked.tar

If you're going to use this technique, check carefully that git ls-files --others --exclude-standard on its own produces the list of files you expect!

A few notes on this solution might be in order:

  • I've used -z to get git ls-files to output the list of files with NUL (a zero byte) as the separator between files, and the -0 parameter to xargs tells it to consider NUL to be the separator between the parameters it reads from standard input. This is a standard trick to deal with the possibility that a filename might contain a newline, since the only two bytes that aren't allowed in filenames on Linux are NUL and /.

  • If you have a huge number of untracked files then xargs will run the tar command more than once, so it's important that I've told tar to append files (r) rather than create a new archive (c), otherwise the later invocations of tar will overwrite the archive created just before.

like image 196
Mark Longair Avatar answered Oct 15 '22 02:10

Mark Longair


Generally if they are ever going to be useful, then you should be checking them in! If you don't need them right now, then check them in and remove them! IE, track them as deleted files. That way you can always recover them later if you decide you do need them.

Remember, a fundamental rule of using a version control system is "track everything". Small changes, broken changes (in a branch), etc. You never know when you might need those files or changes again, so use your VC system to make sure you can't lose them.

like image 41
Wes Hardaker Avatar answered Oct 15 '22 04:10

Wes Hardaker