[Git] Modify file & version rollback & undo modification & delete file

modify file

Git is better designed than other version control systems. Git tracks and manages changes, not files.

Why amend?

Adding a new line/deleting a line/changing some characters/deleting some and adding some/creating a new file is also considered a modification


For example: Modify the ReadMe file once

image-20230614154405022

At this time, the ReadMe in the warehouse is different from the ReadMe in our workspace. You can check git statusthe status of the warehouse by using

image-20230614154458524

The above results tell us that the ReadMe has been modified, but the addition and submission have not been completed. At present, we only know that the file has been modified. It would be better if we can know exactly which places have been modified.

  • You can use git diff 文件名the command to display the difference between the temporary storage area and the work area file , and the displayed format is the common diff format of Unix
  • Also available git diff HEAD -- 文件名: See the difference between repository and workspace files

image-20230614154657074


At this time, after git add, then use git commit

image-20230614154856026


version rollback

Git can manage the historical version of the file, which is also an important capability of the version controller. If one day you find that there is a big problem with the previous work and you need to start over in a specific historical version, at this time, you need the function of version rollback. You can use git resetthe command to roll back the version, and you can specify to roll back a submitted version

Note: The essence of "rollback" is to roll back the content in the version library. Whether the work area or the temporary storage area is rolled back is determined by the command parameters, that is to say, no matter what parameters are used, the content of the version library will definitely be rolled back

git reset syntax rules

 git reset [--soft 或者 --mixed 或者 --hard] [HEAD]

--mixed: It is the default option , you can use it without this parameter. This parameter returns the content of the temporary storage area to the content of the specified submission version , and the workspace file remains unchanged

--soft: This option does not change the content of the workspace and the temporary storage area , but rolls back the version library to a specified version

--hard: This option returns both the staging area and the working area to the specified version

  • Notice! ! ! ! ! : Do not use this option if there is uncommitted code in the workspace --hard, because the workspace will be rolled back at this time, and the uncommitted code will never be retrieved , so be careful before using this parameter

HEAD Description:

  • It can be directly written as commit id (version number), indicating the version to be returned
  • HEAD indicates the latest version of the current master

Method 1:

  • HEAD^ indicates the previous version

  • HEAD^^ Previous version

  • And so on...

Method 2:

  • HEAD~0Indicates the current version
  • HEAD~1Indicates the previous version
  • And so on...

Example: Update 3 versions of the test file and make 3 submissions respectively

image-20230614162528700

View commit history

image-20230614162332755

Assuming that after submitting version3, it is found that version3 is written incorrectly, and you want to roll back to version2, the method at this time is:

  • What I hope here is to roll back the content of the workspace to version2 , so you need to use --hardthe option,

image-20230614163658800

At this point, the content of the test file has rolled back to version2. Check git logthe commit log again and find that HEAD points to version2

image-20230614163803409

But now what if I regret it and want to go back to version3? At this time, you can continue to use git resetthe command to roll back to version3the version, but you must get the version number (commit id) of version3 to specify the rollback version, but at this time, git logthe version number of version3 cannot be printed out. If you are lucky, we will You can find the previous records from the terminal. If you are unlucky, the version number of version3 has been lost

Git provides a git reflogcommand for users to record every local command , so that all operation records can be easily found

image-20230614164230085

At this time, this 4927c33is part of the version number of version3, and part of it can also be used to represent the target version when git rolls back

image-20230614164430423


Notice

The version rollback speed of Git is very fast, because Git has a HEAD pointer pointing to the current branch master internally, and saves the latest version number (commit id) of the current master branch in the file .gitunder the directory refs/heads/master. When we roll back the version At the time, Git only stores refs/heads/mastera specific version number, which can be simply understood as the following diagram:

image-20230614171335147


Undo changes

For example: If we have written code in our workspace for a long time , the more we write, the more we can’t continue writing, and we feel that what we wrote is really rubbish code, and we want to revert to the previous version

Case 1: The code in the workspace has not been added

At this point, you can directly delete the code you have added in the workspace. But it may be written for 3 days without submitting the code. At this time, it is difficult to delete the newly added code. Of course, you can also use it to see the git diff filenamedifference before deleting, but it will take 3 days to delete the code, and it will be very expensive. The probability will also fix the bug

Solution: you can usegit checkout -- 文件名The command returns the corresponding files in the workspace toThe latest add or committime status . Remember: at this time --can not be discarded, otherwise the command has other meanings

image-20230614202616044

Case 2: The code in the workspace has been added but not committed

The code in the workspace has been added to the temporary storage area ==> At this time, you can use <font color='blue'>git resetthe command to roll back the version,

  • Step 1: Return the content of the temporary storage area to the specified version content, but the workspace file remains unchanged ==> use the --mixedoption, but --mixedthe default parameter, so it can be omitted
  • Step 2: Discard the code in the current workspace and return to the state of the latest add or commit

Step 1: Use git reset --mixed filethe version content of the rollback staging area

image-20230614203112093

Step 2: Use git check -- 文件名: to discard the modification of the file in the workspace

image-20230614203227980

Case 3: The code in the workspace has been added and committed

At this point: You can use to git reset --hard HEAD^roll back both the staging area and the work area to the previous version , and the version library will definitely roll back. At this time HEAD^: Indicates the previous version . Of course, this is premised: the local version library has not been pushed to the remote to roll back to the previous version. And when using git reset, the version library will definitely roll back

image-20230614203606157


image-20230614203922772

Delete Files

Modifications to the workspace include: adding, deleting, and modifying files in the workspace

The premise of the following example is: the file file has been added and committed . If a file is deleted without adding and committing, it will be impossible to recover!

Example: delete file file

Directly using rm to delete is useless, git status will immediately tell you which files have been deleted

image-20230614204054809

At this time, the content of the workspace and the version library are inconsistent. To delete files, in addition to deleting the files in the workspace, you must also clear the files in the version library . Of course, there are two cases:

  • case1: Accidentally deleted

At this point git checkout --文件名, you can restore using

image-20230614204236148

  • case2: Do you want to delete the file from the repository

Use rm to delete only the files in the workspace. At this time, you can use the command: git rm file name: delete the file from the temporary storage area and the workspace, and you need to run it again after executing the commandcommit

image-20230614204551261

The file is now removed from the repository and staging area

Guess you like

Origin blog.csdn.net/chuxinchangcun/article/details/131985527