How to make git ignore ignored in .gitignore
I have a Git repository that contains a file .gitignore
that lists several files (which are generated during build) to be ignored.
$ cat .gitignore
foo
$ ls
bar
$ git status
On branch master
nothing to commit, working directory clean
$ touch foo
$ ls
bar foo
$ git status
On branch master
nothing to commit, working directory clean
$
In a specific clone of this repository, I want to see the files listed in .gitignore
.
This can be done with the option --ignored
before git status
:
$ git status
On branch master
nothing to commit, working directory clean
$ git status --ignored
On branch master
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
foo
nothing to commit, working directory clean
$
Nice. My question is, how can I make this persistent for a given repository , so that I don't have to remember to add the flag --ignored
?
$ git status
On branch master
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
foo
nothing to commit, working directory clean
$
I'm sure there is some git config
duration that allows me to configure my clone to ignore the content .gitignore
...
Also, I don't want to have this option for all repositories; only for a select few.
source to share
none of the suggested solutions did what I really wanted.
however, @Jubobs' suggestion for a wrapper function finally gave me an idea of how to actually do it.
outline
-
mark a given repository for change, preferably using the git way. this can be accomplished with
git config
and some custom variable -
Why stop adding a single option (
--ignored
) to a single command (status
)? it would be nice if we could pass default arguments for each git command,
realization
so I got the following small snippet to be sent on startup:
git() {
local GIT=$(which git)
case "x$1" in
x|xconfig)
${GIT} "$@"
;;
*)
${GIT} $1 $(${GIT} config --get $1.options) "${@:2}"
;;
esac
}
so in my custom repository I just do:
$ git config --add status.options "--ignored"
after that, running the following command on that repo will show me even the ignored files, while the other repos will behave as usual.
$ git status
discussion
-
you can add your personal options to any git command by creating a config option named
${cmd}.options
. for example if you prefer to decorate your outputgit log
, just do:$ git config log.options "--decorate"
-
The script excludes the subcommand
config
for security reasons. this should prevent knee shooting with something like:$ git config add config.options --fuzz
-
finally, it uses some bashisms (namely
${@:2}
) to allow arguments containing spaces; if your shell is notbash
, take care ...
source to share
What you need to do is create an alias (name it istatus
) for the command $ git status
.
$ git istatus
On branch master
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
foo
nothing to commit, working directory clean
How to do this described here and even there a similar question on SO .
TL; DR: Basically you just need to add lines to ~ / .gitconfig:
[alias]
istatus = status --ignored
source to share
Impossible (yet)
Git (at least v2.1 and older) doesn't allow you to configure git status
to ignore files by default.
What Git allows you to do, however, changes the behavior git status
with respect to the raw file with status.showUntrackedFiles
.
Perhaps a future version of Git will have an option " status.showIgnoredFiles
" ...
So what can you do?
1 - Define the alias locally
The obvious and probably best would be, as suggested by Kleskowy in the answer, to define a local alias in your repository:
git config alias.istatus "status --ignored"
Then running git istatus
instead git status
on the repository in question will also result in a list of ignored files.
2 - Define a little wrap around git
Caveat . The following approach is applied at the user level, not at the repository level.
Alternatively, if you don't mind having a little wrapper around git
, you can define the following, which automatically uses the flag --ignored
at startup git status
:
git() {
if [[ ($1 == "status") ]]; then
command git status --ignored "${@:2}";
else
command git "$@";
fi;
}
Put these lines in your file .<shell>rc
and send it last.
Here is the test result (in bash
) of this wrapper in one of my repositories:
Note that ignored files also appear in the list, although the flag is --ignored
not explicitly used.
Conclusion
If someone can't think of a better approach, what you are asking is essentially a feature request for Git; so I'm not sure if your question is the case here ...
source to share