Advanced Git Commands

git log: View commit details

Sample output of git log. Use j and k on the keyboard to scroll up and down and q to exit the log view.

commit 4bdffb07c5abd0d41388c991dc03661be07fe6d0 (HEAD -> main, origin/main, origin/HEAD)
Merge: 269f9f4 11690c2
Author: Jiahao <woojiahao1234@gmail.com>
Date:   Fri May 17 21:46:40 2024 +0800

    Merge pull request #7 from francisyzy/main
    
    Update links

commit 11690c25ce187263ff77725647475fee6d5e1faa
Author: francisyzy <francisyzy@me.com>
Date:   Fri May 17 21:37:24 2024 +0800

    Add feedback QR code

commit f5458f36c67c927fcd41e65e29eb5b8a1491d7a1
Author: francisyzy <francisyzy@me.com>
Date:   Fri May 17 17:14:09 2024 +0800

    Update links

commit 269f9f48ddb550da9223d03123279910dd8c5336
Merge: a1fc9bf 01cdd59
Author: Francis Yeo <github@francisyzy.com>
Date:   Fri May 17 12:15:50 2024 +0800

    Merge pull request #3 from woojiahao/main

Each log entry will describe what that commit does. Each commit will have a commit hash, author and date.

For example this commit:

commit f5458f36c67c927fcd41e65e29eb5b8a1491d7a1
Author: francisyzy <francisyzy@me.com>
Date:   Fri May 17 17:14:09 2024 +0800

    Update links
  • commit hash is f5458f36c67c927fcd41e65e29eb5b8a1491d7a1

  • author is francisyzy <francisyzy@me.com> which was set by the author's git config

  • date of the commit is Fri May 17 17:14:09 2024 +0800

  • message of the commit is Update links

The git commit history is also viewable on the Github repository itself.

git revert: Revert a commit

Lets you 'undo' a commit. It will undo the changes made by that commit and make a new commit to do so.

Do note that this is not the method of removing sensitive information as commit history can still retrieve it.

There is a stackoverflow discussion on how to remove such information and rewrite history. Do note that rewriting history will result in other people's repo being broken.

git reset: Reset HEAD to the commit

This will reset the current HEAD to the commit hash.

Useful reset command that can be used to edit the commit message before its being pushed to the remote is: git reset --soft HEAD~. The command will reset the files to the commit right before the current commit and leave the files in the latest commit in staged. Allowing you to git commit -m "Updated message" to change the git commit message.

git checkout: Checkout files (and also a branch) commits

You can also use git checkout <commit hash> to go to view the files at a specific commit too.

A useful feature to have when you want to check out the files of that commit before you reset to that commit.

Last updated