Git Commit History

Last Updated on 2023-03-02

[#single-git-commit-history]Viewing a single [.inline-code]git[.inline-code] commit’s history[#single-git-commit-history]

The [.inline-code]git show[.inline-code] command is used to view the changes of a specific commit:

 git show 

For example, I can type [.inline-code]git show 0f8497[.inline-code] to see the log message and the changes that occurred in this specific commit. On the left, we have the command and the right pane is the result with the diff.

[#last-git-commits]Show the last [.inline-code]git[.inline-code] commits[#last-git-commits]

To see the changes made in the last commit without using a hash, you can use the [.inline-code]git show HEAD[.inline-code] command. The [.inline-code]HEAD[.inline-code] here refers to the most recent commit within the git history of the project.war. You can use [.inline-code]HEAD~1[.inline-code] to go back an extra commit, [.inline-code]HEAD~2[.inline-code] to go back two, etc.

[#branch-entire-history]Viewing a git branch’s entire history[#branch-entire-history]

Each branch has a commit history. To list commits as a view of a branch's history, you can use the [.inline-code]git log[.inline-code] command with the branch name.

  •  [.inline-code]git log[.inline-code]: shows the commit history for the branch currently checked out. If you have not checked out a branch, this will show you the commit history of the entire repository.
  • [.inline-code]git log <branch-name>[.inline-code]: shows the commit history for the specified branch and any commits shared by it's parent branches
  • [.inline-code]git log <file-path>[.inline-code]: shows the commit history of the file path

As a developer working at a fast paced startup, I like to use [.inline-code]git log -n --oneline[.inline-code] to view a summary of the last n commits in one-liners.

The screenshot above compares just running [.inline-code]git log[.inline-code] vs [.inline-code]git log -4 --oneline[.inline-code].

[#GitHub-commit-history]Using GitHub to show commit history[#GitHub-commit-history]

To view the commit history of a repository on Github, you can click on  the “Commits” button on the homepage of the project. This re-routes to a URL of the form [.inline-code][.inline-code]  and shows a list of the commits made to the project with full detail. Below is a screenshot of the React library’s most recent commits.

[#recover-lost-commits]Use [.inline-code]git reflog[.inline-code] to recover lost commits[#recover-lost-commits]

There may be instances when you use [.inline-code]git log[.inline-code] but the commit you are searching for is not showing up. With Git, it's possible to lose a commit by accidentally using commands like [.inline-code]git reset --hard[.inline-code] or through Git's garbage collection which removes unreferenced objects from the repository. These commits may not show up when calling [.inline-code]git log[.inline-code], but you may be able to recover it using [.inline-code]git reflog[.inline-code].

Unlike [.inline-code]git log[.inline-code], [.inline-code]git reflog[.inline-code] is a local recording of changes made and tracks commits across every branch. By default, this tool keeps the record for 90 days and lets you return to old commits not referenced by any branches.

[#recall-various-commands]Use AI to recall these various [.inline-code]git[.inline-code] commit history commands[#recall-various-commands]

If you’re using Warp as your terminal, you can use Warp’s AI Command Search feature to surface the various commands to check history discussed above.

The easiest way to do this is to trigger the feature with [.inline-code]#[.inline-code], and type what you are looking for, such as:

 $ # show lost commits