Git reflog rolls back local code

Time:2021-7-22

background

  • Programmer a makes three local commit ‘Demo1’, ‘demo2’ and ‘demo3’
  • Programmer a accidentally rolls back git reset — hard ‘commit1’, and rolls back to the first commit
  • Programmer a modifies the file and makes a commit, ‘demo4’

Q: how to retrieve the two reset commit and merge the latest commit ‘demo4’

Using git log, you can only see the submission records of Demo1 and demo4
Git reflog rolls back local code

git reflog

Git reflog can view all operation records of all branches (including deleted commit records and reset operations)
Recovery steps

git reflog 
git reset  --hard  hash

Git reflog rolls back local code
List all the submission records, and then you can roll back. Here, you need to roll back the submission to demo3
Git reflog rolls back local code
It has been restored to the previous code, so the problem comes, the submission of demo4 is eaten, how to solve it?

git cherry-pick

Git cherry pick can copy one or more commits from another branch to the current branch
Recovery steps

git cherry-pick  hash

Git reflog rolls back local code


End, it’s done