Why is there a staging process in git?

前端 未结 5 1768
旧巷少年郎
旧巷少年郎 2021-01-01 17:20

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

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 17:55

    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.

提交回复
热议问题