Basic operations of Git: version rollback

4 version rollback

1 git workflow

Submit workflow

  • Workspace: that is, the code modified by your current branch, before git add xx! Excludes git add xx and git commit xxx.
  • Staging area: git add xxx has been added, but git commit xxx has not been done.
  • Local branch: git commit -m xxx has been submitted to the local branch.

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

Rollback workflow

When uploading code to the remote warehouse, problems will inevitably occur, and the code may need to be rolled back during any process:

The function of git reset is to modify the location of HEAD, that is, to change the location pointed by HEAD to a previously existing version.

git reset --hard HEAD^

That is, roll back one version. After the rollback is completed, the workspace will be the code of the previous version and it will be clean.

git reset --soft HEAD^

Roll back one version and put the incorrectly submitted code changes in the staging area.

git reset --mixed HEAD^(和不带参数是一样的)

Roll back one version and put the incorrectly submitted code changes in the workspace.

2 Git undo & rollback operations (git reset and get revert)

code in workspace

git checkout -- a.txt   # 丢弃某个文件,或者
git checkout -- . # 丢弃全部
  • Note: git checkout - . discards everything, including: newly added files will be deleted, deleted files will be restored, and modified files will be restored. These premises all talk about returning to the way it was before the staging area. There will be no impact on the code previously saved in the staging area. It has no effect on the code submitted to the local branch. Of course, if you haven't staged or committed anything before, it will be back to the way it was last time you pulled it.

The code is git added to the cache area and is not submitted.

git reset HEAD .  或者
git reset HEAD a.txt
  • This command only changes the staging area and does not change the workspace. This means that without any other operations, the actual files in the workspace will remain unchanged from before the command was run.

git commit to local branch, but no git push to remote

git log # 得到你需要回退一次提交的commit id
git reset --hard   # 回到其中你想要的某个版

or

git reset --hard HEAD^  # 回到最新的一次提交

or

git reset HEAD^  # 此时代码保留,回到 git add 之前

git push commits changes to the remote repository

  1. Using git reset directly deletes the specified commit.
git log # 得到你需要回退一次提交的commit id
git reset --hard
git push origin HEAD --force # 强制提交一次,之前错误的提交就从远程仓库删除
  1. Using git revert is to use a new commit to roll back the previous commit.
git log # 得到你需要回退一次提交的commit id
git revert   # 撤销指定的版本,撤销也会作为一次提交进行保存
  1. The difference between git revert and git reset

    • Git revert uses a new commit to roll back the previous commit. The commits before this commit will be retained;
    • git reset is to go back to a certain commit. The commit and previous commits will be retained, but the modifications after this commit ID will be deleted.

3 scenes

scene one:

Oops, I just committed the unwanted code to the local warehouse, but I haven't done the push operation yet!

Scene two:

It’s completely finished. There is a problem with the code that was just updated online. You need to restore the code submitted this time!

Scene three:

I just realized that a previous commit was stupid and now I want to kill it!

4 operations

For scenario one

All operations before git push are performed in the "local warehouse". For the time being, we call the code restoration operation of "local warehouse" "undo"!

  • Situation 1: The file was modified, but the git add operation was not performed (undone in the working tree)
git checkout fileName

git checkout .
  • Situation 2: The git add operation is performed on multiple files at the same time, but this time only a part of the files are submitted.
$ git add *

$ git status

# 取消暂存

$ git reset HEAD
  • Scenario 3: The file has performed a git add operation, but you want to undo the modifications to it (rollback within the index)
# 取消暂存

git reset HEAD fileName

# 撤销修改

git checkout fileName
  • Situation 4: The modified file has been git committed, but you want to modify it again without generating a new Commit.
# 修改最后一次提交

$ git add sample.txt

$ git commit --amend -m"说明"
  • Situation 5: Multiple git commit operations have been performed locally, and now I want to undo one of the commit operations.
git reset [--hard|soft|mixed|merge|keep] [commit|HEAD]

For scenario two

A git push has been performed, that is, it has been pushed to the "remote warehouse". We call the code restoration operation that has been submitted to the "remote warehouse" "rollback"! Note: Rolling back the remote warehouse is risky, so you need to make a backup in advance and notify other team members!

If you are tagged every time you update online, congratulations, you can quickly handle the above scenario 2.

git checkout 

If you go back to what the current HEAD points to

git checkout 
  • Situation 1: Withdraw the specified file to the specified version
# 查看指定文件的历史版本

git log

# 回滚到指定commitID

git checkout
  • Scenario 2: Delete the last remote commit

    • Method 1: Use revert
    • Method 2: Use reset
git revert HEAD

git push origin master
git reset --hard HEAD^

git push origin master -f

The difference between the two:

Revert means to abandon the modifications of the specified commit, but a new commit will be generated, and the commit comments need to be filled in. The previous history records are all there; reset means to
point the HEAD pointer to the specified commit, and the abandoned commit record will not appear in the history record.

  • Scenario 3: Rollback a commit
# 找到要回滚的commitID

git log

git revert commitID

Delete a commit

git log --oneline -n5

Git undo & rollback operations (git reset and get revert) 2

git rebase -i "commit id"^

Note: You need to pay attention to the last ^ sign, which means the previous submission of the commit id

git rebase -i "5b3ba7a"^

Git undo & rollback operations (git reset and get revert) 3

Delete the relevant commit in the edit box, such as pick 5b3ba7a test2, then save and exit (if you encounter a conflict, you need to resolve the conflict first)!

git push origin master -f

Through the above operations, if you want to process multiple historical commits, you can choose git rebase -i and just delete the corresponding records. Rebase can also edit commit messages and merge multiple commits

Guess you like

Origin blog.csdn.net/DeepLearning_/article/details/132815098