Work area and staging area

  • One thing that differentiates Git from other version control systems such as SVN is the concept of staging.
  • Let’s start with the noun explanation.
  • Working Directory
  • The ones you see on your computer, like minelearngitA folder is a workspace:

  • Repository Repository
  • A workspace has a hidden directory. Git, which is not a workspace, but a git repository.
  • Git repositories contain many things, the most important of which is calledStage (or index)The staging area of Git, and the first branch that Git automatically created for usmaster, and pointing tomasterA pointer to theHEAD.

  • Branches andHEADWe’ll talk about that later.
  • Adding files to Git repository is done in two steps:
  • The first step is to usegit addI’m going to add the file,You are essentially adding file changes to the staging area;
  • The second step is to usegit commitCommit changes,This essentially commits the entire contents of the staging area to the current branch.
  • Because when we create a Git repository, Git automatically creates a unique one for usmasterBranch. So, now,git commitIs tomasterCommit changes on the branch.
  • You can simply say that all changes to the file that need to be committed are put into the staging area, and then commit all changes to the staging area at once.
  • As the saying goes, the proof is in the pudding. Now, let’s do it again. Rightreadme.txtMake a change, like adding a line that says:
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Copy the code
  • Then, create a new one in your workspaceLICENSEText file (write whatever you want).
  • Git status
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   readme.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        LICENSE

no changes added to commit (use "git add" and/or "git commit -a")
Copy the code
  • Git tells us very clearly thatreadme.txtHas been modified, andLICENSEIt’s never been added yet, so its state isUntracked.
  • Now, use the command twicegit add,readme.txtandLICENSEGit status:
$ git add readme.txt LICENSE
Copy the code
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   LICENSE
        modified:   readme.txt
Copy the code
  • Now, the state of the staging area looks like this:

  • So,git addThe command essentially puts all the changes to commit intoStaging areaThen, executegit commitYou can commit all changes to the staging area to the branch at once.
$ git commit -m "understand how stage works"
[master b1f1f31] understand how stage works
 2 files changed, 3 insertions(+), 1 deletion(-)
 create mode 100644 LICENSE
Copy the code
  • Once committed, the workspace is “clean” if you haven’t made any changes to it:
$ git status
On branch master
nothing to commit, working tree clean
Copy the code
  • Now the repository looks like this, and the staging area is empty:

summary

  • Staging area is a very important concept in Git. Understanding staging area will help you understand what Git does.
  • Git files are divided into workspace, version repository, and version repository are divided into stage and master(repository).
  • Work area >>>> staging area >>>> warehouse
  • git addRemove the file from the workspace >>>> staging area,git commitRemove the files from the temporary storage area >>>> warehouse,
  • git diffView differences between workspaces and staging areas