In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the methods of the previous version of Git recovery". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the methods of the previous version of Git recovery?"
I. description of the problem
In the process of using github to realize the development of multi-person cooperative programs, we sometimes have the situation of error committing. At this time, we hope to undo the commit operation and make the program go back to the way it was before submission. This paper summarizes two solutions: reset and revert.
Second, background knowledge
Version management of git and understanding of HEAD
Every time you use git to commit, Git automatically strands them into a timeline, which is a branch. If there is no new branch, then there is only one timeline, that is, there is only one branch, which in Git is called the main branch, or master branch. There is a HEAD pointer to the current branch (if there is only one branch, it points to master, while master points to the latest commit). Each version has its own version information, such as a unique version number, version name, and so on. As shown in the following figure, suppose there is only one branch:
III. Solutions
Method 1: git reset
Principle: the function of git reset is to modify the location of HEAD, that is, to change the location pointed to by HEAD to a pre-existing version, as shown in the following figure, suppose we want to fall back to version 1:
Applicable scenario: you can use this method if you want to revert to a previously submitted version, and we don't want any versions submitted after that version.
Specific operations:
1. View the version number:
You can use the command "git log" to view:
You can also see it on the github website:
two。 Use the "git reset-- hard target version number" command to roll back the version:
Then use "git log" to view the version information, and the local HEAD has already pointed to the previous version:
3. Use "git push-f" to commit changes:
You will get an error if you use "git push" at this time, because the version that our local library HEAD points to is older than that of the remote library:
So we have to use "git push-f" to force it up, and that's fine:
From the github graphical interface, the HEAD of the remote library has also been pointed to the target version:
The fallback was successful!
Method 2: git revert
Principle: git revert is used to "reverse" a version in order to undo the changes to that version. For example, we commit three versions (version 1, version 2, and version 3) and suddenly find that version 2 is not good (for example: there is bug), and we want to undo version 2, but do not want to affect the submission of version 3, we can use the git revert command to reverse version 2 and generate a new version 4, which will retain the things of version 3, but cancel the things of version 2. As shown in the following figure:
Applicable scenario: this method can be used if we want to undo a previous version, but want to keep the later version of the target version and record the entire version change process.
Specific operations:
For example, there are now three files in the library: READ.md, text.txt, and text2.txt.
1. View the version number:
You can view it from the command line (enter git log):
As shown in the figure, the most recent two versions are called "add text.txt" (that is, a new file text.txt) and "add text2.txt" (a new file text2.txt). At this time, we do not need the text.txt file, that is to say, we do not want the operation of the "add text.txt" version, which can be achieved by reversing the "add text.txt" version.
You can also view the version number through the graphical interface of the github website:
two。 Reverse with "git revert-n version number" and submit with "git commit-m version name":
(1) reverse and use the "git revert-n version number" command. With the following command, we reverse the version with the version number 8b89621:
Git revert-n 8b89621019c9adc6fc4d242cd41daeb13aeb9861
one
Note: conflicts may occur here, so you need to manually modify the conflicting files. And ask for the git add file name.
(2) submit, using "git commit-m version name", such as:
Git commit-m "revert add text.txt"
one
At this point, you can use "git log" to view the local version information, and you can see that a new version has been generated, which turns out to be the "add text.txt" version, but retains the "add text2.txt" version:
3. Use "git push" to push to the remote library:
Git push
one
View the remote library version information displayed on github:
At this point, check the files in the warehouse, and there are two remaining ones: READ.md and text2.txt.
Succeed in doing it!
Thank you for your reading, the above is the content of "what are the methods for restoring the previous version of Git?" after the study of this article, I believe you have a deeper understanding of what the method of restoring the previous version of Git has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.