Question:
I’m new to version control and I understand that “committing” is essentially creating a backup while updating the new ‘current’ version of what you’re working on.
What I don’t understand is what staging for is from a practical perspective. Is staging something that exists in name only or does it serve a purpose? When you commit, its going to commit everything anyway, right?
Edit: I think I may be confusing the terminology. Is a ‘staged’ file the same thing as a ‘tracked’ file?
Answer:
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.