Commands
git-reflog
- Manage reflog information- Reference logs, or “reflogs”, record when the tips of branches and other references were updated in the local repository. Reflogs are useful in various Git commands, to specify the old value of a reference. For example,
HEAD@{2}
means “whereHEAD
used to be two moves ago”,master@{one.week.ago}
means “where master used to point to one week ago in this local repository”, and so on. See gitrevisions(7) for more details.
- Reference logs, or “reflogs”, record when the tips of branches and other references were updated in the local repository. Reflogs are useful in various Git commands, to specify the old value of a reference. For example,
git-for-each-ref
- Output information on each ref- Example:
git for-each-ref refs/tags
- Example:
Separating a subdirectory out into a separate repo
See 👉 Splitting a subfolder out into a new repository - GitHub Docs which uses git-filter-repo
Difference between double and triple dot in git diff
and git log
See Double-Dot “..” vs. Triple-Dot “…” in Git Commit Ranges Baeldung on Ops
Summary of what using dots notation means for git diff and git log:
Command | Descriptions |
---|---|
$ git log branch1..branch2 | displays the commits that are in branch2 but not in branch1 (main) |
$ git log branch1…branch2 | shows the commits that are included in either branch1 or branch2, but not both |
$ git diff branch1..branch2 | shows the full differences between any two commits of branch1 and branch2 |
$ git diff branch1…branch2 | displays the differences between the common ancestor of both branches and another branch |
Moreover, we use these notations to compare different sets of commits, which is useful for many Git operations.
Git Tags
- Can be annotated - a tag object - or lightweight - pointer to another ref e.g. a commit.
Key Differences: Annotated vs Lightweight tags
Table below llm-generated – Source: ChatGPT
Feature | Annotated Tag | Lightweight Tag |
---|---|---|
Stored as a Git Object? | ✅ Yes — a tag object | ❌ No — just a ref (pointer) |
Contains metadata? | ✅ Yes (tagger, date, message, etc.) | ❌ No |
Object Type (git cat-file ) | tag | same as target (e.g., commit ) |
File in .git/objects/ ? | ✅ Yes — an actual file in objects/ | ❌ No |
File in .git/refs/tags/ | ✅ Yes — points to tag object’s hash | ✅ Yes — points directly to commit |
Can be signed (-s )? | ✅ Yes | ❌ No |
Display Logs of Tagged Commits
Partlyllm-generated - source - and partly my revisions/re-writing
Question
how can i get the git tags for my repo in the right order, like commits for git log? (reverse chronological) Or better yet, can I run a git log that filters to display only commits which are tagged (any tag type)
Tags in Git aren’t naturally “ordered” because lightweight and annotated tags are just refs pointing to objects.
1. Show all tags sorted by commit date (reverse chronological, like git log
) - If you want a sorted tag list with metadata
git tag --sort=-creatordate
--sort=-creatordate
sorts tags by the commit date of the tagged object.- Replace
-creatordate
withcreatordate
for chronological order.
If you want to see the commit info alongside:
git for-each-ref --sort=-creatordate --format '%(refname:short) %(creatordate) %(subject)' refs/tags
2. Run git log
showing only tagged commits - If you want a tag list like git log
git log --decorate --tags --simplify-by-decoration
-
--simplify-by-decoration
collapses the log to only commits that have tags (or other refs). -
--tags
ensures tags are shown in decorations. -
Add
--oneline
if you want it compact:git log --oneline --decorate --tags --simplify-by-decoration
3. If you want just the commit hashes of tagged commits (reverse chronological)
git rev-list --tags --date-order --reverse
-
Add
--pretty
if you want more details:git rev-list --tags --date-order --pretty
Git Reflog
This section is llm-generated
git reflog
is a built-in Git command that records the history of references, such as commits, branch creations, branch deletions, and other updates. It serves as a safety net, allowing you to recover lost or deleted commits and branches even when they are no longer visible in regular Git logs Source 2.
By default, git reflog
outputs the reflog of the HEAD
reference, which is considered to be an iconic reference to the branch that is currently active. You can access a git ref by using the name@{qualifier}
syntax. For example, to get the entire reflog of all the references, you can execute git reflog show --all
Source 0.
There are several subcommands of git reflog
that provide additional functionality:
-
git reflog show
: This subcommand is an alias forgit log -g --abbrev-commit --pretty=oneline
. It displays a chronological list of reflog entries for the current branch, highlighting the commit hashes, actions, and timestamps Source 0. -
git reflog expire
: This subcommand is used for cleaning old or unapproachable reflog entries. The danger of this subcommand is the possibility of data loss. In fact, the expire subcommand is not used by users. It is used only by the Git. By default, the reflog date of expiry is set to 90 days. Pass--expire=time
argument togit reflog expire
for specifying an expire time Source 0. -
git reflog delete
: This subcommand is designed for deleting passed reflog entries. However, the end users avoid executing this subcommand, because, as in the case ofgit reflog expire
, there is a risk of data loss Source 0.
The reflog is a powerful tool for maintaining a safety net in your Git repository and recovering from various accidental or unexpected changes, such as those times when you need to recover lost commits or branches and have lost track of your Git history Source 5.
For example, if you accidentally reset or delete a branch, the reflog keeps a record of the previous state, including commit hashes. You can use git reflog
to find and recover a lost commit by examining its entries Source 2.
Here is a basic example of how to use git reflog
:
# Show the reflog for the current branch
git reflog
# Show the reflog for a specific branch
git reflog show test_branch
# Show the reflog for a git stash
git reflog stash
# Use a reflog entry to checkout a previous state
git checkout HEAD@{1}
Remember that git reflog
is a local command and its entries are not pushed to remote repositories by default. It is a useful tool that can be used in DevOps workflows Source 2.