62

When doing git commit, is there a way to not display the untracked files in my editor (defined in $EDITOR)? I know how to do so in the shell (git status -uno), but I'd like do it in editor as well.

Note that I do not want to ignore these files forever; I just don't want to see them on certain occasions.

Mr. Lance E Sloan
  • 3,297
  • 5
  • 35
  • 50
chiggsy
  • 8,153
  • 5
  • 33
  • 43
  • 1
    Possible duplicate of [How do I do a 'git status' so it doesn't display untracked files without using .gitignore?](http://stackoverflow.com/questions/594757/how-do-i-do-a-git-status-so-it-doesnt-display-untracked-files-without-using) – Cos Callis May 15 '17 at 15:28

6 Answers6

100

If you don't ever want to commit them to your repo, use a .gitignore file to ignore them. More details can be found on the gitignore man page. They won't show up as untracked files when entering your commit message in your $EDITOR.

If you simply don't want to see them when committing in any repo, set the Git config variable status.showUntrackedFiles to no, as noted here:

$ git config --global status.showUntrackedFiles no

For applying this to a single repo, instead use:

$ git config --local status.showUntrackedFiles no
Steven C. Howell
  • 16,902
  • 15
  • 72
  • 97
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • 24
    if you want to ignore the untracked files in the current repo only, rather than globally use: `git config --local status.showUntrackedFiles no` – yaitloutou Feb 01 '17 at 00:22
  • 1
    @yaitloutou, Your extension should be added into the answer. – oliversm Jul 28 '17 at 14:09
  • @yaitloutou, will one have to run that command everytime one formats one's computer and checks out that repo, or is that setting save with the repo (by using `--local`) – run_the_race Aug 09 '22 at 19:35
  • 1
    @run_the_race, yes. by default git configs (being local, global, or otherwise) are not persistents. Since each collaborator should have their own. Although, you can add a `.gitconfig` file in the repo root, then include its settings, as explained here https://stackoverflow.com/a/18330114/2245329. In this case, It's easier to just rerun the command once per format though imo – yaitloutou Aug 15 '22 at 07:57
42

From the git-commit man page:

       -u[], --untracked-files[=]
           Show untracked files (Default: all).

           The mode parameter is optional, and is used to specify the handling of untracked
           files. The possible options are:

           ·   no - Show no untracked files

           ·   normal - Shows untracked files and directories

           ·   all - Also shows individual files in untracked directories.

               See git-config(1) for configuration variable used to change the default for
               when the option is not specified.
J. Cordasco
  • 836
  • 6
  • 15
  • Great thing about git is they're really trying to keep command line options consistent between commands with similar functionality. – J. Cordasco May 05 '10 at 15:20
  • Do you know if there is a way to change the setting permanently, but independently for the `status` and `commit` commands? I want to see them with `status`, but not for `commit`, but `git-config(1)` shows only the option `status.showUntrackedFiles` which effects both commands. – Philipp Wendler Feb 09 '13 at 11:21
  • Can I please ask you how to write this command into source tree? I've tried running terminal and write git commit -u --untracked-files=no, but received error. – FrenkyB Oct 20 '17 at 05:21
  • Sorry - it wasn't error, but I still see untracked files. I don't understand where should this command be written - into terminal or somewhere else? – FrenkyB Oct 20 '17 at 05:28
  • @FrenkyB You need to use either -u no or --untracked-files=no, not both. – Mayank Dec 05 '17 at 21:15
8

You can temporary use the git commit option -uno to mask untracked files (git help commit).

If you want a permanent solution use the .gitignore file.

For instance, if you want to ignore the file bar.foo and any file with the .bak extension, you juste have to create a .gitignore file in the root directory of your project containing :

bar.foo
*.bak

Some file are ignored by a global gitignore file (for instance, dot file and directory are ignored).

Lohrun
  • 6,432
  • 1
  • 24
  • 22
5

Add the file names - or templates (wild cards) for the file names - to the .gitignore file and add that to the repository:

git add .gitignore
git commit -m 'Added .gitignore file'

For example, for my Go repository, I have a .gitignore file containing:

*.o
*.a
*.so
*.pl
*.6
*.out
_obj/
_cgo_defun.c
_cgo_export.c
_cgo_export.h
_cgo_gotypes.go
*.cgo1.go
*.cgo2.c
example/example
ifix1/esqlc-cmds.c

I should probably compress the '_cgo_' names with a wild card; the other '.c' file is generated from a '.ec' file so it does not need to be tracked.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
5

There are times notifications aren't wanted about either a changed repo file or a new file that needs to be added to the repo. However, adding the name of the file to .gitignore might not be a good option, either. For example, locally-generated files that other users aren't likely to generate (e.g., files created by an editor) or files of experimental test code might not be appropriate to appear in a .gitignore file.

In those cases, use one of these solutions:

  1. If the file is a changed repo file

    Use the command:

    git update-index --assume-unchanged "$FILE"

    To undo this, use the command:

    git update-index --no-assume-unchanged "$FILE"

    The update-index command doesn't work with new files that haven't been added to the repo yet, though.

  2. If the file is new and not added to the repo

    Add its filename to the repo's exclude file:

    echo "$FILE" >> .git/info/exclude

    This also works for changed repo files, but there isn't a specific undo command for it. The exclude file would need to be edited and the filename deleted from it. Or other commands could approximate it:

    ex -s -c"g/^${FILE}\$/d" -cwq .git/info/exclude

    Note that this overwrites the existing exclude file and if the filename specified contains special characters that could affect the regular expression, the results are unpredictable.

    This usage of the exclude file is recommended by the page "Ignoring files" on GitHub Help.

Mr. Lance E Sloan
  • 3,297
  • 5
  • 35
  • 50
-2

If you are using git-extensions tool in the commit tab just select the files you want to ignore, right click and add them to .gitignore

kumar
  • 1
  • 2