[Very important] In-depth understanding of "modification" in Git

Glossary

staged: has been staged, indicating that the current file has been put into the cache area by the git command
unstager: not yet staged, meaning that it has not been placed in the cache yet

0. Note: Git manages changes, not files

For now, assume you've fully grasped the concept of scratch pads. Below, we will discuss why Git is better designed than other version control systems, because Git tracks and manages changes, not files.

What is modification, you ask?

For example, if you add a new line, this is a modification. If you delete a line, it is also a modification. If you change some characters, it is also a modification. If you delete some and add some, it is also a modification. Even creating a new file is considered a modification. a modification.

1. Git manages changes

Git manages modifications. When you use the git add command, the first modification in the workspace is put into the staging area, ready to be submitted, but the second modification in the workspace is not placed in the staging area. Therefore, git commit is only responsible for submitting the modifications in the staging area, that is, the first modification is submitted, and the second modification will not be submitted.

Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ git add readme.txt

Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   readme.txt


Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ git commit -m "git tracks changes"
[master 30ea488] git tracks changes
 1 file changed, 2 insertions(+), 1 deletion(-)

Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ 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")

After submitting, use the git diff HEAD - readme.txt command to view the difference between the latest version in the workspace and the repository:

$ git diff HEAD -- readme.txt
diff --git a/readme.txt b/readme.txt
index 3fc510d..700e9d2 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,4 +1,4 @@
 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.
\ No newline at end of file
+Git tracks changes of files.
\ No newline at end of file

2. Undo changes

Principle explanation

The command git checkout -- readme.txt means that all modifications to the readme.txt file in the workspace are undone. There are two situations:

  • One is that readme.txt has not been placed in the temporary storage area since it was modified. Now, undoing the modification will return to the same state as the version library;
  • One is that readme.txt has been added to the temporary storage area and then modified. Now, undo the modification and return to the state after being added to the temporary storage area.

In short, it is to return this file to the state of the last git commit or git add.

Experiment 1 made changes to the file, but it has not been added to the cache

Use git checkout --[file] to throw away changes from current workspace

Current Git status

$ 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.
My stupid boss still prefers SVN.
Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ 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")

We want him to fall back to not writing the last sentence

$ git checkout -- readme.txt

Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ 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.

Sure enough, it's back to the state just now, I'll look at it again, the current state of Git

Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
nothing to commit, working directory clean

Nothing, this is not the end!

The - in git checkout - file command is very important, without -, it becomes a "switch to another branch" command

Experiment 2 Modified the file and added it to the cache

Use git reset to roll back the modification of the file from the staging area to the workspace, and then use git checkout to throw away the modifications in the workspace

Git also tells us that using the command git reset HEAD <file> can undo the changes in the staging area (unstage) and put it back in the workspace:

$ git reset head readme.txt
Unstaged changes after reset:
M       readme.txt

Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ 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")

The git reset command can either roll back the version or roll back the changes in the staging area to the workspace. When we use HEAD, it means the latest version

The job now is to discard the changes in the current workspace

Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ git checkout -- readme.txt

Administrator@WIN-E2URN8GKKHJ MINGW64 ~/Desktop/learngit (master)
$ git status
On branch master
nothing to commit, working directory clean

Really comfortable now!

Experiment 3 The file has been modified and pushed to the local repository

You can use git reset --hard 1094a to roll back the head pointer to the previous version, which will also be synced to the local file.

Tags: git github

Posted by matchew on Sat, 15 Oct 2022 01:38:27 +0300