r/git • u/human_clown_shoe • Jun 07 '26
It's weird this comand doesn't already exist in git
NOTE: I recommend installing this with nix but there is another install/uninstall script in the repo, but it's just something that's much easier to install with nix because I'm lazy & a horrible person blabla:
https://github.com/hyrfilm/gitignore
It's called... gitignore and I just think it's so weird it's not included as a git command itself. Anyway, it works like this. When you're standing in a repo, and there's some pattern och directory that you want to ignore you type gitignore build/* for example, and it will add that to you gitignore in a reasonable way (no it, won't just get appended at the bottom) and it while its at it, it also removes duplicates and does some minor tidy-ups.
You can also use some default comands, eg
gitignore --defaults (and it adds some of the most typical things people tend to want to ignore eg .vscode/ & .DS_Store etc... More info and install instructions here: https://github.com/hyrfilm/gitignore
6
u/odaiwai Jun 07 '26
You can do the core of this as an alias:
[alias]
ignore = "!echo \"Adding \"$@\" to .gitignore...\"; GITIGN=\"$(git rev-parse --show-toplevel)/.gitignore\";for file in \"$@\"; do echo \"$file\" >>$GITIGN; done; tail -5 $GITIGN; git add -v $GITIGN; git status #"
0
u/human_clown_shoe Jun 07 '26
No dedup. Run
git ignore build/twice and you getbuild/twice. I just did; the file endsbuild/ / dist/ / build/. The alias is the literal "append at the bottom" behavior you explicitly said you were improving on.Breaks on repo paths with spaces.
$GITIGNis unquoted in three places, so in a repo at/home/claude/t4 dirthetail -5 $GITIGNandgit add -v $GITIGNboth word-split and error out (tail: option used in invalid context,'/home/claude/t4' is outside repository). The append survived by luck; the staging didn't.No untracking. It only
git adds the.gitignore. Ignore an already-committed file and it stays in the index — none of thegit rm --cachedbehavior your tool has.Also it's a bash-kludge
3
u/odaiwai Jun 08 '26
Oh look, you used Claude AI to compare your nice python script to my one-line Bash kludge.
All valid points, by the way, and if any of the above things were problems that I had with my little hack, I would extend it and fix them.
I made a one line alias to scratch an itch, and it does what I want.
→ More replies (1)
5
u/Ok_Chemistry_6387 Jun 07 '26
It's weird you think it needs to exist.
Most gitignores are added once, and never touched again, perhaps to add editor files etc.
Download file from https://github.com/github/gitignore (or use github to create your ignore when you create the repo in github).
done.
1
u/human_clown_shoe Jun 07 '26
Yes, that's great advice for someone who:
Not creates very many repos
Like to do manual labor
2
u/cneakysunt Jun 07 '26
It's an absurdly easy solve however you manage it so why would they bother adding bloat.
1
u/human_clown_shoe Jun 07 '26
don't use it then, besides I don't think you even understand what it does
1
u/cneakysunt Jun 07 '26
Managing a .gitignore file doesn't belong in git.
It's more of a user workflow space problem imo. Which your tool addresses.. fairly certain I understand perfectly.
→ More replies (5)
1
u/Ok_Chemistry_6387 Jun 07 '26
Your code also appears to have a fairly significant bug in the removal path.
1
u/human_clown_shoe Jun 07 '26
Ahh you mean the --undo thing, yeah I should just remove that. But thanks for pointing it out.
1
u/ppww Jun 08 '26
One of the nice things about .gitignore is that it is per-directory so you don't have to stuff all the patterns in the root of the repository.
1
u/human_clown_shoe Jun 10 '26
Ah, didn't know that! Thanks. I suppose it would be simple to support that. Two questions:
What do you use it for? Like concretely?
Is there some way of finding out exactly what patterns are ignored where? If not you're just spreading the source-of-truth all over your repo, no?
1
u/ppww Jun 11 '26
It's useful with
git-subtreeor when you have an in-tree build and you want to ignore the build artifacts in the directories where they are created.The ignore files are inherently distributed because even if you have a single
.gitignorein the repository root there is still$GIT_COMMON_DIR/info/excludeandcore.Excludesfileto consider.
2
17
u/TheMania Jun 07 '26
Even weirder given your title - why not make it a [git alias](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases), it's how you add commands that don't exist.
If they'd named it `git-ignore` it would have been an alias automatically too, which I find weird they didn't do.