As a version control tool, the most basic function of Git is to record the historical modification information of the project so that developers can understand the evolution of the project, and it also provides some convenient operation tools, such as rollback operations, to facilitate developers Easily manage code changes during project development.
Git can roll back versions, which is very useful for developers because they can go back to previous versions, view the code and make modifications. This article explains how to use Git to roll back a version.
1. Check the current version information in Git
Before rolling back the version, we need to find out the current version first. Using the git log command provided by Git, you can view all the submission history of the current branch, as well as detailed information at the time of submission.
Execute the following command:
git log
This command will display the SHA-1 checksum, author name, and email address of all commits , submission date and submission instructions and other details.
If there is too much information on the screen to read when all history records are displayed, you can press the q key to exit.
2. Rolling back to a specific version
It is very easy to roll back to a previous version in Git. You only need to use the reset command provided by Git.
If you want to roll back the version to a specific commit point, you can use the following command:
git reset <commit>
Where
HEAD can also be used instead of
3. Roll back to the previous version
If you want to roll back to the previous version, you can use the following Git command:
git reset HEAD^
This command will roll back the current version to the previous version. HEAD^ means the previous version, and HEAD^^ means the previous version.
If you want to roll back more versions, just use HEAD~n, for example, roll back to the first 5 versions:
git reset HEAD~5
4. Rollback version and retain changes
If you need to roll back to the previous version and retain the changes in the current working directory, you can use the stash command and Git reset command provided by Git to achieve this.
git stash
This command will put all uncommitted changes into Git stash.
Then, execute:
git reset <commit>
This command will roll back to the version you want.
Now execute the stash command again:
git stash
It will retrieve the modifications previously posted to Git stash.
5. Rollback the submitted version
If you have submitted the code to the warehouse and want to rollback the submitted version, you can use the --hard parameter.
Switch to the branch to be rolled back and execute the following command:
git reset --hard <commit>
This command will Undo to the specified version and delete uncommitted changes in the working directory.
6. Rolling back the execution results
When rolling back the project version, you can run the git revert command. This command will roll back the modifications of the specified version and create a new submission to store these changes. Undo changes.
For example, executing the following command will undo commits and modifications and roll back the code to a specific commit:
git revert --no-commit <commit>..HEAD
This command will undo all changes made from
Git is a very powerful version control tool that can perform version control, rollback and other operations on projects. Mastering the functions and basic usage of Git will help developers better manage projects.
The above is the detailed content of How to roll back version in git. For more information, please follow other related articles on the PHP Chinese website!