

You can even combine the carets and tildes.

When you have a merge commit, that commit has two parents, and HEAD^ is the ancestry reference that is built to handle that. You don't have to use numbers, you can keep adding carets or tildes like HEAD^^^ or HEAD~~~.īoth HEAD~ and HEAD^ are the same when dealing with commits that only have one parent. So if you have 3 commits on a branch, the latest being HEAD, you can access the oldest or grandparent commit using HEAD~2. You can also use ancestry references with tildes like HEAD~. You can use any commit ref instead of HEAD. They're called ancestry or relative references because they're referring to commits based on their relation to other commits. In that same blog, I also touched on ancestry/relative references with carets (like HEAD^). I covered short SHAs, long SHAs, tags, branch names, head, and HEAD in #gitPanic - HEAD. Using time qualifiers, you can get all the commits on the main branch from the last week like You can also pass a timestamp if you want to get really specific. This is also known as git ref pointer syntax and the index of the commit in the reflog file is not the only thing you can pass. Meanwhile, and HEAD would show you the commit you have checked out. The reflog shortname refers to the head of main and refers to the second latest commit on main. They work the same when you apply them to HEAD or a branch name, because any reflog, including the stash, is basically an array of logs of refs. I covered reflog shortnames as they apply to the stash in #gitPanic - stash.
Git restore how to#
If you're just interested in how to use what reflog prints out to get back lost work, skip ahead to Reset. The next section will cover reflog shortnames and all the possible refs I could find. If you pass a ref to reflog, you can see other branches or commits that aren't HEAD. This means interactive rebase will overwrite the log, but not the reflog.īecause the reflog will only be lost after months or by running git reflog expire or git reflog delete, you can always rely on the reflog to keep a secret record of your recent commits. The log is pushed to the remote and public. The reflog is on your local machine and private. The -g option tells git log to show the reflog and not the log. In other words, git reflog is a formatted version of git log, with one key difference. In fact, git reflog show is an alias for git log -g -abbrev-commit -pretty=oneline. This will give you a list of all the commits in the branch you're in with a short SHA, reflog shortname, and commit message. Running git reflog is the same as git reflog show HEAD. Like refs, this directory has information about head logs in. When you run git reflog, you're asking git to show you the log of refs it keeps in. I touched on git log, git show, and git diff in Interactive Rebase, but reflog is even more powerful. Before I talk about discarding changes with reset, let's dig into the reflog and refs. I've talked about HEAD and passed a SHA here, but that's just the tip of the ref iceberg. Enter fullscreen mode Exit fullscreen mode
