都2020年了还在为错误提交代码犯愁?

1,954 阅读4分钟

1.前言

在日常的开发过程中,你难免会将不该提交的代码提交了,不该push到远程分支的代码push到了远程分支,或者解决代码冲突时,覆盖别人代码并提交。

这些突发状况在初次遇到的时候都会让你手足无措,很是慌乱,没事,看完这篇文章,就可以彻底治愈好你慌乱。

2.术语介绍

  • 工作区:开发者开发使用的区域
  • 暂存区:将新建的文件进行版本控制后,就添加到暂存区中
  • 本地仓库:部署在本机存放文件的仓库
  • 远程仓库:部署在远程存放文件的仓库

3.操作流程

正常开发流程如下:首先创建我们需要的文件--->对新建的文件进行版本控制,此时文件添加到暂存区域--->提交暂存区域的文件到本地仓库--->推送本地仓库的文件到远程仓库

4.撤回暂存区的文件到工作区

4.1 新建一个名为 a.txt 的文件

4.2 将 a.txt 文件进行暂存操作

➜  java-primary-example git:(master) ✗ git add a.txt
➜  java-primary-example git:(master) ✗ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   a.txt

4.3 将 a.txt 撤回到工作区域中

➜  java-primary-example git:(master) ✗ git reset HEAD a.txt
➜  java-primary-example git:(master) ✗ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        a.txt
nothing added to commit but untracked files present (use "git add" to track)

5.撤回已经 commit 未 push 的文件

注意:执行步骤 4.1 和 4.2 中的操作

5.1 提交 a.txt 文件

➜  java-primary-example git:(master) ✗ git commit -m 'commit a.txt file'
[master 9487c06] commit a.txt file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt
➜  java-primary-example git:(master) git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

5.2 使用--soft 模式进行撤回

➜  java-primary-example git:(master) git reset --soft HEAD~
➜  java-primary-example git:(master) ✗ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        new file:   a.txt

如上可以看到,文件已经从本地仓库回退到了暂存区中,达到了撤回已提交文件的目的

5.3 使用--mixed 模式进行撤回

➜  java-primary-example git:(master) git reset --mixed HEAD~
➜  java-primary-example git:(master) ✗ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        a.txt
nothing added to commit but untracked files present (use "git add" to track)

如上可以看到,文件已经从本地仓库回退到了工作区中,也达到了撤回已提交文件的目的

5.4 使用--hard 模式进行撤回

➜  java-primary-example git:(master) git reset --hard HEAD~
HEAD is now at f165d1a add ignore file
➜  java-primary-example git:(master) git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean

如上可以看到此模式的回退是最干净的回退,连文件都没有了

5.5 三种模式比较

序号 模式 效果
1 --soft 从名字可以得知此模式是一种柔软模式,柔软到只是从本地仓库撤回到暂存区
2 --mixed 从名字可以得知此模式是一种混合模式,混合的意思就是从本地仓库回退到工作区
3 --hard 从名次可以得知此模式是一种比较硬汉的模式,直接从本地仓库撤回并且删除文件

以上 3 种模式可以根据需求进行选择,如果不小心多提交了文件,想回退重新选择提交,可以采用--soft 模式;如果不想提交文件却不小心执行了提交命令,可以选择--mixed 模式;如果错误提交并且提交的文件统统都不想要了,那么就选择最硬气的--hard 模式。

6.撤回已经 push 的文件

注意:执行步骤 4 中的操作,新增一个 push 操作,保证文件被推送到远程服务器

6.1 查看提交日志信息

➜  java-primary-example git:(master) ✗ git log

6.2 使用 git revert 命令撤回文件

➜  java-primary-example git:(master) ✗ git revert  9aa858
➜  java-primary-example git:(master) git push

⚠️git revert 之后一定要记得执行 git push 命令