This text is a part of our “Superior Git” collection. You should definitely comply with us on Twitter or join our e-newsletter to listen to about future articles!
The “Reflog” is one in every of Git’s lesser-known options—however one that may be extraordinarily useful. Some folks seek advice from it as a “security web,” whereas I like to consider it as Git’s “diary.” That’s as a result of Git makes use of it to maintain a journal about each motion of the HEAD pointer (i.e. each time you commit, merge, rebase, cherry-pick, reset, and so forth.). Git logs your actions within the Reflog which makes it a beneficial logbook and place to begin when one thing went fallacious.
On this final a part of our “Superior Git” collection, I’ll clarify the variations between git log and git reflog, and I’ll present you use the Reflog to get better deleted commits in addition to deleted branches.
Superior Git collection:
Half 1: Creating the Excellent Commit in GitHalf 2: Branching Methods in GitHalf 3: Higher Collaboration With Pull RequestsHalf 4: Merge ConflictsHalf 5: Rebase vs. Merge Half 6: Interactive RebaseHalf 7: Cherry-Selecting Commits in GitHalf 8: Utilizing the Reflog to Restore Misplaced Commits (You’re right here!)
git log or git reflog: what’s the distinction?
In earlier articles, I’ve beneficial you utilize the git log command to examine earlier occasions and take a look at your commit historical past, and that’s precisely what it does. It exhibits the present HEAD and its ancestors, i.e. its guardian, the subsequent guardian in line, and so forth. The log goes all the way in which again within the commit historical past by recursively printing each commit’s guardian. It’s a part of the repository which suggests it will get replicated after you push, fetch, or pull.
git reflog, however, is a non-public and workspace-related recording. It doesn’t undergo the checklist of ancestors. As an alternative, it exhibits an ordered checklist of all commits which HEAD has pointed to up to now. That’s why you’ll be able to consider it as some sort of “undo historical past” such as you may see in phrase processors, textual content editors, and so forth.
This native recording technically isn’t a part of the repository and it’s saved individually from the commits. The Reflog is a file in .git/logs/refs/heads/ and it tracks the native commits for each department. Git’s diary often will get cleaned up after 90 days (that’s the default setting), however you’ll be able to simply modify the expiration date of the Reflog. To vary the variety of days to 180, merely kind the next command:
$ git config gc.reflogExpire 180.days.in the past
The repository’s configuration file (.git/config) now contains the variable reflogExpire with the worth 180.days.in the past
Alternatively, you’ll be able to resolve that your Reflog ought to by no means expire:
$ git config gc.reflogExpire by no means
Tip: Do not forget that Git makes a distinction between the repository’s configuration file (.git/config), the worldwide per-user configuration ($HOME/.gitconfig), and the system-wide settings (/and so forth/gitconfig). To regulate the Reflog’s expiration date for the person or the system, add the –system or –global parameter to the instructions proven above.
Sufficient theoretical background—let me present you work with git reflog to appropriate errors.
Recovering deleted commits
Think about the next state of affairs: After taking a look at your commit historical past, you resolve to do away with the final two commits. You courageously carry out a git reset, the 2 commits disappear from the commit historical past… and some time later, you discover that this was a mistake. You’ve simply misplaced beneficial modifications and begin to panic!
Do you actually have to begin from scratch once more? You don’t. In different phrases: hold calm and use git reflog!
So, let’s mess issues up and make this error in actual life. The following picture exhibits our authentic commit historical past in Tower, a graphical Git shopper:
We need to do away with two commits and make the “Change headlines for about and imprint” commit (ID: 2b504bee) our final revision on the grasp department. All we have to do is copy the hash ID to the clipboard after which use git reset on the command line and enter that hash:
$ git reset –hard 2b504bee
Voilà. The commits have disappeared. Now, let’s assume this was a mistake and check out the Reflog to get better the misplaced knowledge. Sort git reflog to view the journal in your terminal:
You’ll discover that each one entries are ordered chronologically. Meaning: the latest—the latest—commits are on the high. And, in the event you look carefully, you’ll discover the deadly git reset motion from a couple of minutes in the past proper on the high.
The journal appears to work—that’s excellent news. So, let’s use it to undo that final motion and restore the state earlier than the reset command. Copy the hash ID (which is e5b19e4 on this particular instance) to the clipboard, like earlier than. You might use git reset once more, which is completely legitimate. However on this case, I’m going to create a brand new department primarily based on the previous state:
$ git department happy-ending e5b19e4
Let’s check out our graphical Git shopper once more:
As you’ll be able to see, the brand new department, happy-ending, has been created and it contains the commits we deleted earlier—superior, nothing is misplaced!
Let’s take a look at one other instance and use the Reflog to get better a complete department.
Recovering deleted branches
The following instance resembles our first state of affairs: we’re going to delete one thing—this time, it’s a complete department that has to go. Possibly your buyer or your workforce chief has advised you to do away with a function department, possibly it was your individual concept to wash up. To make issues worse, a commit (C3 within the image) just isn’t included in any of the opposite branches, so that you’re positively going to lose knowledge:
Let’s really do that after which get better the department later:
Earlier than you’ll be able to delete the department function/login, you could step away from it. (As you’ll be able to see within the screenshot, it’s the present HEAD department, and you may’t delete the HEAD department in Git.) So, we’re going to modify branches (to grasp) after which we’re going to delete function/login:
Okay… now let’s say our buyer or workforce lead had a change of coronary heart. The function/login department (together with its commits) is needed in spite of everything. What ought to we do?
Let’s check out Git’s diary:
$ git reflog
776f8ca (HEAD -> grasp) HEAD@{0}: checkout: transferring from function/login to grasp
b1c249b (function/login) HEAD@{1}: checkout: transferring from grasp to function/login
[…]
Seems we’re fortunate once more. The final entry exhibits our change from function/login to grasp. Let’s attempt to return to the state proper earlier than that and replica the hash ID b1c249b to the clipboard. Subsequent, we’re going to create a department known as function/login primarily based on the specified state:
$ git department function/login b1c249b
$ git department -vv
function/login b1c249b Change Imprint web page title
* grasp 776f8ca Change about title and delete error web page
Nice—the department is again from the useless and in addition contains that beneficial commit we thought we had misplaced:
For those who’re utilizing Git in a desktop GUI like Tower, you’ll be able to merely press CMD+Z to undo your final motion, similar to a textual content editor or phrase processor once you make a typo.
Preserve calm and hold monitor
Git’s Reflog generally is a actual lifesaver! As you’ll be able to see, it’s fairly straightforward to deliver misplaced commits and even complete branches out from the grave. What you could do is locate the proper hash ID within the Reflog—the remaining is a chunk of cake.
If you wish to dive deeper into superior Git instruments, be at liberty to take a look at my (free!) “Superior Git Package”: it’s a group of quick movies about matters like branching methods, Interactive Rebase, Reflog, Submodules and far more.
This was the final half in our collection on “Superior Git” right here at CSS-Tips. I hope you loved the articles. Pleased hacking!
Superior Git collection:
Half 1: Creating the Excellent Commit in GitHalf 2: Branching Methods in GitHalf 3: Higher Collaboration With Pull RequestsHalf 4: Merge ConflictsHalf 5: Rebase vs. Merge Half 6: Interactive RebaseHalf 7: Cherry-Selecting Commits in GitHalf 8: Utilizing the Reflog to Restore Misplaced Commits (You’re right here!)
The submit Utilizing the Reflog to Restore Misplaced Commits appeared first on CSS-Tips. You possibly can help CSS-Tips by being an MVP Supporter.
Subscribe to MarketingSolution.
Receive web development discounts & web design tutorials.
Now! Lets GROW Together!