记一次本人代码提交丢失事故

前情提要:
公司代码提交规范是Merge Request,平时push在自己fork的远程仓库,开发完成后用Merge Request合并到上游仓库。

之前我负责的功能是在wsc分支上工作的,push到了自己的远程仓库,并且发起Merge Request,然后被后续可能切换分支为理由打回。

今天项目负责人通知我要切换到saas2.2分支,目前该分支与wsc分支保持一致,仅存在于上游仓库。

于是我使用git fetch upstream拉取上游仓库saas2.2分支信息,并且checkout到本地。

1
2
3
10:10:23.544: [***] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout -b saas2.2 upstream/saas2.2 --
Branch 'saas2.2' set up to track remote branch 'saas2.2' from 'upstream'.
Switched to a new branch 'saas2.2'

然后将本地saas2.2分支push到远程仓库saas2.2分支(在IDEA中选择了origin作为remote):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
10:10:42.123: [***] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/saas2.2:saas2.2
Enumerating objects: 67, done.
Delta compression using up to 4 threads
Total 33 (delta 12), reused 0 (delta 0)
remote: hooks/pre-receive:3: warning: Insecure world writable dir /opt/gitlab/embedded in PATH, mode 040777
remote: hooks/update:3: warning: Insecure world writable dir /opt/gitlab/embedded in PATH, mode 040777
To http://192.168.3.112:8888/qhs/***.git
remote: hooks/post-receive:3: warning: Insecure world writable dir /opt/gitlab/embedded in PATH, mode 040777
remote:
* refs/heads/saas2.2:refs/heads/saas2.2 [new branch]
remote: To create a merge request for saas2.2, visit:
remote: http://192.168.3.112:8888/qhs/***/merge_requests/new?merge_request%5Bsource_branch%5D=saas2.2
remote:
Done

然后将本地wsc分支合并到本地saas2分支,然后再次push。

然后这次我忘了设置remote:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
10:10:57.803: [***] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false merge wsc
10:11:05.785: [***] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain upstream refs/heads/saas2.2:saas2.2
Enumerating objects: 25, done.
Delta compression using up to 4 threads
Total 9 (delta 2), reused 0 (delta 0)
remote: hooks/pre-receive:3: warning: Insecure world writable dir /opt/gitlab/embedded in PATH, mode 040777
remote: hooks/update:3: warning: Insecure world writable dir /opt/gitlab/embedded in PATH, mode 040777
remote: hooks/post-receive:3: warning: Insecure world writable dir /opt/gitlab/embedded in PATH, mode 040777
remote:
remote: To create a merge request for saas2.2, visit:
remote: http://192.168.3.112:8888/root/***/merge_requests/new?merge_request%5Bsource_branch%5D=saas2.2
remote:
To http://192.168.3.112:8888/root/***.git
refs/heads/saas2.2:refs/heads/saas2.2 a4cc8e2..de8714c
Done

然后我hard reset了本地的wsc分支,

1
2
3
4
10:11:15.153: [***] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout wsc --
Your branch is up to date with 'origin/wsc'.
Switched to branch 'wsc'
10:12:01.366: [***] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false reset --hard c84bdf0753dd540fcb8312b324d1f848aae938a8

并且force push到了远程仓库。至此本地、远程仓库的wsc分支丢失了本次提交。

看起来一切都很美好,直到我看到了我之前忘记设置remote的push引发的WebHook钉钉机器人提醒。。

于是我将本地saas2.2分支 hard reset,并且force push到了上游仓库。。

1
2
3
4
10:15:00.241: [***] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout saas2.2 --
Your branch is up to date with 'upstream/saas2.2'.
Switched to branch 'saas2.2'
10:15:42.094: [***] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false reset --hard a4cc8e2b7ce5dfdc0c25c98b63a77a12c3aa09f3

至此本地、上游仓库的saas2.2分支丢失了本次提交,并且我在合并前就将saas2.2分支push到远程仓库,因此远程仓库也没有此提交记录。。

万幸Gitlab上有关闭的Merge Request记录,可以查看到代码改动。。

得到的经验教训是:使用git push -f之前,一定要仔细看一下提交记录,push之前也要仔细看一下remote到底是什么。。

幸好干了两年活只丢过这么一次代码,还是丢的我自己的。。