Essential Git Commands

Some Git Commands to help you move like a Pro


4 min read

One of the most frequently used commands in the Git toolkit is git <command> --help. It's your lifeline when you need quick assistance with a particular Git command. However, while it's a valuable resource, the sheer volume of information it can display can be overwhelming. Often, I find myself skimming through this sea of text without gathering enough insights to solve my specific problem.

The Significance of Commits

At the heart of Git lies the concept of commits. Commits are snapshots of your project at specific points in time. As you add or remove code from your project, you might want to preserve certain moments in time, typically after introducing a new feature or fixing a bug. Commits serve as reference points or milestones in your project's history. Visualize them as page numbers in your ledger (Think of Git as a ledger that meticulously records every addition and deletion to your code), with each page representing a distinct period of development.

Git is indeed a versatile tool. It's easy to use for straightforward use cases, but it truly shines when wielded by those with a deep understanding of its capabilities. Here are some git commands to help you use the tool like a Pro.

  1. Interactive Rebase

  2. Reflog

  3. Diff

  4. Cherry-Picking

1. Interactive Rebase

git rebase -i [head | branch]

Interactive rebase (git rebase -i) is a powerful tool that grants you precise control over your commit history. It allows you to reorganize, squash, split, reorder, and edit commits interactively. By rewriting your commit history, you can create a cleaner, more organized narrative of your project's development.

Interactive rebase is particularly useful when you want to:

  • Combine related commits into one.

  • Clean up your branch before submitting a pull request.

  • Rewrite commit messages for clarity.

As a side note, it's important to be aware that when you use git rebase, it alters the unique identifier, or hash, of a commit. Consequently, it's generally not recommended to use git rebase on commits that have already been pushed to a shared platform like GitHub. This caution is particularly crucial when working collaboratively, as team members may already be relying on the original commit's hash to reference and use that specific commit.

Read more

2. Reflog

Git's reference log, or reflog, is a hidden treasure that can save you from unintended disasters and help you recover lost commits or branches. It logs all updates to your Git references, such as branches and tags.

Here's why the reflog is invaluable:

  • Accidental Deletions: If you accidentally delete a branch, the reflog can help you find its commit ID and restore it.

  • Lost Commits: If you mistakenly reset your branch and lose commits, the reflog can help you find and restore them.

  • Branch Navigation: The reflog allows you to trace your recent branch checkouts, making it easy to return to a previous state.

Access the reflog with:

git reflog

This command provides a chronological list of reference updates, each with an associated commit ID you can use for recovery.

Read more

3. Diff

Understanding Git's diff command is essential for reviewing code changes and comprehending your repository's status. The diff command reveals the differences between different states of your project:

  • Changes between your working directory and the last commit (git diff).

  • Differences between two commits or branches (git diff commit1..commit2).

  • Changes within a specific file (git diff filename).

The git diff command provides a detailed view of code modifications, highlighting additions, deletions, and modifications. You can navigate through these changes, making it invaluable for code review and understanding code alterations.

Read more

4. Cherry-Picking

Cherry-picking (git cherry-pick) allows you to select and apply specific commits from one branch to another. It's an excellent tool when you want to introduce changes from one branch into another, even if the branches have diverged significantly.

With cherry-picking, you can:

  • Apply a single commit to your current branch.

  • Integrate bug fixes or features from another branch selectively.

  • Maintain granular control over the changes you merge.

Read more

By mastering these four essential Git commands—interactive rebase, reflog, diff, and cherry-picking—you'll have a solid foundation for navigating Git's vast landscape. These tools empower you to maintain a clean commit history, recover from accidents, understand code changes, and efficiently manage your software projects.

The motivation behind this article is to help you become more confident while using Git—confident enough not to fear running a command that could potentially erase all your code. Git is a powerful ally in your development journey, and with the right knowledge, you can harness its capabilities to streamline your workflow and collaborate effectively.