DEV Community

OmarElsamahy
OmarElsamahy

Posted on

Navigating Git History: A Guide to git log, git checkout and git switch

As developers, we've all been there – navigating the vast sea of Git commits, feeling lost in a complex maze. It can be a daunting task, trying to make sense of our version history and finding our way out of a seemingly endless commit.

But fear not, for in these moments lies the importance of mastering Git commands that not only provide us with insights but also serve as a lifeline when things go awry. In this journey through the capabilities of Git, we'll dive into the invaluable git log, git checkout and git switch commands.

They are not simply lines of code, but rather powerful allies. Have you ever wanted to revisit a specific commit, desperate to undo a merge gone wrong or about creating a new branch tied to an older commit? Worry not, as these commands will become guide you in achieving these tasks.

In this guide, we'll showcase their powers how they can be your escape hatch when working with Git.

Basic Usage:
git log
Output:

commit abc123
Author: John Doe <john.doe@example.com>
Date:   2022-01-27

    Updated README.md

commit def456
Author: Jane Smith <jane.smith@example.com>
Date:   2022-01-26

    Added new feature
Enter fullscreen mode Exit fullscreen mode

That is the default git log usage that shows the commit SHA, author, date and the commit message

we can also have different args for that command as:
git log --pretty=format:"%h %ad | %s%d [%an]" --date=short
This command helps us format the output in a different presentation like this

commit abc123 | 2022-01-27 | Updated README.md [John Doe]
commit def456 | 2022-01-26 | Added new feature [Jane Smith]
Enter fullscreen mode Exit fullscreen mode

We could also search for a specific commit with its author, message or date.

git log --author="John Doe"
git log --grep="bug fix"
git log --since="2024-01-01" --until="2024-01-21"
Enter fullscreen mode Exit fullscreen mode

Now for git checkout

The git checkout command is versatile and is used for various purposes, including switching branches, creating new branches, and navigating through commits.

  • When used with a branch name, it switches your working directory to that branch.
  • When used with a commit SHA, it puts you in a "detached HEAD" state, allowing you to explore the commit without being on a specific branch.

Switch to a Specific branch:

git checkout main
git checkout -b new-feature-branch
Enter fullscreen mode Exit fullscreen mode

Switch to a Specific Commit (Detached HEAD State):
git checkout abc123

Now for git switch

git switch branch-name

This command structure is for switching between existing branches like git checkout.

git switch -c new-local-branch origin/remote-branch

This command is used to establish a new local branch that is rooted in a remote tracking branch.

git switch -c new-branch
Enter fullscreen mode Exit fullscreen mode

The -c option for create makes the process more concise and aligns with the common use case of creating and switching branches.

git log
git checkout commit-SHA
git switch -c new-branch-name
Enter fullscreen mode Exit fullscreen mode

After git log you select the commit you want to branch from you should be able to git switch and now you have created a new local branch of it and able to make all required changes

don't forget to push it after committing your changes to your repo and setting your upstream

git push --set-upstream origin new-branch-name

This streamlined flow aids in making parallel changes for a root branch and also allows us to explore historical commits and branch off of them.

Top comments (0)