Git Command Series #2

Nana Lau
5 min readJun 15, 2021

For this week’s blog, I am going to continue talking about other interesting Git command that I came across this week. Nothing really special but could be quite useful and handy for the future me therefore here it is! Let’s dive in!

The first one that I would like to talk about is git reset. It is particularly useful when you have accidentally done some commits on the master branch instead of the new feature or bugfix branch that you are suppose to be working on. How can you go back in time to get this issue fixed? One of the solutions is by using git reset.

By using git reset, it will be able to reset the commits back to a specific commit that you want. After you execute the command, the in-between commits will be gone from your commit log history. You can then create and switch to a new branch and continue your work on the newly created branch.

First, you will need to do git log or git log --oneline(for a condensed log of all commits) to see all commit history on the branch that you are currently working on:

git log OR git log --oneline

By using git log, you will be able to retrieve a list of all commits that you or somebody else have done. Within the list, look for the commit that you would like to reset back to. Remember, look for the one commit that you would like to be the last commit that stayed on that working branch. Once you have located that commit, copy the first 7 digits of the commit hash for that commit.

This is how the commit hash will look like when you use git log
This is how the commit hash will look like when you use git log --oneline

After you have got your desire 7 digits commit hash, do the following, but don’t include the <> (I suggest you read through this whole blog before executing the reset command, it might save your life. I have warned you! Don’t blame me if you get into big trouble later XP):

git reset <insert-commit-hash-here> # don't include the <>

Executing the above command will remove all those unwanted commits on the commit log. You can do git log again to check and make sure if every single one of the unwanted commits is gone. You can then create and switch to a new branch by the following command and continue on what you have been working on:

git switch -c <insert-branch-name-here> # don't include the <>

If you want all those unwanted changes that you made within the repository to be removed along with the commits, then you can do the following:

git reset --hard <insert-commit-hash-here> # don't include the <>

Warning! Make sure this is really what you wanted to do before you execute this command!! After it is executed, you can do git log to make sure all unwanted commits and changes have been removed and all files have been restored back to that specific commit.

If you realized this is not the commit that you want to come back to, I will post about what you can do to undo the git reset command in my next blog.

~~~A little break line right here~~~

The next command that I would like to talk about is git revert. Git revert is quite similar to git reset, they both undo changes, but they achieve that outcome differently. The git reset command moves the branch pointer backward and completely removes the commits. Those commits will be vanished and gone. The git revert command actually creates a new commit on top of your unwanted commit. And because it creates a new commit that undo your unwanted commits, the command line will prompt you to enter a commit message. Let me first walk you through the steps to revert your commits with git revert. It is very similar to git reset. First, you will run git log:

git log OR git log --oneline

then you will look for the specific commits that you would like to revert back to, and copy the first 7 digits of the commit hash. After that you will do:

git revert <insert-commit-hash-here> # don't include the <>

The command line will then prompt you to enter a commit message for the new reverting commit. The prompt will appear in your text editor. Enter your commit message at the very first line. For example, you can say something like “revert back to blah commit”. You can then close out that prompt window and the process will be completed and you can do git log to check your commit log to make sure everything looks fine.

So when and why should we consider using the git revert command? You can decide to use which command base on one condition: have your unwanted work been uploaded and been pull by other collaborators within the project? Or does your work exists in other people’s machines? If so, you should definitely use git revert. It is because if you deleted some work on your machine, but other collaborators don’t know about it, then you push your changes to Github, it will cause a huge disaster that will be a pain in the ass to restore. Since other people have those deleted work on their computer, if they also pushed their changes to Github, there might be serious conflicts and merge issues and nobody wants that to happen.

If you are working on your own project, or those unwanted commits haven’t been push to Github, or you are 100% sure that nobody in the team has pulled your unwanted commits to their machine, then you can definitely do git reset. Just make sure that you haven’t shared those works with others, use reset and no one will ever know!

Alright! That’s all for today’s blog. Hope you all enjoyed it and see you all next week. ⌒°(❛ᴗ❛)°⌒

Of couse I will always end my blog with a cat gif! Happy Monday! Weeeee!

--

--

Nana Lau

A real dummy who are trying to learn in a smart way