0

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).

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90

2 Answers2

2

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
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
0

Use Git Status

Git and RCS have different use cases. It's extremely unusual to want to clear out your Git working tree, so I'm going to assume that this is an X/Y issue where you're trying to see what files are currently untracked.

There's a built-in Git command for this: git status --untracked-files. The default mode of this command is all, as opposed to the output of git status (without flags) which defaults to normal. See man 1 git-status for details.

Example

mkdir -p foo/bar/baz
touch foo/quux foo/bar/wibble
git status -u
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

  foo/bar/wibble
  foo/quux

nothing added to commit but untracked files present (use "git add" to track)

Caveat: Empty Directory Trees

Note that directory trees without files are never shown in this output. That's because Git doesn't track directories except as part of tree objects, so foo/bar/baz from this example will never be shown as untracked unless a file (a.k.a. directory entry) exists within or below baz in the directory tree.

In other words, for baz to show up as untracked, there must either be a file in baz or a subdirectory of baz must contain a file. For example, using the --short flag for more concise output:

$ touch foo/bar/baz/file; git status -s -u
?? foo/bar/baz/file
?? foo/bar/wibble
?? foo/quux

will include foo/bar/baz/file in the output. People often use empty hidden files like .gitkeep for just this purpose.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199