This chapter mainly introduces the git reflog command. Git uses a mechanism called reference logs or "reflogs" to track updates at the top of branches. Many git commands accept parameters that specify a reference or "ref", which is a pointer to a commit. Common examples include:
Reflogs tracks the update time of GIT references in the local repository. In addition to the branch prompt reference log, a special reflog is maintained for git storage. Reflogs are stored in the local warehouse Git directory. The git reflog directory can be found in git/logs/refs/heads / and git/logs/HEAD. If it is used in the warehouse git stash , it can also be in Found in git/logs/refs/stash.
This chapter will introduce the extended configuration options of GIT reflog, common use cases and defects of GIT reflog, how to undo changes using git reflog, and so on.
Basic usage of git reflog
The following is the most basic reflog usage
$ git reflog
This is essentially a shortcut, equivalent to:
$ git reflog show HEAD
This will output {HEAD} reflog. We should see output similar to the following:
eb1050b (HEAD -> feature_branch) HEAD@{0}: checkout: moving from main to feature_branch 1525c48 (origin/main, main) HEAD@{1}: checkout: moving from 2bf1773d87a7806cda25d4d313995bb08adbabf5 to main 2bf1773 HEAD@{2}: commit: rebas b71dc26 HEAD@{3}: rebase: Add undo change command content 31c9ba0 (origin/jiyik, jiyik) HEAD@{4}: rebase: checkout jiyik eb1050b (HEAD -> feature_branch) HEAD@{5}: commit: Adds new feature 1525c48 (origin/main, main) HEAD@{6}: checkout: moving from main to feature_branch 1525c48 (origin/main, main) HEAD@{7}: commit (merge): Conflict resolution
Reflog reference
By default, git reflog will output the reflog of HEAD ref. HEAD is a symbolic reference to the currently active branch. Reference logs can also be used for other references. The syntax for accessing git ref is {name@{qualifier}. In addition to HEAD references, other branches, labels, remote and Git stores can also be referenced.
You can obtain the full reference log of all references by executing the following command:
$ git reflog show --all
To view the reference log of a specific branch, pass the branch name to git reflog show
$ git reflog show jiyik
Executing the above command will display the reference log of the jiyik branch.
Timed reflog
Each reflog entry has a timestamp. These timestamps can be used as qualifier markers for Git reference pointer syntax. This filters Git reference logs by time. Here are some examples of available time qualifiers:
- 1.minute.ago
- 1.hour.ago
- 1.day.ago
- yesterday
- 1.week.ago
- 1.month.ago
- 1.year.ago
- 2011-05-17.09:00:00
Time qualifiers can be combined (for example, 1.day.2.hours.ago) and accept the plural (for example, 5.minutes.ago).
The time qualifier refs can be passed to other git commands.
$ git diff main@{0} main@{1.day.ago}
This example compares the current main branch with the main branch 1 day ago. This example is useful if you want to know what has changed in a time range.
git reflog subcommand and configuration options
git reflog accepts some additional parameters that are considered subcommands.
Show - git reflog show
show is implicitly passed by default. For example, the following command:
$ git reflog main@{0}
Is equivalent to the following command
$ git reflog show main@{0}
In addition, git reflog show is git log -g --abbrev-commit --pretty=oneline Alias for.
Expire - git reflog expire
The expire subcommand clears old or inaccessible reflog entries. The expire subcommand may lose data. This subcommand is usually not used by the end user, but is used internally by GIT. Passing the - n , or -- dry run , option to git reflog expire will perform a "trial run", which will output which reflog entries are marked for modification, but they will not be modified in practice.
By default, the reflog expiration date is set to 90 days. You can pass the command line parameter -- expire=time , to git reflog expire or set GC The GIT configuration name of reflogexpire to specify the expiration time.
Delete - git reflog delete
The delete subcommand is self explanatory and will delete the incoming reflog entries. Like expire, delete may lose data and is not usually called by the end user.
Recover lost submissions
Git will never really lose anything, even when performing a history rewrite operation (such as rebasing or submitting changes). For the next example, let's assume that we have made some new changes to our warehouse. Let's use the following command to view the log information
$ git log --pretty=oneline
338fbcb41de10f7f2e54095f5649426cb4bf2458 extended content
1e63ceab309da94256db8fb1f35b1678fb74abd4 bunch of content
c49257493a95185997c87e0bc3a9481715270086 flesh out intro
eff544f986d270d7f97c77618314a06f024c7916 migrate existing content
bf871fd762d8ef2e146d7f0226e81a92f91975ad Add Git Reflog outline
35aee4a4404c42128bee8468a9517418ed0eb3dc initial commit add git-init and setting-up-a-repo docs
Then we commit these changes and do the following:
$ git commit -am "some WIP changes"
At this point, we enter interactive mode by executing the following command
$ git rebase -i origin/main
During rebase, we use the s rebase subcommand to mark the commit as squashed. During rebase, we compressed some submissions into the most recent "some WIP changes" submission.
Because we compressed the commit, the git log output now looks like:
40dhsoi37656e19d4e4f1a9b419f57850ch87dah987698hs some WIP changes
35aee4a4404c42128bee8468a9517418ed0eb3dc initial commit add git-init and setting-up-a-repo docs
If we check the git log at this time, it seems that we no longer have submissions marked as compressed. What if we want to operate on one of the compressed submissions? Remove its changes from history? At this point, you can use git reflog.
$ git reflog 37656e1 HEAD@{0}: rebase -i (finish): returning to refs/heads/git_reflog 37656e1 HEAD@{1}: rebase -i (start): checkout origin/main 37656e1 HEAD@{2}: commit: some WIP changes
We can see that there are reflog entries at the beginning and end of rebase. Before these entries is our "some WIP changes" submission. We can pass the reflog reference to git reset And reset to the submission before rebase.
$ git reset HEAD@{2}
Executing this reset command will move the HEAD to the submission with "some WIP changes" added. In essence, it is to restore other compressed submissions.
git reflog summary
In this tutorial, we discussed the git reflog command. Some key points covered are:
- How to view the reflog of a specific branch
- How to use reflog undo git rebase
- How to specify and view time-based reflog records
We briefly mentioned that git reflog can be used with other git commands, such as git checkout,git reset And git merge . Learn more in their respective chapters.