git rollback/undo operations

Original link: https://blog.kelu.org/tech/2023/05/05/git-rollback-operation.html

git has three rollback scenarios:

  • Workspace rollback (without git add )
  • Temporary storage area rollback ( git add but not git commit )
  • Repository rollback ( git commit )

Make a record here.

1. Workspace rollback

The workspace is the content that has not been git add , the currently visible directories and files. For example:

image-20230506 PM 05904150

Rolling back is easy:

 git checkout -- [文件名] //撤销指定文件的修改git checkout . //撤销所有文件改动

2. Staging area rollback

The temporary storage area is the content that has been added git add but not yet git commit . For example:

image-20230506 PM 10608132

 git rm --cache [文件名] //撤销暂存区指定文件回到工作区git reset -- [文件名] //撤销暂存区指定文件回到工作区git reset [ HEAD] // 撤销暂存区所有文件回到工作区,HEAD可以省略

3. Repository rollback

Version library : It is the version warehouse saved in .git, that is, those records that have been committed.

 git log -- pretty = oneline // 查看最近几次commit的commit_id

Can be operated with git revert or git reset:

 git revert HEAD // 撤销最近的一个提交git revert <commit_id> // 撤销指定的版本,该操作将自动保存为一次commit

or:

 git reset HEAD benchmarks.rb git reset --soft xxx git reset --hard xxx

The main difference between the two is:

  • Git revert is to roll back the previous commit with a new commit
  • git reset is to directly delete the specified commit
  • git reset is to move HEAD back a bit, and git revert is to move HEAD forward, but the content of the new commit is just the opposite of the content to be reverted, which can offset the content to be reverted

References

This article is transferred from: https://blog.kelu.org/tech/2023/05/05/git-rollback-operation.html
This site is only for collection, and the copyright belongs to the original author.