git undo operation all

Concept

  1. Workspace: that is, the code modified by your current branch, before git add xx! Excludes git add xx and git commit xxx.

  2. Staging area: git add xxx has been added, but git commit xxx has not been done.

  3. Local branch: git commit -m xxx has been submitted to the local branch.

    image-20201031170258434

Chapter 1: Description

1. Code in the workspace

git checkout -- a.txt # Discard a file, or
git checkout -- . # Discard all

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.

2. The code git-added to the cache area but was not committed

git reset HEAD . or
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.

3. git commit to the local branch but no git push to the remote

git log # Get the commit id you need to roll back a commit
git reset --hard <commit_id> # Return to the version you want
or
git reset --hard HEAD^ # Return to the latest commit
or
git reset HEAD^ # The code is retained at this time and goes back to before git add

4. git push submits changes to the remote warehouse

1) The specified commit is directly deleted through git reset
git log # Get the commit id you need to roll back a commit
git reset --hard <commit_id>
git push origin HEAD --force # Force a commit once, and the previous incorrect commit will be deleted from the remote repository
2) Using git revert is to use a new commit to roll back the previous commit
git log # Get the commit id you need to roll back a commit
git revert <commit_id> # Undo the specified version. The undo will also be saved as a commit
3) 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 submission. The submission and previous commits will be retained, but the modifications after this commit ID will be deleted.

Chapter 2: Actual Combat

1. Cancellation

During the development process, you will definitely encounter such a scenario:

Scene 1:

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

Scenario 1, all operations before git push are performed in the “local warehouse”.

Wefor now call the code restoration operation of “local warehouse” “undo”!

Case 1: The file has been modified but the git add operation has not been performed (working Undo within tree)

git checkout fileName
git checkout .

Case 2: git add was performed on multiple files at the same time Operation, but this time I only want to submit some of the files

$ git add *
$ git status

# Cancel temporary storage
$ git reset HEAD <filename>

Case 3: The file has performed the git add operation, but wants to undo the changes to it Its modification (rollback within index)

# Cancel temporary storage
git reset HEAD fileName

# Undo changes
git checkout fileName

Case 4: The modified file has been git commit, but I want to modify it again Modifications no longer generate new Commit

# Modify the last commit
$ git add sample.txt
$ git commit --amend -m"Description"

Case 5: Multiple git commit operations have been performed locally , now I want to undo one of the Commits

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

For specific parameters and usage instructions, please check: Reset Secrets in Git Pro In-depth Guide (2)

Second, Rollback

Scenario 2:

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

Scenario 2, git push has been performed, that is, it has been pushed to the “remote warehouse”.

Wecall 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 situation

git checkout <tag>

If you return to the current HEAD pointer

git checkout <branch_name>

Case 1: Revoke the specified file to the specified version

# View the historical version of the specified file
git log <filename>

# Roll back to the specified commitID
git checkout <commitID> <filename>

Case 2: Delete the last remote commit

Method 1: Use revert

git revert HEAD
git push origin master

Method 2: Use reset

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

The difference between the two:

  • revert abandons the modifications of the specified submission, but will generate a new submission. You need to fill in the submission comments, and the previous history records are all there;
  • reset refers to pointing the HEAD pointer to the specified commit, and no abandoned commit records will appear in the history.

Case three: roll back a certain submission

# Find the commitID to be rolled back
git log
git revert commitID

3. Delete a certain submission

Scene three:

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

git log --oneline -n5

Gitundo&rollbackoperation-log

git rebase -i "commit id"^

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

git rebase -i "5b3ba7a"^

Gitundo&rollbackoperation-rebase

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.

The knowledge points of the article match the official knowledge archives, and you can further learn related knowledge. Git skill tree Home page Overview 6802 people are learning the system