Blogs

Git Basics Part 2: git log and git revert

Introduction

In this quick blog, we will see how to check the current branch's commits using the terminal only. This method can be highly beneficial when checking whether you successfully merged or rebased your branch. Rest assured, I will also guide you through the process of reverting a commit that was pushed accidentally, making it a breeze.
The commands we are going to explain are git log and git revert.

Command#1: git log

git log is very simple; It lets the user check the current branch commits. To use it, open the terminal, navigate to your repo, and do git log. This command is what we are going to use to revert bad commits.

Command#2: git revert

From its name, git revert allows you to revert a bad commit/s in a single line. Let's say you have the below branch where the alphabets refer to the commits (hashes).

A---B---C---D main branch

D is the commit you wish to revert; all you have to do is run git revert D (where D is the commit hash), then push, and that's it!

Let's now put the two commands together into action!

  1. Clone a repo (you can use the one I created for you)
    git clone https://github.com/MumenTayyem/github-basics.git
  2. Create a new branch for you
    git checkout -b {your-name}
  3. Push your branch so you can check it online on Github
    git push origin {your-name}
  4. Do any changes in the repo
  5. Add your changes
    git add .
  6. Create a new commit
    git commit -m "I'm testing revert"
  7. Push your changes
    git push origin {your-name}
  8. Now, let's get the hash of the commit you created in step#6 using
    git log
  9. Push the changes again
    git push origin {your-name}
  10. Check the results, and you will see that the changes you made in step#4 no longer exist in the repo.

Summary

In this blog, you learn how to see your branch's current commits in the terminal and the quickest way to revert bad commits! I hope this is helpful for you!