Add updated files only

The secret option to save you some face
The git logo with the title of the article superimposed next to it.

Keep those untracked files out!

Sometimes you're just a hair too quick when working with git, run a little git add -A or git add ., and then let an unwanted file or two come along for the ride. Regardless of whether it's just random scribbles or actual secrets, it's pretty annoying. And if no-one notices before you push it to the remote (or open a merge request), it could be a good few levels worse. Today I learned a way to avoid this, and I want to share it with you, dear reader.

Now, just to be clear: I don't actually recommend you blindly add all files in a directory or project to git. It's usually better to be explicit about what you're adding. Sometimes, though, it's just easier to add everything. Claiming that I've never done it or that it's never caused me grief would be disingenuous at best.

So what's the solution you ask? It's a simple flag you can pass to git add: -u or --update. Much like the flag name would suggest, this makes it add only updated files.

The docs say that this option will "/update the index just where it already has an entry matching <pathspec>. This removes as well as modifies index entries to match the working tree, *but adds no new files*/". So you can point it to a directory and it will add all changed or deleted files in that directory, but no new files.

You can also use it without a path, which will just add all deleted and modified files: "/If no <pathspec> is given when -u option is used, *all tracked files in the entire working tree are updated*/".

So next time you want to blindly add all your changes (but no new files!) to git:

git add -u

Psst! Here's a little bonus resource for ya: For more information on the behavior of git add with different options, check the answers to this stack overflow question, which is what put me on the trail of git add -u in the first place.

Thomas Heartman is a developer, writer, speaker, and one of those odd people who enjoy lifting heavy things and putting them back down again. Preferably with others. Doing his best to gain and share as much knowledge as possible.