We Are All Monica
We Are All Monica

Reputation: 13354

Git: list only "untracked" files (also, custom commands)

Is there a way to use a command like git ls-files to show only untracked files?

The reason I'm asking is because I use the following command to process all deleted files:

git ls-files -d | xargs git rm

I'd like something similar for untracked files:

git some-command --some-options | xargs git add

I was able to find the -o option to git ls-files, but this isn't what I want because it also shows ignored files. I was also able to come up with the following long and ugly command:

git status --porcelain | grep '^??' | cut -c4- | xargs git add

It seems like there's got to be a better command I can use here. And if there isn't, how do I create custom git commands?

Upvotes: 481

Views: 237635

Answers (11)

Mike Slinn
Mike Slinn

Reputation: 8417

The functionality of git status is available from the libgit2 library. A language binding for libgit2 gives much greater control over git functionality than git porcelain commands do. Various language bindings for libgit2 exist. The most popular binding for Ruby is rugged.

The following is a tiny program I wrote while learning how to walk through the git status results using Ruby and rugged. The code iterates through all dirty entries; new (untracked) files are shown in green, ignored files are shown in yellow, and modified files are shown in red.

require 'colorator'
require 'rugged'
repo = Rugged::Repository.new('.')
repo.status do |path, flags|
  msg = "#{path} #{flags}"
  puts msg.green if flags.include?(:worktree_new)
  puts msg.yellow if flags.include?(:ignored)
  puts msg.red if flags.include?(:worktree_modified)
end

I wrote an article about this code here.

Upvotes: 0

Valentyna
Valentyna

Reputation: 579

git status -u

will list all untracked files. Long form:

git status --untracked-files

Upvotes: 53

Mike Lococo
Mike Lococo

Reputation: 715

git add -A -n
will do what you want. -A adds all untracked and modified files to the repo, -n makes it a dry-run where the add isn't performed but the status output is given listing each file that would have been added.

Upvotes: 69

Petr
Petr

Reputation: 21

I think I found a feature/problem with:

git ls-files --others --exclude-standard

wanting to list only untracked files:
if untracked file is in a modified directory, file will not be listed

Upvotes: 1

Dustin
Dustin

Reputation: 91050

If you just want to remove untracked files, do this:

warning this will remove all of you untracked files including directories

git clean -df

add x to that if you want to also include specifically ignored files. I use git clean -dfx a lot throughout the day.

You can create custom git by just writing a script called git-whatever and having it in your path.

Upvotes: 90

Tim Fulmer
Tim Fulmer

Reputation: 19568

I think this will do the same thing as the original poster intended:

git add .

Adding some caveats:

  • You have run git status and confirmed your local directories are clean
  • You have run git diff on each file reported in git status, and confirmed your changes are clean
  • Your changes are covered with automated unit testing
  • Your changes are covered with automated integration testing
  • You have run the integration tests through a debugger, verifying the new behavior by white box observing the new code in action
  • You have run all linting / code convention rules and they pass
  • You have run all unit tests and they pass
  • You have run all local integration tests, and they pass
  • You have deployed your changes to an app review environment and manually tested them end to end in isolation from other changes
  • You have merged latest from main branch and re-run all automated unit and integration testing, fixing any merge conflicts and test failures

Now, my friend, you are ready to git add . with impunity.

Upvotes: -3

Frank-Rene Schäfer
Frank-Rene Schäfer

Reputation: 3352

All previous answers which I checked would list the files to be committed, too. Here is a simple and easy solution that only lists files which are not yet in the repo and not subject to .gitignore.

git status --porcelain | awk '/^\?\?/ { print $2; }'

or

git status --porcelain | grep -v '\?\?'

Upvotes: 9

code_fodder
code_fodder

Reputation: 16411

I know its an old question, but in terms of listing untracked files I thought I would add another one which also lists untracked folders:

You can used the git clean operation with -n (dry run) to show you which files it will remove (including the .gitignore files) by:

git clean -xdn

This has the advantage of showing all files and all folders that are not tracked. Parameters:

  • x - Shows all untracked files (including ignored by git and others, like build output etc...)
  • d - show untracked directories
  • n - and most importantly! - dryrun, i.e. don't actually delete anything, just use the clean mechanism to display the results.

It can be a little bit unsafe to do it like this incase you forget the -n. So I usually alias it in git config.

Upvotes: 11

youurayy
youurayy

Reputation: 1665

The accepted answer crashes on filenames with space. I'm at this point not sure how to update the alias command, so I'll put the improved version here:

git ls-files -z -o --exclude-standard | xargs -0 git add

Upvotes: 33

takeshin
takeshin

Reputation: 50688

To list untracked files try:

git ls-files --others --exclude-standard

If you need to pipe the output to xargs, it is wise to mind white spaces using git ls-files -z and xargs -0:

git ls-files -z -o --exclude-standard | xargs -0 git add

Nice alias for adding untracked files:

au = !git add $(git ls-files -o --exclude-standard)

Edit: For reference: git-ls-files

Upvotes: 733

cmac
cmac

Reputation: 301

When looking for files to potentially add. The output from git show does that but it also includes a lot of other stuff. The following command is useful to get the same list of files but without all of the other stuff.

 git status --porcelain | grep "^?? " | sed -e 's/^[?]* //'

This is useful when combined in a pipeline to find files matching a specific pattern and then piping that to git add.

git status --porcelain | grep "^?? "  | sed -e 's/^[?]* //' | \
egrep "\.project$|\.settings$\.classfile$" | xargs -n1 git add

Upvotes: 9

Related Questions