Useful Git Commands to Know

4 min read
Cover

Recently Used Git Commands

Here's a collection of commands I use occasionally but can't always remember off the top of my head. I compiled them for my own reference and to share with other engineers.

1. Use --force-with-lease Instead of --force

Many people use git push --force when pushing changes to a specific branch. However, this approach risks accidentally overwriting someone else's work.

Let me illustrate with an example:

  • Developer A discovers an urgent issue on the main branch and starts working on a fix.
  • A finishes the fix and tries to update the local main branch, but in the meantime, developer B has merged new work into the main branch.
  • A, unaware of this, uses git push --force to push their fix.

In this case, B's work disappears due to A's push. Since there's no warning message, A finishes their work without realizing the problem.

To prevent this issue, use git push --force-with-lease instead of git push --force. force-with-lease only allows the push when your local branch is in sync with the remote branch, protecting other people's work.

The same risk exists when using -f on personal branches. That's why I've developed a habit of never using --force even in my personal work, and whenever I see a colleague using --force, I recommend using --force-with-lease in all situations.

References: https://www.atlassian.com/blog/it-teams/force-with-lease

2. Tracking Down Issues with git bisect

Pinpointing exactly when a bug was introduced isn't easy, especially when there are many commits stacked up. That's when git bisect comes in handy.

git bisect uses binary search algorithm to quickly find when a specific bug was introduced. It helps you effectively track down the commit that caused the problem.

How to use git bisect

  1. git bisect start: First, start a bisect session with git bisect start. This tells Git that you're entering bisect mode.

  2. Specify good and bad commits: Mark the last known working commit as good and the commit where the bug was first discovered as bad.

git bisect good <good_commit>
git bisect bad <bad_commit>
  1. Perform binary search: Git performs a binary search between the two specified commits and moves to the midpoint commit. Build and test the code at this state to check for the bug.

  2. Input results: Based on test results, enter git bisect good if the commit has no issues, or git bisect bad if it does. Repeating this process, Git gradually narrows down the range to find the problematic commit.

  3. git bisect reset: Once you've found the problem commit, end the bisect session with git bisect reset and return to your original working branch.

In real development, git bisect is useful in situations like:

  • When a bug appeared after a certain point, but you don't know which commit caused it
  • When you need to quickly find the root cause in an urgent situation

This process helps you accurately identify the cause and fix it. It's especially helpful when working in teams, as you can quickly identify the source of bugs.

3. When gitignore Isn't Working

Sometimes gitignore doesn't apply even when it's correctly configured.

git rm -r --cached .

This usually happens due to cache file issues, and the command above can resolve it.

4. Viewing Branches by Recent Activity

When working with about 2 branches, you can easily switch back and forth to the previous branch using git checkout -.

But when working with 3 or more branches simultaneously, sometimes you can't remember the branch names. In such cases, use the following command to see branch names in order of recent activity along with their dates.

git for-each-ref --sort=-committerdate --format='%(committerdate:short) %(refname:short)' refs/heads/
2024-09-09 feature/F
2024-09-06 feature/E
2024-09-05 feature/D
2024-09-05 bugfix/C
2024-08-30 main
2024-08-28 bugfix/B
2024-08-27 feautre/A