How to Use git reflog and git cherry-pick to retrieve deleted commit records

Source: Internet
Author: User
Tags how to use git

Assume there are three commit, git status:

Commit3: Add test3.c

Commit2: Add test2.c

Commit1: Add test1.c

Simulate the loss of the commit record and execute git reset -- hard head ~ 1. The commit3 file is deleted, and the test3.c file is invisible in the working tree. To restore commit3, use git reflog and git cherry-pick.

$ Git reflog
502dd0f head @ {0}: Head ~ 1: updating head
147b3b5 head @ {1}: commit: test3
502dd0f head @ {2}: commit: Test2
0692c03 head @ {3}: Commit (initial): test1

Head @ {0}: Head ~ 1: updating head
The red bold is the deleted commit3, And the GIT reset -- hard 502dd0f can be used to run the GIT log without this record.Delete the red record and restore cmit3. use git cherry-pick.

$ Git cherry-pick 147b3b5
[Master 02c1e69] test3
0 files changed, 0 insertions (+), 0 deletions (-)
Create mode 100644 test3.c

After running git log, you can see:

Commit3: Add test3.c

Commit2: Add test2.c

Commit1: Add test1.c

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.