Git management modification, undo modification and deletion of files

1. Management modification

git manages changes, not files. The modification can be to add a new line, delete a line, add some characters, delete some characters, delete a file or create a new file, etc.

  • Now we modify the file sample.txt, and then use the git add command to submit the file to the temporary storage area:

vim sample.txt
cat sample.txt
git add sample.txt
git status
  • You can see that the line "git tracks changes" is added, and then the file sample.txt is modified again, and then submitted using git commit:

vim sample.txt
cat sample.txt
git commit -m "git tracks changes"
git diff
git status
  • The last line of the modified file content adds a word "of". After submission, use git diff to view the difference between the local library and the workspace, and find that the content of the second modification has not been submitted to the local library. Use the git status command to find that the files in the local library have been modified, but they have not been submitted to the temporary storage area. If you want to submit the second modification to the local library, you need to use the git add command to first submit it to the temporary storage area, and then use the git commit command.


2. Undo the modification

2.1 When not added

The above second modification has not been submitted to the temporary storage area using the git add command. At this time, we want to abandon the modification in the work area. How should we do it?

  • In fact, after using git status above, git told me that the git restore command will restore the file in the workspace to the appearance of the file at the time of the last commit.

git restore sample.txt
cat sample.txt
  • It can be seen that the file sample.txt in the workspace has been restored to the appearance of the last commit. That is, the function of the git restore command is to discard all modifications to files in the workspace before git add.

2.2 When added

If the modified file has been submitted to the temporary storage area using the git add command, but I want to abandon the modification of the temporary storage area, what should I do?

  • First, we first modify the file sample.txt, and then use the git add command to submit it to the temporary storage area:

vim sample.txt
cat sample.txt
git add sample.txt
git status
  • After using the git status command, git tells us that the git restore --staged command can discard the contents of the temporary storage area.

  • 可以发现,工作区的内容没有变,只是使用git status命令后发现文件sample.txt的状态变为没有git add之前的状态了。即git restore --staged 命令的作用是将 git add 的文件变成 git add 之前的状态。

  • 如果此时还想放弃工作区的内容,就再使用git restore 命令。

2.3已commit时

已经使用git commit命令提交到本地库的文件

  • 想要恢复到git add时的状态可以使用git reset --soft HEAD

  • 想要恢复到git add之前的状态可以使用git reset --mixed HED 或者git reset HEAD

  • 想要恢复到没有修改之前的状态可以使用git reset --hard HEAD


三、删除文件

删除文件也是修改的一种。

  • 现在我们先创建一个新文件test.txt,然后使用git add和git commit提交到本地库:

touch test.txt
git add test.txt
git commit -m "add test.txt"
  • 这时候,我们将文件test.txt删除掉,再查看本地库的状态:

rm test.txt
git status
  • git告诉我们可以是使用git rm 将文件删除:

git rm test.txt
git stauts
  • 文件被成功删除了。


四、总结

1.只有使用git add命令交到暂存区的文件,才能再使用git commit提交到本地库。

2.git restore 命令的作用是在 git add 之前,放弃工作区中对文件的所有修改。

3.git restore --staged 命令的作用是将 git add 的文件变成 git add 之前的状态。

4.已经使用git commit命令提交到本地库的文件:

  • 想要恢复到git add时的状态可以使用git reset --soft HEAD 。

  • 想要恢复到git add之前的状态可以使用git reset --mixed HED 或者git reset HEAD 。

  • 想要恢复到没有修改之前的状态可以使用git reset --hard HEAD 。

5.想要删除掉已经提交到本地库的文件,可以使用git rm 直接删除。

Guess you like

Origin blog.csdn.net/qyqyqyi/article/details/128693239