This is the 13th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

This is a note I wrote many years ago when I first learned Git.

Git Fundamentals

  • Working Directory: Specifies the Directory that can be viewed on the computer
  • Repository: Your workspace has a hidden directory, git. This is not a workspace, but a Repository for Git.
  • Stage (or index) : Staging area
  • Git automatically creates the first branch for us, master, and a pointer to master called HEAD

Note:

  • Git tracks and manages changes, not files.
  • Every change that is not git added to the staging area will not be added to the COMMIT.

Git Introduction

  • After installing Git, you should first do some basic configuration, such as setting the default username and email address:
$ git config --global user.email "[email protected]"
$ git config --global user.name "Your Name"
Copy the code
  • Create a Git repository (a folder with a Git managed version) :
$ mkdir new_rs
$ cd new_rs
$ git init
Copy the code

Git init creates a subdirectory./. Git. The repository is based on this directory, and the contents are maintained by Git itself.

  • Add files to Git repository:
$ git add file_0
$ git add file_1 file_2
$ git commit -m "commit message"
Copy the code

The “Commit Message” should be a brief description of what the change does so that you can refer to it later.

  • Check the current state of the workspace (any files that have changed but have not been added or committed)
$ git status
Copy the code
  • See the changes in the file

Git diff: git diff: git diff: git diff:

$ git diff
Copy the code
  • Go back to a past version

To view the committed version history:

$ git log
Copy the code

If this is too verbose, use: git log — pretty=oneline to display one record in a row.

In the log result, we can see the top record (HEAD), which is the last committed version. Each version is uniquely identified by a string of SHA1 codes as an ID.

To go back to the previous version:

$ git reset --hard HEAD^
Copy the code

HEAD^ is the previous version, HEAD^^ is the previous version, HEAD~100 is the last 100 versions.

When you revert to the previous version, the last version of the Git log (the HEAD before it) disappears, but not completely. Use the ID of the missing version (the first few bits will do, not the whole list) to restore:

$git reset -- hard 1094aCopy the code

To restore is to change the current version, which is to point the HEAD to a different version number.

But if you can’t find the previous version ID, you can use:

$ git reflog
Copy the code

Git reflog keeps a history of each command (operation record), which allows you to find new versions that have disappeared.

  • View the differences between the latest version of the workspace and the repository
$ git diff HEAD -- readme.txt
Copy the code
  • Undo changes (discard workspace changes) (when you changed the contents of a file in your workspace and want to discard workspace changes directly)
$ git checkout -- readme.txt
Copy the code

There are several possible scenarios:

  1. readme.txtIt has not been put into the staging area since the change was made. Now, undo the change and return to the exact same state as the repository.
  2. readme.txtOnce you’ve added it to the staging area, you’ve made changes, and now you undo the changes and return to the state you were in after you added it to the staging area.
  • Unstage the staging changes and place them back in the workspace
$ git reset HEAD readme.txt
Copy the code

There are also several different scenarios:

  1. When you make changes to a file in your workspace and want to throw out your workspace changes, use the git checkout — file command.

  2. When you change the contents of a file in your workspace and add it to the staging area, you want to discard the change in two steps. The first step is to use the git reset HEAD file command to return to scenario 1, and the second step is to use scenario 1.

  3. When an inappropriate change has been committed to a repository and you want to undo the commit, see the version Rollback section, but only if it has not been pushed to a remote repository.

  • Delete the file

First rm in the system, then git RM