Management of change

  • Now, assume that you have a complete grasp of the concept of staging areas. Next, we’ll discuss why Git is better designed than other version control systems because it tracks and manages changes, not files.
  • What is a modification, you ask? If you add a line, it’s a change, if you delete a line, it’s a change, if you change some characters, it’s a change, if you delete some characters, it’s a change, it’s a change, if you create a new file, it’s a change.
  • Why does Git manage changes, not files? Let’s do the experiment. First, make a change to readme.txt, such as adding a line of content :(adds the last line of content)
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes.
Copy the code
  • Then, add:
$ git add readme.txt
Copy the code
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   readme.txt
Copy the code
  • Then, modify readme.txt again:
$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
Copy the code
  • Commit:
$ git commit -m "git tracks changes"
[master 519219b] git tracks changes
 1 file changed, 1 insertion(+)
Copy the code
  • After submission, take a look at the status:
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")
Copy the code

Why didn’t the second change be submitted?

Calm down, let’s review how it works:

  • First change -> git add -> Second change -> git commit
  • You see, we talked about, in front of the Git management is to change, when you use Git add command, after changes are for the first time in the work area into the staging area, ready to submit, however, in the work area the second change is not in the staging area, so the Git commit only responsible for the commits the staging area, also is the first time changes are submitted, The second change will not be committed.
  • After submission, usegit diff HEAD -- readme.txtCommand to viewThe workspaceandVersion of the garageDifferences in the latest version:
$ git diff HEAD -- readme.txt diff --git a/readme.txt b/readme.txt index db28b2c.. 9a8b341 100644 -- a/readme.txt +++ b/readme.txt @@-1,4 +1,4 @@git is a distributed version control system free software distributed under the GPL. Git has a mutable index called stage. -Git tracks changes. +Git tracks changes of files.Copy the code
  • As you can see, the second change was not committed.
  • So how do YOU commit the second change? You can go ongit addagaingit commitOr you can take your time submitting the first changegit addThe second revision, and thengit commit, which is equivalent to combining the two changes and committing them together:
  • First change ->git add-> Second change ->git add -> git commit

summary

  • Now that you understand how Git keeps track of changes every time they are made, if not usedgit addTo the staging area, then you won’t joincommitIn the.
  • git diff filename: Compares the workspace with the staging area
  • git diff HEAD -- filename: Compares the latest version of the workspace and repository
  • ifgit diffOutput blank indicates that the workspace is clean (clean should mean the same as the area being compared)