4. Basic operation of Git local warehouse - view submission history and version rollback

1. View commit history

There are two commonly used commands to view the git commit history:

# 查看历史提交记录
git log   

# 以列表形式查看指定文件的历史记录
git blame <文件名>

git log

This command will follow our commit timeline, and then list all historical commits.

Example :

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-x018iJol-1670132301876)(../picture/image-20221203221619650.png)]

The submission history information displayed by git log generally includes the following four pieces of information:

However, git logthe command also has many option parameters for us to choose, so that we can control the form of the output submission information.

  • For example, if we want to view relatively short submission information, we can use the --oneline parameter,git log --oneline
  • For example, we view the submission information of the specified author,git log --author=用户名

The following table lists the commonly used option parameters and functions of the git log command:

parameter effect
-p Displays the differences between each update in patch format
–-stat Display file modification statistics for each update
–-shortstat Only show the last line number in --stat modify add remove stat
–-name-only Display modified file list only after committing information
–-name-status Display the list of newly added, modified and deleted files
–-abbrev-commit Show only the first few characters of the SHA-1 instead of all 40 characters
–-relative-date Use a short relative time display (e.g., "2 weeks ago")
–-graph Displays an ASCII graphical representation of the branch merge history
–-pretty Use a different format to display historical commit information. Available options are oneline, short, full, fuller and format (followed by specifying the format)
-(n) Show only the most recent n commits
–-since, --after Only show commits after the specified time
–-until, --before Only show commits older than the specified time
–-author Show only commits related to the specified author
–-committer Show only commits related to the specified committer

For a detailed explanation, please refer to the explanation in the following article:

https://blog.csdn.net/jjlovefj/article/details/86476925

Example :

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-kvjA0nzT-1670132301877)(../picture/image-20221203223842427.png)]

git blame

If you want to view the modification record of the specified file, you can use the git blame command. The command format is as follows:

git blame <file>

Example :

2. Version forward and backward

The essence of Git version forward and backward is to move the HEAD pointer . The HEAD pointer, by default, points to the latest commit (commit). The approximate relationship between HEAD and commit is as follows:

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-2JMJB3OF-1670132301877)(../picture/image-20221203233059324.png)]

To advance or roll back the version, use git resetthe command , so that you can specify to return a certain submitted version.

The syntax format of the git reset command is as follows:

git reset [--soft | --mixed | --hard] [HEAD]

Among them, –soft | --mixed | --hard are 3 parameters respectively.

HEAD parameter description :

  • HEAD indicates the current version
  • HEAD^ previous version
  • HEAD^^ previous version
  • HEAD^^^ Go up to the previous version
  • And so on...

You can use ~ number representation

  • HEAD~0 indicates the current version
  • HEAD~1 Previous revision
  • Previous revision on HEAD^2
  • Previous revision on HEAD^3
  • And so on...

2.1 git reset --mixed

–mixed is the default parameter, which can be omitted when executing the command. This parameter will move the HEAD pointer and reset the index (scratch) area, but the content of the work area remains unchanged.

1. Reset the temporary storage area (that is, cancel the content just added to the temporary storage area)

First pass git addthe command to submit the file to the temporary storage area.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-QiweGoqD-1670132301878)(../picture/image-20221203234732628.png)]

Then, execute git reset --mixedthe command to restore the content just submitted to the temporary storage area, as follows:

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-8B3UBMRD-1670132301878)(../picture/image-20221203235036606.png)]

The command actually omits HEAD, the entire complete command:

git reset --mixed HEAD

HEAD actually means the current version, which is equivalent to canceling the content just added to the cache (temporary storage area).

2. The temporary storage area rolls back to the previous version

git reset --mixed HEAD^            # 回退所有内容到上一个版本  
git reset --mixed HEAD^ hello.php  # 回退 hello.php 文件的版本到上一个版本  

This command is equivalent to rolling back the content of the local warehouse to the previous version (that is, restoring the content of the latest commit), but this command will not change the files in the workspace.

2.2 git reset --soft

The –soft parameter only moves the HEAD pointer, and the contents of the temporary storage area and the work area will not be changed (equivalent to just rolling back to the state before submission).

git reset --soft HEAD^     # 回退到最近一次未提交前的状态
git reset --soft HEAD~3    # 回退到上上上一次未提交前的状态

Example :

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-J0N8yBmZ-1670132301878)(../picture/image-20221204003938306.png)]

The application occasion of the –soft parameter is generally: for example, after we submitted the previous commit, we found that the description information of the submission was wrong , and we want to change the description information of the submission . At this time, we can use the --soft parameter to just roll back the HEAD Pointer (this parameter does not reset the staging area and work area). Then we commit again and write the correct submission description information.

2.3 git reset --hard

–hard This parameter will move the HEAD pointer of the local library, and will also reset the temporary storage area and the work area . Be careful in use.

git reset --hard HEAD^  			# 回退到上一个版本 
git reset --hard HEAD~3  			# 回退到上上上一个版本  
git reset –hard bae128  			# 回退到某个版本回退点之前的所有信息。 
git reset --hard origin/master      # 将本地的状态回退到和远程的一样

2.3.1 Version rollback example

1. First check the current status and submission history of git, and check the contents of the test.c file:

2. Use git reset --hard HEAD^the command to roll back the previous version:

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-lRxTIaKx-1670132301879)(../picture/image-20221204113920223.png)]

After using this command, it is back to the previous version. The latest commit is to submit a new .gitignore file. Now I check the submission history and find that this submission is indeed missing, and I check the workspace directory, and the file has been deleted.

3. Roll back a version again, and check whether the content of test.c has been rolled back:

At this time, the HEAD pointer is equivalent to pointing to the commit before rolling back twice.

2.3.2 Version forward example

We rolled back two versions above, and the --hard parameter can change the content in our workspace. We found that we made a mistake, and we don't want to go back, what should we do if we want to restore the original version?

We can also use the git reset --hard command to restore the previous version rollback (it can be said that the version is advancing, anyway, it is to move the HEAD pointer).

If you use the HEAD parameter , you can only implement the version backward operation, so we can only use the index value to achieve the version forward.

git reset --hard [HEAD]   # 只能实现版本的后退操作
git reset --hard [索引值]  # 使用索引值既可以实现版本后退,也可以实现版本前进

1. First use git log --onelinethe command to view the index value submitted each time.

Of course, if the version has been rolled back, the git log command will continue to list the current commit. So before we roll back the version, we can save the previous git log information, and then roll back the version.

Or you can use the git reflog command to get the commit index value, which can view all changes in the current warehouse, as follows:

2. Use the f226d87 index value of the above picture to advance the version to the version before the rollback.

git reset --hard f226d87

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-bDh0MNSw-1670132301879)(../picture/image-20221204130845003.png)]

2.4 Summary

  • –mixed parameter, move the HEAD pointer, you can modify the content of the temporary storage area, but will not modify the files in the work area. This parameter is the default parameter of the reset command, so it can be omitted when executing the command parameter.
  • –soft parameter, this parameter just moves the pointer of HEAD, and will not affect anything in the temporary storage area and work area.
  • The –hard parameter moves the HEAD pointer, and also changes the contents of the temporary storage area and the work area.

Guess you like

Origin blog.csdn.net/luobeihai/article/details/128171764
Recommended