Summarize how Git rolls back in different situations

Keywords: Programming git

Revoke

All operations before git push are performed in the "local warehouse". Let's call the code restore operation of the "local warehouse" revocation for the time being.

Case 1: The file has been modified, but no git add operation (modification file within working tree) <==> git checkout <filename>/.

git checkout fileName
git checkout .

Scenario 2: Git add operation was performed on several files at the same time, but this time only want to submit a part of the file git add <=> git reset HEAD < filename >

$ git add *
$ git status
# Cancel temporary storage
$ git reset HEAD <filename>

Case 3: The file performs the git add operation, but wants to undo its modification (index rollback) (modify file and add) <==> git reset HEAD < filename > & git checkout < filename >

# Cancel temporary storage
git reset HEAD fileName
# Revocation
git checkout fileName

Situation 4: The modified file has been git commit, but want to modify it again and no longer produce a new Commit git commit <=> git commit -- amend-m'msg'

# Modify last submission 
$ git add sample.txt
$ git commit --amend -m"Explain"

Situation 5: Several git commit operations have been performed locally, and now I want to undo one of them, git multiple commit <=> git git reset [- hard | soft | mixed | merge | keep] [commit | HEAD]

General use--soft

git reset [--hard|soft|mixed|merge|keep] [commit|HEAD]

RollBACK

Scenario 2 above, git push has been carried out, that is, it has been pushed to the "remote warehouse". We call the code restore operation that has been submitted to the "remote warehouse" rollback! Note: It is risky to roll back the remote warehouse. Backup and other team members should be notified in advance.

Scenario 1: Switch to tag or branch

If you tag every time you update the line, congratulations, you can quickly handle the situation in scenario 2 above, git tag <=> git checkout <tag>.

git checkout <tag>

If you go back to the current HEAD <==> git checkout < branch_name >

git checkout <branch_name>

Case 2: Revoke the specified file in history <=> git checkout < commitID > < filename >

# View the historical version of the specified file
git log <filename>
# Roll back to the specified commitID
git checkout <commitID> <filename>

Case 3: Delete the last remote submission of git revert HEAD || git reset --hard HEAD^

Mode 1: There will be new commit records using revert

git revert HEAD
git push origin master

Mode 2: Using reset does not generate new commit records

git reset --hard HEAD^
git push origin master -f

The difference between them is as follows:

  • revert is a change that waives the specified submission, but generates a new submission that needs to fill in the submission annotations, all of which are in the previous history.
  • reset refers to referring to a HEAD pointer to a specified submission, and there will be no abandoned submission record in the history.

Scenario 4: Roll back a commit in history <=> git revert < commitID >

# Find the commitID to roll back
git log
git revert commitID

Similarly, revert will have a new commit submission record, and reset can also be used here.

delete

Delete a commit in history <==> git rebase-i < commitID >

git log --oneline -n5

git rebase -i <commit id>^

Note: Attention should be paid to the last ^ sign, which means the previous commit of commit id

git rebase -i "5b3ba7a"^

Delete commit in the edit box, such as pick 5b3ba7a test2, and save the exit (if you encounter _conflict _need to resolve _conflict)!

git push origin master -f

By doing this, if you want to process more than one commit in history or choose git rebase-i, just delete the corresponding record. Rebase can also edit commit messages and merge multiple commits.

Posted by tlawless on Fri, 01 Mar 2019 16:51:23 -0800