Writing in the front
In Git, you can use reset, checkout, and Revert to undo bad decisions you made in the past.
Read with the concept of Git’s three regions in mind
Git: Stage, Stage, Stage, Stage, Stage, Stage, Stage
Eat the first chestnut and understand what rollback is
There is an Android development engineer named Xiao Ming, who is developing on the feature branch. The history of his feature branch is shown in the figure below. Everything is normal and according to the plan, when he receives a requirement change notice saying “user integral module, this version will not be used”. Xiao Ming needs to change it back quickly.
If Xiao Ming can not roll back the history, need human operation to find the code about “user integration module”, and then note or delete the release one by one, this is more sad, I guess Xiao Ming may cry.
If Ming uses Git and knows how to roll back, all he has to do is find the commit at the end of the rollback and start the rollback with a single command.
If Ming had been an old hand, he would have created a new branch for backup and then rolled back, which would have only required two commands:
The above perfectly solved the change of demand at this time. Xiao Ming was very happy.
Pick up your chestnuts and recognize the three patterns of reset
Git reset –hard
- –hard
- — Mixed (default mode)
- –soft
The three parameters correspond to different scopes, which are closely related to the three Git regions mentioned in the beginning, because the three modes exist because there are three partitions.
In practice, you may encounter a variety of weird requirements for undo operations, understand the difference between the three modes, it is fun to use. Here’s a list of two or three scenarios you’re sure to encounter and use the reset mode. Can easily solve the problem.
Xiao Ming writes code ing; The colleague said, “Xiao Ming, please pull the latest code of the warehouse, Blabla…” ; Xiao Ming started “Git pull”; Git pull failed because there are uncommitted changes in the staging area that may be overwritten. Xiaoming is sure that it will not be overwritten, and does not want to create a new commit, but also wants to save the changes; Excuse me: what should I do?
Git reset // Then pull git pull XXXCopy the code
Xiaoming just created a commit; Then I found a small error in the commit; But in order to maintain good branch history, I don’t want to create another commit because of this small change; Excuse me: what should I do?
Git add. Git commit -m'xxx'
Copy the code
Git commit –amend this is the –soft solution, but there is a more convenient way to do it without undoing it.
// Resubmit git add. Git commit --amendCopy the code
Be careful when using reset-hard because it will completely reset your workspace and discard changes. Be sure to discard any unsaved changes before using it, or you may not be able to retrieve them after resetting.
Another way to undo things is to revert, which is a magical command.
Revert is an offset undo
Revert, a difficult word to understand, loosen the seal of extraordinary imagination.
Imagine that adding a.tuck to a commit, and revert creates a new commit that removes A.tuck. Both submissions exist at the same time, so A.tuck never existed and the original commit is revoked.
It’s like a positive and negative relationship 1 plus minus 1 is equal to 0.
For the first example, Ming would do this if he used revert “Undo integral module”
Git revert HEAD ^^^... headCopy the code
Git log to see the branch history after the operation, not less, but more, add a few inversion commit.
Here’s what you might be wondering about the use of the scenario in the first revert example:
Why do you have reset?
This is because, in the example below, there are still submissions to save at the top of the branch. Reset does not work. You can only reverse the middle submissions with revert.
If you don’t want the new reverse commit contaminating branch history, you can also launch your dark magic rebase-i and drop the intermediate commit. Git really does anything, as long as you know how to do it.
reset VS revert ,Ready? GO!
Since reset and Revert both undo a submission, many people naturally compare them. I was one of those people, and I got bored, and I drew a comparison, like this:
It can be clearly seen that the two have different changes in branch history:
reset
Back to historyrevert
Add history by creating reverse commit
From a data security perspective, Revert is safer than reset because the operation can be reversed and reversed. Reset is a complete discard, but consider creating a backup branch as in the first example to ensure security.
From a long-term maintenance perspective, Reset has a relatively clean history, and reverse revert doesn’t make much sense because there are very few requirements that you can roll around with.
Reset does not work in scenarios where a commit is revoked and no longer branches at the top, which revert can do.
The above differences between the two end of the discussion, no summary, depending on your personal preferences and needs for scenarios to use.
You don’t undo the submission, you undo the file
For example, I often write a lot of code and realize that an idea doesn’t work. At that point, I’ve already written so much code that I can’t even run out of Command+Z. Manual change is too troublesome and prone to error, how to do?
At this time you need to undo the modification of the file, the following is to introduce to you, how to do file undo.
The second command of checkout
The checkout command is familiar with all the fancy operations performed on the HEAD pointer, such as switching branches, checking out commits, checking out tags, etc.
Surprisingly, it can also be used to restore files in the workspace!
Git add
to add the changes to the staging area for the next commit. Git checkout —
Git Checkout —
The new operation of reset
I want to commit the workspace 1.txt next time, so I execute git add. Git reset HEAD
git reset HEAD
Look at the comments. This action can remove changes to a file from the staging area. This command is useful when the error add does not want to commit a file.
Write in the back
Well, the above is all the content of this blog, I hope to help you understand and learn to submit the history of undo and file modification of undo.
The next blog will cover a special healing command called reflog, which can be used in conjunction with reset to recover data, including those that have been undone. Stay tuned!
See you Next Blog!