How Git rolls back code
foreword
The version management tool most used in work now is git, and I believe everyone in Internet companies is using it. Therefore, today I will give a brief introduction to the use of git and common commands, one is used as a learning record for myself, and the other is used as a memo for work. Of course, a small partner in need can also be used as an answer reference item for search questions. Well, without further ado, let's introduce how git rolls back code, today's topic?
We will introduce the following three situations:
One is that the code has not been submitted to the staging area
The other is that the code has been submitted to the staging area
The last one is that the code has been pushed to the remote warehouse
Let's first explain the term temporary storage area :
Staging area: There are a lot of things stored in Git's version repository, the most important of which is the staging area called stage (or index), as well as the first branch that Git automatically created for us master
, and master
a pointer to it calledHEAD
.
1. The code has not been submitted to the staging area
Git will tell you that you git checkout -- file
can discard changes to the workspace:
$ git checkout --readme.txt
git checkout -- readme.txt
The meaning of the command is to readme.txt
undo all the modifications of the file in the workspace. There are two situations here:
One is
readme.txt
that it has not been placed in the temporary storage area since the modification, and now, undoing the modification will return to the same state as the version library;One is
readme.txt
that after it has been added to the temporary storage area, it has been modified again. Now, undo the modification and return to the state after adding to the temporary storage area.
git checkout --file
The command in the command is --
very important. If it is not , it becomes the command of "switch to another branch". We will encounter the command --
again in the later branch management .git checkout
2. The code has been submitted to the staging area
Git also tells us that git reset HEAD <file>
the changes to the staging area can be undone (unstaged) and put back into the workspace with the command:
$ git reset HEAD readme.txt Unstaged changes after reset: M readme.txt
git reset
The command can either roll back the version or roll back the modification of the staging area to the working area. When we use HEAD
it, it means the latest version
3. The code has been pushed to the remote warehouse
In actual work, how can we remember in our minds what is changed every time in a file with thousands of lines, otherwise what would we do with the version control system. There must be some command in the version control system that can tell us the history, and in Git, we look at it with the git log
command:
git log
Command to display commit logs from most recent to furthest
If you think that the output information is too much and you are dazzled, you can try adding --pretty=oneline
parameters
First, Git must know which version the current version is. In Git, the HEAD
current version is represented by
The last version is HEAD^
, the last version is , of course , it is easier to HEAD^^
write 100 of the 100 versions above , so it is written .^
HEAD~100
to use the git reset
command:
$ git reset --hard HEAD^
You can specify a version back in the future:
$ git reset --hard commitId
Now, you roll back to a certain version, turn off your computer, regret it the next morning, and want to revert to a new version? What if I can't find the new version commit id
?
In Git, there is always regret medicine. When you $ git reset --hard HEAD^
roll back to the x
xx version, and then want to restore to yyy, you must find x
the commit id of xx. Git provides a command git reflog
to log every command you make:
$ git reflog
in conclusion:
HEAD
The version pointed to is the current version, so Git allows us to traverse the history of versions using commandsgit reset --hard commit_id
.Before switching,
git log
you can view the commit history to determine which version to roll back to.To go back to the future, use the
git reflog
command history to see which version of the future to go back to.
atorvastatin medication & lt;a href="https://lipiws.top/"& gt;atorvastatin 10mg usa& lt;/a& gt; buy lipitor for sale
Yuhbxw
2024-03-11