Git operation-Modify and delete, git operation Modification
Manage changes
First modification->git add
-> Second modification->git commit
If you modifygit add
, The second modification has not yet come and 'git add', ifgit commit
The second modification is not in the version library, and Git manages the modification. After you use the git add command, The first modification in the workspace is placed in the temporary storage area and ready to be submitted. However, the second modification in the workspace is not in the temporary workspace. Therefore, git commit is only responsible for submitting the modification to the temporary workspace, that is, the first modification is submitted, the second modification will not be submitted.
First modification->git add
-> Second modification->git add
->git commit
In this way, both modifications are submitted to the version library ..
Undo Modification
Scenario 1: If you make some changes in the file, you have not submittedgit add
But cannot be manually revoked.
Method: you cangit checkout -- file
Discard workspace modification:
Another situation is that README.txt has been added to the temporary storage area and modified. Now, undo the modification and return to the state after it is added to the temporary storage area.
In short,git checkout -- file
Is to bring this file back to the status of the last git commit or git add.
Scenario 2: If the file has been modifiedgit add
The temporary storage area has been saved, but it has not been submitted. In this case, you need to cancel the modification of the temporary storage area.
Method: use commands.git reset HEAD file
You can undo the modification of the temporary storage area (unstage) and put it back into the workspace. Note:git reset
The command can be used to roll back the version or to the workspace. When we use HEAD, it indicates the latest version.
In this case, the temporary storage area is clean, the workspace is modified, and then reused.git checkout -- file
Discard workspace changes
Delete an object
Scenario 1: Create a new file test.txt in the work area and submit it to the version library. delete the file.
Method: You can delete test.txt directly in the work area.git rm
And thengit commit
In this way, the file will be deleted from the version library.
Scenario 2: test.txt is deleted by mistake in the work area, but the version Library also has
Method:$ git checkout -- test.txt
You can restore accidentally deleted files to the latest version.
git checkout
In fact, it is to replace the workspace version with the version in the version library. Whether the workspace is modified or deleted, you can "Restore it with one click ".