A: “A small white at the front end, she is not familiar with git, and the code she has worked so hard for a week is gone.”
B: “oh? Why not? “
A: “enter git log at the terminal and list all commit information, as shown in the following figure:”
A: “The information of commit is very simple, that is, it has done 6 functions, and each function corresponds to the submission of A:“commit, namely, feature-1 to feature-6.”
B: “Okay, then what?”
A: “then the front-end white pit dad, implemented a forced rollback, as follows:”
git reset --hard 2216d4e
A: “Xiao Bai rolled back to feature-1, and -hard was added when rolling back, causing all the codes from feature-2 to feature-6 to be lost. now git log is shown as follows:”
A: “now the code for feature-2 to feature-6 is gone.”
A: “Xiao Bai has added a new commit submission based on this, and the information is called feature-7, as shown in the following figure:”
A: “now there is no feature-2 to feature-6, and one more feature-7.”
A: “Then, how can classmate B recover all the missing codes feature-2 to feature-6 and keep the code of feature-7?”
A: “classmate b, start your performance.”
B: “ah, ha, ha, ha! This topic I will “
This problem is a very classic and classic git problem, which can be solved by git reflog and gitchery-pick in fact.
Basically mastered git reflog and gitchery-pick, your git command line operation is even a successful entry.
Next, explain how to operate one by one.
You only need to enter in the terminal:
Then you will show all your previous git operations. All your previous operations have been recorded by git, as shown in the following figure:
At this time, remember two values: 4c97ff3 and cd52afc, which are the hash codes of feature-7 and feature-6 respectively. Then perform a rollback to return to feature-6:
git reset --hard cd52afc
Now we return to feature-6, as shown in the following figure:
Ok, we went back to feature-6, but feature-7 is gone. how can we add it? At this time, git cherry-pick was used. Just now we know that the hash code of feature-7 is 4c97ff3. The operation is as follows:
git cherry-pick 4c97ff3
Once you have typed it, your feature-7 code will come back. There may be some conflicts during this period, just follow the prompts to resolve them. The final result is as follows:
Whether it is very simple, the codes of feature-1 to feature-7 are merged together, and the previous codes are all returned.
The article is integrated into an interesting article seen by WeChat Open Community.https://developers.weixin.qq ….