Some undo/rollback operations of git

In the process of personal development and collaborative development, we connect our local warehouse and remote warehouse through git. The first thing we need to know is that git has a workspace, a staging area and a Git warehouse. The code we write locally is in the workspace without executing git commands.

Execute git add to mark the files in the workspace as staged and add them to the staging area.

Execute git commit to save the marked staged files to the local warehouse.

In the normal development process, the git commands we often use are: git add, git commit, git push, git pull, git merge, etc., but these commands are all under the condition that all operations are smooth. If necessary, For some undo and rollback operations, what commands can help us? Let’s sort them out together:

1. Cancellation of workspace 

For local modifications, without operating git commands, we can view the modified contents through git diff.

If we may just write some test code and want to cancel these modifications, we can undo it with the following command:

// 全部文件撤销
git checkout -- .

//单个文件撤销
git checkout -- [filename]

At this time, there is no output from git diff. Note: Once the above command is executed, the previous modifications cannot be retrieved.

2. Cancellation of the temporary storage area

 After writing the code locally, we will add it to the staging area through git add. At this time, if we want to see the modification of the file, git diff will not output anything. To view the modifications in the staging area, you can execute the following command:

git diff --staged 

At this time you can see the same output as before our git add.

If we want to withdraw the modifications from the staging area, we can do the following:

// 全部文件撤销
git reset .

//单个文件撤销
git reset [filename]

Return to the workspace state before committing the staging.

3. Revocation submitted to the local warehouse

To submit the local warehouse, we will pass the following command:

git commit -m "commit一次"   

// 查看一下操作记录
git log

 

 If you want to undo this commit, you can find the record of the previous commit:

git checkout a6dbda177723c7615a095f494e6152fda12be11f

// 或者
git reset --hard HEAD~1

//再次查看日志
git log

If you don’t want to undo the latest commit, but want to undo a previous commit:

git revert 99053897bc56d302af8f0d4ed68311d21af3174c

 After revert, a new revert commit record will be generated at the front of the commit history.

4. Modify the commit message

The commit copy is wrong and I want to modify it:

git commit --amend -m "修改正确的msg"

 An additional commit record will not be generated, but the previous one will be modified.

5. Merge into the last commit

The first time I modified the file, I submitted a commit; later I modified some things and I didn’t want to generate two commits:

git commit --amend

 6. Cancel merge

If there is a conflict after the git merge branch, you want to withdraw the merge first:

git merge --abort

Because in the recent work process, there have been some unexpected situations and some git rollback operations have been performed, so I have summarized the above several rollback and withdrawal operations, and I hope it can bring some help to everyone~

Guess you like

Origin blog.csdn.net/weixin_46422035/article/details/121532519