How to undo operations in Git in Linux

  
                

It can be said that all operating systems have undo operations, and Linux systems are no exception. And in the Linux Git you can undo most of the wrong operations, let's take a look.

When you make a new commit, Git saves a snapshot of your codebase at that particular point in time; after that, you can use Git to return to an earlier version of your project.

In this blog post, I'll explain some of the common scenarios where you need to "undo" changes that have been made, and the best way to do this with Git.

Undo a & ldquo; published & rdquo; changes

scenario: You have performed the git push, send your changes to GitHub, and now you realize which of these commit One is problematic, you need to undo that commit.

Method: git revert SHA

Principle: git revert will generate a new commit, which corresponds to the specified SHA. It is the opposite (or reversed). If the original commit is “substance”, the new commit is “antimatter” — any content deleted from the original commit will be added back in the new commit, any content added in the original commit Will be deleted in the new commit.

This is Git's safest and most basic undo scenario, because it doesn't change history — so you can now git push the new "reverse" commit to offset the commit you submitted incorrectly.

Fix last commit message

Scenario: You have a clerical error in the last commit message, you have executed git commit -m “Fxies bug #42”, but before git push You realize that the message should be "Fixes bug #42″."

Method: git commit --amend or git commit --amend -m “Fixes bug #42”

Principle: git commit --amend will update and replace with a new commit With the recent commit, this new commit combines any changes with the contents of the previous commit. If no changes are currently made, this operation will only rewrite the last commit message.

Undo “Local”Modify

Scenario: A cat walks past the keyboard, inadvertently saves the changes, and then destroys the editor. However, you have not committed these changes. You want to restore everything in the modified file — just like the last commit.

Method: git checkout -- "bad filename"

Principle: git checkout will modify the files in the working directory to a state recorded before Git. You can provide a branch name or a specific SHA that you want to return, or by default, Git will think that you want checkout to be HEAD, the last commit of the current checkout branch.

Remember: any changes you make in this way "undo" will really disappear. Because they have never been submitted, Git can't help us recover them later. You have to make sure you understand what you throw away in this operation! (Maybe you can confirm with git diff first)

Reset “Local”Modify

Scenario: You submitted something locally (not yet pushed), but all of these things It's very bad, you want to undo the previous three commits — just as they have never happened.

Method: git reset "last good SHA" or git reset --hard "last good SHA"

Principle: git reset will return your codebase history to the specified SHA state. It's like these submissions have never happened. By default, git reset preserves the working directory. In this way, the submission is gone, but the modifications are still on the disk. This is a safe choice, but usually we would like to “subtract” & commit and modify the content — this is the function of the --hard option.

Resume after undoing "local modification"

Scenario: You submitted several commits, then git reset --hard to undo these changes (see previous paragraph), then You realize that you want to restore these changes!

Method: git reflog and git reset or git checkout

Principle: git reflog is a great resource for restoring project history. You can recover almost anything — anything you committed with — just by reflog.

You may already be familiar with the git log command, which will display a list of commits. The git reflog is similar, but it shows a list of times when the HEAD has changed. Previous1234Next page Total 4 pages

Copyright © Windows knowledge All Rights Reserved