Why is there the staging area between "git add" and "git commit"? I understand the concept, but fail to see the sense in adding files to the staging area before actually commiting. Why not skip this step?
To stage a file is simply to prepare it finely for a commit. Git, with its index allows you to commit only certain parts of the changes you've done since the last commit.
When you're ready to save a copy of the current state of the project, you stage changes with git add . After you're happy with the staged snapshot, you commit it to the project history with git commit . The git reset command is used to undo a commit or staged snapshot.
The truth is: the index is a staging area. Every SCM has it, but git shows it to you, and uses it effectively.
One main reason is so you don't have to commit your entire working directory. You can move portions of it to the index and commit just those.
For example, you're working on two different things, and now your code looks like
//random code
//bug fix
//new feature
You can stage just the //bug fix
line and commit that, and than stage the //new feature
line and commit that.
You now have two different commits for each. Later on in testing you realize the new feature
commit broke something, you can delete the new feature
commit, but don't have to recommit the bugfix
As Dickon Reed noted in his answer, you also gain performance as git commit
now only needs to add what is in the index to the commit. It doesn't need to search through your tree to find all the changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With