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!
-
Clone a repo (you can use the one I created for you)
git clone https://github.com/MumenTayyem/github-basics.git
-
Create a new branch for you
git checkout -b {your-name}
-
Push your branch so you can check it online on Github
git push origin {your-name}
- Do any changes in the repo
-
Add your changes
git add .
-
Create a new commit
git commit -m "I'm testing revert"
-
Push your changes
git push origin {your-name}
-
Now, let's get the hash of the commit you created in
step#6 using
git log
-
Push the changes again
git push origin {your-name}
- 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!