Why would I want stage before committing in Git?

When you commit it’s only going to commit the changes in the index (the “staged” files). There are many uses for this, but the most obvious is to break up your working changes into smaller, self-contained pieces. Perhaps you fixed a bug while you were implementing a feature. You can git add just that file (or git add -p to add just part of a file!) and then commit that bugfix before committing everything else. If you are using git commit -a then you are just forcing an add of everything right before the commit. Don’t use -a if you want to take advantage of staging files.

You can also treat the staged files as an intermediate working copy with the --cached to many commands. For example, git diff --cached will show you how the stage differs from HEAD so you can see what you’re about to commit without mixing in your other working changes.

Leave a Comment