If an intern’s local branch XXX was deleted by mistake, and the branch XXX code was not pushed to a remote location, how can I find the commit records and code of the previous branch XXX?

The tadpole series: Three years of interview in a big factory — two questions (partial difficult)


Look at this topic is a bit interesting, a meal operation fierce as tiger……

  1. First clone a Git repository code down to the friends of hands-on practice can directly use my gitdemo warehouse address: https://github.com/2oops/gitdemo.git

  2. After clone, make some changes, then add and commit to local, as shown below

  3. Git branch a: checkout the branch. Git checkout master: checkout the branch. Git branch -d XXX (git branch -d XXX, git branch -d XXX) As shown in figure

  4. After deleting the branch, the local code for the change is gone. Now we can use git log -g to check the commit record.

  5. Git branch recover-xxx A git branch recover-xxx A git branch recover-xxx It is also recommended not to use the previous branch name.)

    At this point we can see that the code modified by the XXX branch has been retrieved.


To summarize, there are two steps:

1. Run 'git log -g' to check the commit record. 2Copy the code