Date:

10 Git Commands You’ll Wish You Knew Earlier

10 Git Commands to Save You Time and Frustration

1. git reflog – Recover Lost Commits

Sometimes, you accidentally delete a branch or reset to the wrong commit. git reflog lets you go back in time and recover lost work.

What it does: Tracks every change made in the repository, even those you thought were gone.

When to use:

  • You accidentally deleted a branch.
  • You need to recover a commit after a bad reset.

Command:

git reflog

2. git cherry-pick – Apply Specific Commits

You need a commit from another branch but don’t want to merge everything. git cherry-pick brings in only the commit you need.

What it does: Lets you apply a specific commit from one branch to another.

When to use:

  • You need a bug fix from a different branch without merging unnecessary changes.

Command:

git cherry-pick 

3. git bisect – Find the Problematic Commit

When a bug appears but you don’t know which commit introduced it, git bisect helps you track it down efficiently.

What it does: Uses binary search to identify the commit that caused an issue.

When to use:

  • You need to find which commit introduced a bug.

Command:

git bisect start
git bisect bad  # Mark the current commit as bad
git bisect good   # Mark a known good commit

4. git stash pop – Switch Tasks Without Losing Work

You’re in the middle of coding, and suddenly, you need to switch branches. git stash pop lets you do that without losing progress.

What it does: Saves your uncommitted changes so you can apply them later.

When to use:

  • You need to switch branches without losing your work.

Command:

git stash pop

5. git log – Understand Branches and Merges

You need to understand how branches diverged and merged. git log shows you the commit history.

What it does: Displays the commit history of your repository.

When to use:

  • You need to understand how branches diverged and merged.

Command:

git log --oneline --graph --all

6. git reset – Reset Your Branch

You need to reset your branch to a specific commit. git reset does just that.

What it does: Moves your branch to a specific commit.

When to use:

  • You need to reset your branch to a specific commit.

Command:

git reset 

7. git checkout – Switch Branches

You need to switch branches. git checkout lets you do that.

What it does: Switches your branch to a different one.

When to use:

  • You need to switch branches.

Command:

git checkout 

8. git clean -f – Remove Untracked Files

Your working directory is cluttered with untracked files. git clean -f helps clean it up.

What it does: Deletes untracked files from your working directory.

When to use:

  • You have untracked files interfering with your work.

Command:

git clean -f

9. git rebase -i – Edit Your Commit History

You need to clean up your commits before merging. git rebase -i lets you squash, edit, or delete commits.

What it does: Allows you to modify commit history interactively.

When to use:

  • You need to clean up your commit history before merging.

Command:

git rebase -i HEAD~

10. git diff --staged – Review Staged Changes

You want to verify staged changes before committing. git diff --staged shows you exactly that.

What it does: Displays changes between the staging area and the last commit.

When to use:

  • You want to verify staged changes before committing.

Command:

git diff --staged

Conclusion

These 10 Git commands can save you time and frustration by providing a range of useful functionality for managing your Git repository. Whether you need to recover lost commits, apply specific commits, or edit your commit history, there’s a Git command for you.

FAQs

Q: What is the purpose of git reflog?

A: git reflog is used to recover lost commits and track every change made in the repository, even those you thought were gone.

Q: How does git cherry-pick work?

A: git cherry-pick applies a specific commit from one branch to another, allowing you to bring in a bug fix or feature without merging unnecessary changes.

Q: What is the purpose of git bisect?

A: git bisect uses binary search to identify the commit that introduced a bug, helping you track down the problematic commit efficiently.

Latest stories

Read More

LEAVE A REPLY

Please enter your comment!
Please enter your name here