🎏 This is the 18th day of my participation in the Gwen Challenge. Check out the details: Gwen Challenge

0 x00 📢 preface

👇 Git getting started is linked below. Read the articles in that order at 👇.

Git goes from quitter to starter column

This is the sixth article, through the combination of pictures and texts to learn the reset command function and working principle.

For better difference analysis, the initialized project is used as the default operation directory. Refer to the project initialization Settings above for details

0x01 Reset

The git reset command is used to reset the git file. It can move the HEAD pointer and optionally change the staging area (index) and working directory based on the option parameters.

The common syntax format is as follows:

git reset [<mode>] [<commit>]
Copy the code

The mode default value is –mixed, and the options include –soft, –mixed, –hard, –merge, –keep, etc. This article only introduces –soft, –mixed, –hard.

Commit specifies the commit node (branch) to reset. The default is HEAD.

The submission node can be specified using either the HEAD~N syntax or the ^ symbol: HEAD The current submission node HEAD^ The parent of the current submission node is equivalent to HEAD~ HEAD^^ The parent of the current submission node is equivalent to HEAD~2

Run the following command to view the initial project information.

Git commit history
$ git hist --all
Git status
$ git st
NPM install treer -g
$ treer -i '.git'
# check index contents
$ git ls-files -s     
Copy the code

– soft options

Run git reset –soft HEAD~2. Reset moves the HEAD branch, unlike checkout moves the HEAD itself.

You can see that the point of the HEAD branch moves from node 7416512 to node 8e47817. Because the submission object of this node branch is inconsistent with the content of the staging area, the readme. md file is added and the file. TXT file is modified when the file status is checked. Index and working directory contents are not affected.

– mixed options

Git reset [–mixed] HEAD~2 run git reset [–mixed] HEAD~2. The mode default value is –mixed. Reset updates the staging area (index) with the contents of the current snapshot pointed to by HEAD.

Run git st (Git status) and git ls-files-s to check the file status and index.

  • Temporary area (index) content updated, onlyfile.txtConsistent with the current snapshot,Readme.mdThe file is removed.
  • File status is displayed in the working directoryfile.txtChanged but not staged compared to the contents of the staging area (index);Readme.mdFiles identified as untraced (Untracked), which means Git didn’t have this file in the previous snapshot (commit) and needs to use itgit addCommand to add trace.

– hard option

Run git reset –hard HEAD~2 and it continues the –mixed operation overwriting the index and overwriting the contents of the index in the working directory.

The only dangerous use of the reset command is the hard flag, which actually destroys data. Any other form of reset call can be easily undone, but the –hard option cannot, because it forces the files in the working directory to be overwritten.

conclusion

The reset command overwrites the HEAD, Staging Area, and Working Directory in a specific order, stopping when specifying the following options:

  1. Move the point of the HEAD branch (stop there if –soft is specified)
  2. Make the staging area (index) look like HEAD (stop here if not specified or –mixed is specified)
  3. Makes the working directory look like a staging area (index) (if –hard is specified)

The specified path

The reset command provides an action path and is often used to cancel pending changes. The syntax is as follows.

git reset [commit] <paths>
Copy the code

Run git reset file.txt (equivalent to git reset –mixed HEAD file.txt) to partially update the index by a specified file or collection of files.

  • Move the point of the HEAD branch (skipped)
  • Make staging area (index) look like HEAD (end of command run)

In the initial project, change the file.txt content to v6 and use git add to temporarily store the changes.

After you run the git reset file. TXT command, a message is displayed indicating that file. TXT has changed and has not been saved temporarily.

Next, use git checkout — file.txt to overwrite the working directory file from the staging area, where file.txt is v5.

Impact quick lookup table

A tree is a collection of files, not a specific data structure.

Git as a system manages and manipulates these three trees in its usual way:

The tree use
HEAD The snapshot of the last commit and the parent of the next commit
Staging Area ( Index ) A snapshot of the expected next commit
Working Directory sandbox

Quick lookup table of the impact of commands on trees

The command HEAD Staging Area/Index Working Directory
Specify the submission
reset --soft [commit] REF NO NO
reset [--mixed] [commit] REF YES NO
reset --hard [commit] REF YES YES
checkout <commit> HEAD YES YES
The specified path
reset [commit] <paths> NO YES NO
checkout [commit] <paths> NO YES YES

REF indicates that the command moves the reference to the branch pointed to by HEAD. HEAD indicates that only the HEAD itself is moved. YES indicates that the tree is affected

0 x02 📚 reference

“Reset reveal “,ebook “Git reference “,ebook “Git reset”,docs

0x03 Attention column

This article has been included in the column 👇, you can directly follow.

Read more | The series continues to be updated