Rewinding Time in Git: Exploring Commits & Branching

Rewinding Time in Git: Exploring Commits & Branching

posted Originally published at medium.com 2 min read

In the last story, we created a new repository on GitHub and pushed our code to the main branch. In this one, let’s rewind a bit, I'll walk you through how to look back at your development history and test new features safely using Git.

By now, you’ve probably updated your project and pushed changes quite a few times. Every commit you've made is like a snapshot, a version of your project frozen in time.

But how do you go back and look at an old version, maybe just to check a single line of code?

Most people head to GitHub -> Commits tab. But you don’t need to leave the terminal for that. Here's how you can do it right from the command line.

See Your Project’s History

git log

This shows a list of commits made by you and your collaborators. Each commit is identified by a unique 40-character SHA-1 hash. This hash acts like a version ID.

A sample log entry looks like:

commit 7abd5964ad8ef67ac20f5c52eea34e0d3bebe110
Author: hiruthicShaSS <*Emails are not allowed*>
Date: Fri Jun 25 16:42:07 2021 +0530

    Opening articles in new tab and card title fixed

That’s a lot of info! Let’s trim it down.

Get Specific Commits

Want just the last few entries?

git log -3

Want logs after a specific date?

git log --after="2021-05-01"

Combine with --before to get a date range!

Want Less Detail? Use --oneline

git log --oneline

This condenses each commit into a single line:

7abd596 Opening articles in new tab and card title fixed

To include branch and tag info too:

git log --oneline --decorate

Or if you're just looking for a human-friendly summary:

git shortlog

Enter Branches

Every Git project starts with the default main branch. But you shouldn’t develop or test directly on it. That’s what branches are for.

Check your current branches:

git branch

Let’s create a new one for development:

git branch development

But wait, this doesn’t switch to the new branch yet. To move to it:

git checkout development

Or do both in one shot:

git checkout -b development

Honestly, this is what I always use .

Why Branching Matters

Each branch is an isolated version of your code. You can try out features, break stuff, go wild, and it won't affect your main branch. When you're done and happy with your changes, you can merge it back.

If it all goes south? Just delete the branch and start over from main (just kidding, we'll talk how to fix f*ckups in git later).

Working with Git branches is like owning the Eye of Agamotto, every time you mess up, you can just rewind time and try again. Cool, right?

Until then:

Learn Git. Get fit.

Stay Curious. Adios ✌️

If you read this far, tweet to the author to show them you care. Tweet a Thanks

Really good read! The way you break things down makes it much easier to follow.

More Posts

Mastering Collaborative Coding: A Deep Dive into Git Branching and Merging for Teams

Aditya Pratap Bhuyan - Jun 24

Understanding the basics of GIT

Hiruthic Sha - Jun 25

Git and GitHub for Python Developers A Comprehensive Guide

Tejas Vaij - Apr 7, 2024

Introducing Git In A New Light

ByteHackr - Jan 1

Git Under the Hood

István Döbrentei - Jun 28
chevron_left