How to not f- up your local files with Git part 3

Rewriting history and how to handle mistakes

Scope of this article

Following the first two articles I received a number of questions regarding details about Git history, some less common Git commands, and the reasoning behind certain aspects of Git.

Tools

Just as we did in the previous articles we’ll use the same old Git client and Visual Studio Code for the awesome Git integration.

Introduction — The logic behind Git

I wanted to give a little more detail on Git and its utility. First of all, like most you may have approached Git as a tool to save your projects so you do not lose your work. This is a good reason to start using Git but there is more to it than that as you might have already noticed, Git’s strength is the ability to document your projects history, using it only to save is like having a plane and just using the wheels to go around.


Chapter I — Atomic commits

How can we use Git to it’s full potential then? I talked about doing scoped commit but I did not fully explain the reasoning behind it.

Yes, atomic commit is a thing, it even has a Wikipedia article about it.
  • Add 50 grams of butter
  • Cook at medium heat until butter melts
  • Add some milk
  • Pour a glass of wine
  • Drink glass of wine
How VSCode displays changes under the “Git” tab
  • Add new multiply action
Highlight, open three dots menu, stage selected ranges.

Chapter II — Changing Git history

And usually its a bad thing.
# This is the default way to merge
# fetch grabs the changes, merge applies them
$ git fetch
$ git merge
# Can be written as:
$ git pull
# This is the default way to rebase
# fetch grabs the changes, rebase applies them
$ git fetch
$ git rebase
# Can be written as:
$ git pull --rebase
Commit history on terminal using git log.
$ git revert commitHash
Output of git log showing the result of git revert of the previous commit.
$ git rebase -i HEAD~2
Result of the command above, simply delete the lines containing the commits to permanently remove them.
$ git reset commitHash

Conclusion or TLDR

Your project history is the biggest tool at your disposal when using Git, you can interact with it in a number of ways, get comfortable checking it with git log, to have something meaningful to look at write good atomic commits, easy to read and easy to modify in case such need arise.
Revert wherever you want as it reserves history, rebase only in case of sensitive info leak, reset to write better commits.

Chingu

Join our friendly community's mission to create a collaboration & opportunity hub for tech-learners! Developers | Designers | Data Scientists

Thanks to Jim Medlock and Chance Taken.

Francesco Agnoletto

Written by

Javascript developer @streamroot

Chingu

Chingu

Join our friendly community's mission to create a collaboration & opportunity hub for tech-learners! Developers | Designers | Data Scientists