Git -- undo operations

undo operation

  • At any stage, you may want to undo some operations

  • Some undo operations are irreversible

--amend patch commit

Sometimes we only find out that a few files were missed and not added after the submission, or the submission information was wrongly written. At this point, you can run the commit command with the --amend option to resubmit

git commit --amend

The content of the temporary storage area will be submitted again, but the information submitted last time will be overwritten

git commit -m 'initial commit'
git add forgotten_file
git commit --amend -m "second commit" 
  • In the end there will only be one commit history, and the second commit will replace the result of the first commit

  • If you check the commit history git log, you will only find the second commit submitted for the second time, but not the initial commit submitted for the first time

  • Advantages: The latest submission information can be slightly improved without disrupting the submission history of the code warehouse. Submit once every small piece of modification, and the submission history will be particularly bloated

unstaged file

At present, I use the latest version of git (2.31.1), it will prompt to use git restore to cancel the temporary storage, but the official document will use git reset

  • git restore

  • git reset

restore

polo@B-J5D1MD6R-2312 watermarker % git add .
polo@B-J5D1MD6R-2312 watermarker % git status
位于分支 master
您的分支领先 'origin/master' 共 5 个提交。
  (使用 "git push" 来发布您的本地提交)

要提交的变更:
  (使用 "git restore --staged <文件>..." 以取消暂存)
    删除:     test1.txt
    删除:     ttrtt.txt

Execute git restore

git restore --staged 文件名
polo@B-J5D1MD6R-2312 watermarker % git restore --staged test1.txt
polo@B-J5D1MD6R-2312 watermarker % ls
README.md    font        markers.py    test.py        venv
polo@B-J5D1MD6R-2312 watermarker % git status
位于分支 master
您的分支领先 'origin/master' 共 5 个提交。
  (使用 "git push" 来发布您的本地提交)

要提交的变更:
  (使用 "git restore --staged <文件>..." 以取消暂存)
    删除:     ttrtt.txt

尚未暂存以备提交的变更:
  (使用 "git add/rm <文件>..." 更新要提交的内容)
  (使用 "git restore <文件>..." 丢弃工作区的改动)
    删除:     test1.txt

Untracked files here can continue to use git restore, and the previous changes will be discarded directly. For example, here is a deleted file, and it will restore the file after execution

polo@B-J5D1MD6R-2312 watermarker % git restore test1.txt
polo@B-J5D1MD6R-2312 watermarker % git status
位于分支 master
您的分支领先 'origin/master' 共 5 个提交。
  (使用 "git push" 来发布您的本地提交)

要提交的变更:
  (使用 "git restore --staged <文件>..." 以取消暂存)
    删除:     ttrtt.txt

polo@B-J5D1MD6R-2312 watermarker % ls
README.md    font        markers.py    test.py        test1.txt    venv

reset

git reset HEAD <文件名>

specific chestnuts

olo@B-J5D1MD6R-2312 watermarker % git add .
polo@B-J5D1MD6R-2312 watermarker % git status
位于分支 master您的分支领先 'origin/master' 共 5 个提交。
  (使用 "git push" 来发布您的本地提交)
要提交的变更:
  (使用 "git restore --staged <文件>..." 以取消暂存)
    删除:     test1.txt
    删除:     ttrtt.txt
polo@B-J5D1MD6R-2312 watermarker % git reset HEAD test1.txt
重置后取消暂存的变更:
D    test1.txt
polo@B-J5D1MD6R-2312 watermarker % git status
位于分支 master您的分支领先 'origin/master' 共 5 个提交。
  (使用 "git push" 来发布您的本地提交)
要提交的变更:
  (使用 "git restore --staged <文件>..." 以取消暂存)
    删除:     ttrtt.txt
尚未暂存以备提交的变更:
  (使用 "git add/rm <文件>..." 更新要提交的内容)
  (使用 "git restore <文件>..." 丢弃工作区的改动)
    删除:     test1.txt
能看到 test1.txt 已经撤销暂存区了

reset undoes all submissions in the staging area at the same time

git reset 

High-risk operation, without any parameters, directly revoke all submissions

specific chestnuts

polo@B-J5D1MD6R-2312 watermarker % git add .
polo@B-J5D1MD6R-2312 watermarker % git status
位于分支 master
您的分支领先 'origin/master' 共 5 个提交。
  (使用 "git push" 来发布您的本地提交)
要提交的变更:
  (使用 "git restore --staged <文件>..." 以取消暂存)
    重命名:   test1.txt -> test22.txt
    重命名:   ttrtt.txt -> test3.txt
    新文件:   testt.txt
polo@B-J5D1MD6R-2312 watermarker % git reset
重置后取消暂存的变更:
D    test1.txt
D    ttrtt.txt
polo@B-J5D1MD6R-2312 watermarker % git status
位于分支 master
您的分支领先 'origin/master' 共 5 个提交。
  (使用 "git push" 来发布您的本地提交)
尚未暂存以备提交的变更:
  (使用 "git add/rm <文件>..." 更新要提交的内容)
  (使用 "git restore <文件>..." 丢弃工作区的改动)
    删除:     test1.txt
    删除:     ttrtt.txt
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)
    test22.txt
    test3.txt
    testt.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
polo@B-J5D1MD6R-2312 watermarker % ls
README.md    markers.py    test22.txt    testt.txt
font        test.py        test3.txt    venv
polo@B-J5D1MD6R-2312 watermarker %
本来有三个提交,执行完 git reset 直接都撤销出暂存区,需要重新提交了

Undo changes to a file

How to operate if you don't want to keep the modification of a certain file? It is to restore it to the way it was last submitted (or just cloned)

git also has a corresponding prompt (the new version has not found this prompt yet)


Changes not staged for commit:  (use "git add <file>..."toupdate what will be committed)
  (use "git checkout -- <file>..."to discard changes in working directory)
    modified:   CONTRIBUTING.md

Focus on remembering this checkout command

git checkout -- <file>...

specific chestnuts

polo@B-J5D1MD6R-2312 watermarker % git add .
polo@B-J5D1MD6R-2312 watermarker % echo 12344 >> test3.txt
polo@B-J5D1MD6R-2312 watermarker % git status
位于分支 master
您的分支领先 'origin/master' 共 5 个提交。
  (使用 "git push" 来发布您的本地提交)

要提交的变更:
  (使用 "git restore --staged <文件>..." 以取消暂存)
    重命名:   test1.txt -> test22.txt
    新文件:   test3.txt
    重命名:   ttrtt.txt -> testt.txt

尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
  (使用 "git restore <文件>..." 丢弃工作区的改动)
    修改:     test3.txt

polo@B-J5D1MD6R-2312 watermarker % git checkout -- test3.txt
polo@B-J5D1MD6R-2312 watermarker % git status
位于分支 master
您的分支领先 'origin/master' 共 5 个提交。
  (使用 "git push" 来发布您的本地提交)

要提交的变更:
  (使用 "git restore --staged <文件>..." 以取消暂存)
    重命名:   test1.txt -> test22.txt
    新文件:   test3.txt
    重命名:   ttrtt.txt -> testt.txt

You can see that the modification of test3.txt has been revoked

focus

  • It is also a high-risk command

  • After executing the git checkout command, any local modifications to that file will disappear, and Git will overwrite it with the most recently committed version

Guess you like

Origin blog.csdn.net/qq_41663420/article/details/129639029