Git and GitHub: Backtracking

Git and GitHub: Backtracking

Learn how to backtrack in Git.

ยท

5 min read

Introduction

In the previous article, you learned how to take good care of your projects.

Now that you've got a well-formed repository, you can start backtracking in Git.

Backtracking can be helpful for many different reasons, like:

  • if you made an accidental commit and didn't realize it then

  • thought something was a good idea before, but changed your mind

  • or any other situations where you were in a better spot before ๐Ÿ˜…

In this article, I'll teach you how to backtrack in Git. You'll use Bakery for this, so please cd into that directory now.

NOTE #1: You might want to be at a computer or laptop (to get the full experience).

NOTE #2: Throughout the article, I'll use the --no-edit for reverting and resetting. I choose to have it there to avoid the commit message editor, instead allowing Git to use the default revert commit message of Revert "original commit message".

git revert

Let's start with git revert. Use this command to revert a given directory.

To go back to your HEAD commit (aka your most recent commit), it's as simple as this:

git revert HEAD --no-edit

To go back further than your most recent commit, there are two methods you can use.

The first method is to use the above code, adding ~x to the end. x is the commit number (aka how far back to go). For example, if you wanted to go back 3 commits, you'd use this code:

git revert HEAD~3 --no-edit

The second method is a bit more involved. First, figure out what the commit id of your desired commit is, and keep it somewhere for safekeeping.

GIT BASH JARGON: A commit id is the first 7 characters of your SHA-1, which looks something like this: 753e3d00db15b7ffd41de7902ba831813f8c5446. In this case, the first 7 characters would be 753e3d0.

NOTE: To figure out what commit you want to go back to, use git log to list your previous commits. This is where it pays off to have frequent and descriptive commits ๐Ÿ˜‰)

Once that's figured out, use the following to revert to your desired commit:

git revert <commit-id> --no-edit

Mini-Test: Use what you learned above (and previously) to update Bakery! Try making a faux commit (with random additions/deletions), then reverting it.

git reset

Next, let's learn git reset. You can use this command to unstage (aka remove a file from the staging area) a given file or reset a given directory.

It's very similar to git revert, with a couple big differences (which I'll explain soon).

To unstage a given file with git reset, try this:

git reset HEAD <filename>

To go back by one or more commits, you only have the option to use your commit id (as explained earlier) to reset your changes.

After you find your desired commit's id, reset using this:

git reset <mode-option> <commit-id>

NOTE: For help filling out <mode-option>, check out this resource.

Mini-Test: Use what you learned above (and previously) to update Bakery! Try making a faux commit (with random additions/deletions), then resetting it back.

git revert vs. git reset

You may be wondering: What's the difference between git revert and git reset? Why do we use both?

Let's use Bakery to answer the first question (and hopefully the second along the way).

As always, use pwd to make sure that you're in Bakery first!

Now, make random changes and execute the two-stage commit process in Bakery a couple times. Don't worry about messing it up - you can revert (or reset ๐Ÿ˜„) later.

Let's say that you didn't really want to update Bakery with your changes, but you still wanted to keep a record of them for the future (for some reason or another).

For that, you'd use git revert. Do that now, if you'd like, using what you learned earlier about reverting commits.

Let's instead say that you didn't want to update Bakery with those random changes, but you also didn't want to keep track of them in the future (like they never happened ๐Ÿคซ).

In that case, you'd use git reset. Do that now, if you'd like, using what you learned earlier about resetting commits.

TL;DR: Check out steps 1 and 2 of these w3schools articles: Git Revert and Git Reset

git commit --amend

The last command to note is git commit --amend.

NOTE: For clarity, this knowledge should only be applied to local projects, not remote repositories or projects in a team.

To amend your most recent commit, simply enter this into your terminal:

git commit --amend -m "New commit message"

The output is similar to using HEAD, but for commits instead of reverting or resetting.

To amend a commit further back, check out this Devconnected article.

Mini-Test: Use what you learned above (and previously) to update Bakery! Try making a previous commit more descriptive with git commit --amend.

Conclusion

In this article, you learned some commands to help you backtrack in Git, including:

- git revert (used to revert a given directory to a previous commit)
- git reset (used to unstage a given file or reset a given directory to a previous commit)
- git commit --amend (used to change a previous commit message)

Great job on completing the seventh part of the series!

This is the 7th part of a 10 part series to teach Windows users about Git Bash. This series includes:

  • how to install Git Bash

  • how to navigate your file system

  • how to change your file system

  • how to set up Git and GitHub

  • how to connect Git and GitHub

  • how to keep up good practices in Git

  • how to branch in Git

  • ...and more!!

Stay tuned for more - a new part will come out every Tuesday! ๐Ÿ˜Š

ย