gitversion-controlrcs

How do i do a clean with git like rcsclean


i am used to rcs. after checking in, i usually do an rcsclean to remove all of the tracked fines that are present, so that only untracked files are present in the directory. i sometimes do an rcsclean -u to undo any changed to tracked files.

how do i do this in git?

afterwards there should be only untracked files in the current directory and it's subdirectories (except of course for the .git repository itself).


Solution

  • TL;DR

    Git isn't RCS. You can do what you want, but you will cause yourself long-term suffering by trying to treat a Git repository as a file-based revision control system.

    You can remove the files Git is tracking, but projects with larger/deeper directory trees will require you to take extra steps to remove empty directories (which Git doesn't track as first-class objects), and you will lose the ability to do things like git commit -a without Git thinking you're deleting all your files.

    Removing Files and Directories

    You can remove all the files Git knows about by using the ls-files subcommand. You need to pipe the output to xargs because the rm command doesn't handle null-terminated strings natively, and paths like "foo bar/baz quux" would cause problems without them.

    To remove all files committed to Git:

    $ git ls-files -z | xargs -0 -- rm -f
    

    However, Git doesn't really track directories; it tracks trees. So, unless you have all your files in the root of the project, you'll need to remove the empty directories yourself as a separate command if you want them gone too. For example:

    $ find . -type d -empty -not -path '*.git*'
    

    Commands to Avoid After Cleaning

    Once you've done this, you'll need to avoid commands like git commit -a which will treat all the missing files as deleted. Instead, you'll have to stage files much more carefully. You might also want to investigate man git-update-index and consider whether the --assume-unchanged flag will help your situation or make it worse.

    In addition, the git status command will continue to show lots of changes because you've deleted files from the working tree. If you have large numbers of files that you've "cleaned" from your working tree, the output of commands like git status may become so cluttered that it becomes useless without filtering. For example, to find files that are really untracked rather than deleted or modified:

    $ git status -su | fgrep '??'
    ?? bar
    ?? wibble