What's the use of the staging area in Git?

前端 未结 5 1018
青春惊慌失措
青春惊慌失措 2021-02-01 14:54

What is the point of git add . or git add to add it to the staging area? Why not just git commit -m \"blabla\"?

5条回答
  •  萌比男神i
    2021-02-01 15:46

    The staging area is like a rough draft space, it's where you can git add the version of a file or multiple files that you want to save in your next commit (in other words in the next version of your project).

    Note that you can copy versions of files into the staging area and also take them out of the staging area before you make your commit which is why I referred to it as a rough draft space.

    What the git add command actually does is copy that version of your file from your working directory to the staging area.

    (This is a common misconception, people may think in their mental model that the file is moved but actually it is copied.)

    The journey a file takes to have an updated version of it added to your repository:

    • 1️⃣It is edited in your working directory. The working directory is like a work bench it is where you edit your files, add new files and delete files.
    • 2️⃣Then the file is added to the staging area using the git add command
    • 3️⃣Finally it is included in the next commit when you use the git commit command

    The nice thing about being able to choose which files to add to the staging area and to include in a commit is that you can organize your work better this way.

    You can add all the updated files that are related to one piece of work and when you make a commit you can add a message that mentions that piece of work.

    This way you can organize your commits better.

    This video

提交回复
热议问题