1. Code revocation
At present, everyone is using it git
. In project development, there will always be problems with the submitted code. If you want to undo the rollback operation, I will introduce you separately about the undo rollback code.
If the normal submission code is to be submitted to the remote warehouse, there are the following 3-step commands:
git add . ------------> git commit -m 说明 -------------> git push
(添加到暂存区) (提交到本地仓库) (推送到远程仓库)
In git
, the command to revoke the code is mainly git reset
, but there are 3 modes of git reset --soft
, git reset --hard
, and . git reset --mixed
These three commands are mainly for the rollback of the code after the warehouse has been submitted, and basically all of them will be rolled back with the commit log version number. The different meanings represented by the three modes are as follows:
-
--soft
: This command indicates that the codecommit
will not change locally after the code is revoked to the temporary storage area. -
--mixed
: This command indicates that the codecommit
will not change locally until the code is revoked to the temporary storage area. -
--hard
: This command means to roll back the code to a certain version, and the code will be changed to the specified version locally, so be careful.
What is to be said below is simply as shown in the figure below:
Emphasis again: --hard
command, be careful when using it, it will directly roll back the code and disappear. Even the newly written local ones are cleared.
2. Undo in different modes
The following explains the phenomenon after revocation around the above submission steps and revocation mode. Before talking about undo, let's get familiar with two commands git status
, git log
.
First of all, git status
through this command, you can see which branch the current code is under and the code situation in the workspace.
As shown in the figure above, after entering in the code workspace git status
, you can see that the current code is v1.0
under the branch, and there is one file in the current workspace 5.txt
that has not been submitted to the temporary storage area.
git log
The command is to view the record of the code submission, and you can see the submission information and version number (SHA code). As shown in the figure below:
Of course, the command execution location I mentioned in this article needs to be git
executed in the project workspace, usually under the project root path.
For code undo, I mainly think of the following types of undo, 提交暂存区的撤销(add撤销)
, 提交本地仓库撤销(commit撤销)
, 推送远程仓库撤销(git push之后的撤销)
. details as follows:
2.1 add cancel
When the code is git add .
submitted to the temporary storage area, if you want to withdraw from the temporary storage area, there are mainly the following methods.
2.1.1 git reset
Revocation
If you want to withdraw the code submitted to the temporary storage area directly, the most convenient way is to enter git reset
the command. This command or input git reset HEAD
has the same effect. This step undoes the code to git add .
its pre-execution state, as shown in the image below:
2.1.2 git restore
Revocation
Of course, there is another command that is git
recommended, but it feels less git reset
convenient. That is, in the line above the green text in the figure above, git restore --staged
command.
git restore --staged .
or:
git restore --staged 文件名
2.1.3 git reset --mixed
Revocation
At the beginning, we mentioned three modes of revocation, among them --mixed
, if you want to revoke the code just submitted to the temporary storage area, you can also do the following:
git reset --mixed
But those three modes --soft
, --mixed
, --hard
are mainly used to roll back the code based on the committed code version number , which is a bit overkill here. In addition, only --mixed
the undo operation can be completed without writing the version number , and the other two have no effect without writing the version number (except HEAD^).
Ask a question: Can I use the code before --soft
it can be rolled back git add .
? Can usage --hard
be rolled back? In addition, what will happen to the rollback?
2.2 commit revocation
After the code is git commit -m 备注说明
submitted to the local warehouse, if you want to roll back the code, there are several ways to roll back the code in different states. Main usage --soft
, --mixed
mode. As for the method of appending the version number later, it is only --soft
introduced in the mode below.
2.2.1 Withdraw to the temporary storage area
Only after the code is revoked to the temporary storage area, if it has not been added to the local warehouse, you can only use --soft
the mode. To use this mode, you need to add the version number. There are mainly the following ways of writing.
2.2.1.1 git reset --soft HEAD^
Writing
HEAD^
Indicates revocation to the previous version number, one of which ^
means the last time, ^^
means the last time, and so on. In addition, it should be noted that the code can be revoked to the subsequent state (temporary storage area state) without windows
normal input under the system . But it needs to be entered under the system .git reset --soft HEAD^
git add .
windows
git reset --soft HEAD^^
This is because windows
the default line break in the cmd console under the system is ^
not, \
if there is less input, it will prompt more? , which means to ask you whether you need to enter the next line, and ^
the symbol is regarded as a newline character and ignored by the git command.
Therefore, if windows
the following is undo 2 times commit
, you need to enter 4 times ^
.
解释:当每次commit之后,都会有提交的记录版本号(通过输入git log命令查看),通过HEAD^只能撤销一次,
拼接多个就会撤销多个。
2.2.1.2 git reset --soft HEAD~
Writing
git reset --soft HEAD~1
Command, which 1
means to undo the last submission, if it is the previous one ~2
.
This command does not distinguish between windows
system and non-system windows
issues, and this command can be used first.
2.2.1.3 git reset --soft 版本号
Writing
If the version you want to revoke was long ago and cannot be ^
locked with a number or multiple numbers, in this case, you can git log
view the previous submission records by using , and then revoke according to the version number, git reset --soft 版本号
.
Also see yourself needing to use --mixed
, --hard
(cautiously) to fallback to different modes of code.
As shown in the figure below, it is the log situation viewed:
D:\workspaces\testproject>git log
commit 15047ee4229c4c0255a62816290c2a41068e8327 (HEAD -> master)
Author: lingsf <944916889@qq.com>
Date: Tue Jan 3 20:16:53 2023 +0800
提交9文件
commit 09f0fd5e00d863ccab839824c0ac11f2c82dabf2
Author: lingsf <944916889@qq.com>
Date: Tue Jan 3 20:04:43 2023 +0800
提交8文件
commit 9e10843e96f7cd5a138f405231f06c61e83c4429
Author: lingsf <944916889@qq.com>
Date: Tue Jan 3 19:48:54 2023 +0800
提交7文件
commit 05fd4925cf0e4341a404d5c5596470d6053d7319
Author: lingsf <944916889@qq.com>
Date: Tue Jan 3 16:40:48 2023 +0800
Fall back to 提交7文件
this version:
D:\workspaces\testproject>git reset --soft 9e10843e96f7cd5a138f405231f06c61e83c4429
D:\workspaces\testproject>git log
commit 9e10843e96f7cd5a138f405231f06c61e83c4429 (HEAD -> master)
Author: lingsf <944916889@qq.com>
Date: Tue Jan 3 19:48:54 2023 +0800
提交7文件
2.2.2 Before withdrawing to the temporary storage area—git reset --mixed
If you want to undo to the temporary storage area (that is, git add .
before), you only need to --soft
modify to --mixed
. Other usage is the same as above.
2.3 Modify comments after commit
In fact, many times, we may revoke the code just because the comments are not well written and we want to revise it again. If you just want to modify the next commit
comment, you can use the command to enter the vim editing space commit
before pushing the remote branch, enter the input to enter the edit mode, modify the top comment text, click the key after the modification, and then enter to save and exit That's it. As shown below:git commit --amend
i
esc
:wq
Of course, if it has been pushed to the remote branch, although the log content can also be modified through this command, the new log record will be submitted after modification, and there will still be old comments on the remote.
In addition, if we submit twice locally commit
and fill in the instructions twice, we can only modify the last one.
2.4 Undo after the remote warehouse has been pushed
There is no difference between pushing to a remote warehouse and submitting to a local warehouse. You can still use the three modes --soft
, --mixed
, and --hard
undo. No matter what mode you use, the revocation is our local code, which has nothing to do with the remote. As for whether you merge back to the original branch or pull a new branch after you revoke the code, the code will not affect the remote branch.
Use --hard
(with caution) to fallback code for different modes.
In addition, the code records that have been pushed to the remote branch cannot be undone. You can only undo the local changes and submit them back, but you cannot undo the previous submission records.
3. Practical operation
3.1 Scenario 1: The online branch is covered and new functions are added.
Once we added a follow-up rental inventory demand for an online project, and the research and development was directly developed by the online main branch. However, newly developed functions cannot be launched directly due to testing and other problems. Later, several bugs were raised online and needed to be modified. Because the online branch has already added the code of the subsequent rental stock, in this case, if you want to roll back the online branch, you can take the following steps.
3.1.1 Pulling a new temporary branch
First, pull out a new temporary branch based on the current branch (including the subsequent rent demand of the stock).
3.1.2 Find the rollback location through the commit record
Based on the submission instructions (here reflects the importance of correctly writing the code submission remarks), find the demand point where the code first submits the subsequent rent stock, and record the version number before it. As follows:
After analysis, it is necessary to roll back to 19f5a4a3
this time before being able to roll back to the state of the online branch.
3.1.3 Based on --hard
fallback codes
Based on the version number on the right side of the above picture 19f5a4a3b87f0478a9ed061de5fa0e756a36c295
, click Copy, and then in the newly pulled temporary branch, enter git reset --hard 19f5a4a3b87f0478a9ed061de5fa0e756a36c295
to complete the code rollback of this branch. After the test has no problem, then merge the branch or rename it to a new line on the master branch.
3.2 Scenario 2: Some functions have been written in the local code, but not yet submitted
If you add new functions locally on the main branch, it is mainly because you have added a lot of developed things and have not yet submitted them. At this time, you may urgently solve a problem online and need to modify it, but the newly added code has not been completed yet, and you need to To change the previous branch, this should be done at this time.
3.2.1 New branch based on the current code
Do not submit the code to the warehouse. If it is submitted, follow the previous steps (cannot use –hard) to roll back, and then use it first to pull out git checkout -b 新分支名
a branch. Commit the code to at least the local repository on this branch.
3.2.2 Switch back to the original branch
When the new branch code has been submitted, and then switch back to the main branch, this branch is still an online branch, and the required problems can be urgently modified.
4. Summary
Code undo is a frequently used function, and the most commonly used commands are git reset
and git reset --amend
modify comments. In the process of using, be sure to pay attention --hard
, and you will know the difference with more practice.