The local code is deleted and the scene is restored, and the git command retrieves the code

Use the scene, I hope everyone will not make such a mistake

The thing is like this, in the development of the company, because the computer is too broken, the company changed me to a computer with better configuration, but the computer was used by someone else. After I installed the environment, since the computer already has git, I directly started Pulled the code, then cleared the git account, and then the problem came. (Remember to be sure, first clear the git account and mailbox and then pull the code for development)

Under what circumstances will the local code be deleted by mistake?

When I submit the code after development, I can only add but cannot commit. It reminds me that my branch is clean and has not been tracked. The answer given to me on the Internet is to force push, so I force push, and the code is deleted directly, so I panic , I subconsciously looked at the git window, and suddenly found that...
insert image description here
I don’t have this branch at all, and this status should still be someone else’s account. Since the branch is not corresponding, the local warehouse is not stored in it, and the forced push directly caused the code to be lost. Rewrite it? ? Rewriting is impossible, so let me share how I got it back.

git retrieves local code

At this time, the first thing I thought of was to print the log. When I used git log, I found that I was wrong. This command can only print every submission record of the remote warehouse. My code is lost locally, so I can’t find it. No record, next I executed another command git reflog.
insert image description here
This command will print the record of your local submission every time. The graffiti is your local version id. You only need to find the version you want to go back to. Execute the following command.
insert image description here
git reset --hard 'the local version id mentioned above', after executing this command, the lost code will be found, but now this branch is not my branch, at this time I switched to the master branch and I found that it is still The state when the code was just pulled down was very clean, so I switched back to the previous branch (the state of other people's accounts), so I executed the following command.

// 就是把当前回退回来的版本的代码,复制一份并切换到我账号的dev分支上
git checkout -b dev
//因为别人账号上的代码我无法管理,无法合并,无法push

Because I don't need so many branches for a development, I switched back to the master branch and executed the following command

// 就是把dev上的回退回来的的代码,合并到master分支上来
git merge origin/dev
//因为我是单人开发,只有一个分支对应了远程分支。

Now that the code has been brought back on track, I don't need to talk about the following operations. Direct regular git three bursts will do.

Guess you like

Origin blog.csdn.net/m0_52313178/article/details/119065815