Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a staging process in git?

Tags:

git

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?

like image 281
fasseg Avatar asked Jul 23 '11 19:07

fasseg


People also ask

What does it mean to stage a commit?

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.

What to do after staging in git?

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.


1 Answers

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.

like image 179
Andy Avatar answered Sep 25 '22 00:09

Andy