Skip to main content
  1. Posts/

Git Commands (that not every developer knows)

·4 mins

Git is an essential tool for modern developers, but many only scratch the surface of its true power. Here are some less commonly known but highly useful Git commands that can elevate your Git-fu!


1. git stash with options #

Many developers use git stash to temporarily save their work. But did you know you can specify changes or even stash untracked files?

# Stash tracked and untracked files:
git stash -u

# Stash only certain files or directories:
git stash push -m "WIP on feature X" path/to/file

# Apply the last stash without removing it:
git stash apply

This allows you to stash more flexibly and recover work in progress quickly.


2. git reflog to Recover Lost Commits #

Ever lost a branch or commit after an unintended reset or rebase? git reflog tracks changes to the HEAD, allowing you to recover lost commits.

# List all actions on the repository’s history:
git reflog

# Recover a commit that was lost:
git reset --hard <commit_hash_from_reflog>

Even if a commit was “deleted”, git reflog can be a lifesaver.


3. git bisect for Debugging #

When tracking down the commit that introduced a bug, git bisect automates the process by doing a binary search.

# Start the bisect process:
git bisect start

# Tell Git the current commit is bad:
git bisect bad

# Tell Git a known good commit:
git bisect good <commit_hash>

# Git will checkout a middle commit, then mark it as good or bad until the buggy commit is found.

This command saves tons of time when working with large repositories and long histories.


4. git worktree for Multiple Working Directories #

Need to work on multiple branches simultaneously? Instead of switching branches, git worktree allows you to check out a branch to a separate directory.

# Create a new worktree for a feature branch:
git worktree add ../path/to/new-dir feature-branch

# Remove a worktree when you're done:
git worktree remove ../path/to/new-dir

This allows you to work in parallel on multiple branches without needing to switch contexts.


5. git cherry-pick with ranges and sequences #

Most developers know about git cherry-pick for copying individual commits, but it can also be used with ranges!

# Cherry-pick a range of commits:
git cherry-pick <start_commit>^..<end_commit>

# Skip a failing commit during cherry-pick:
git cherry-pick --skip

This is useful when you need to port multiple features or fixes across branches.


6. git rerere (Reuse Recorded Resolution) #

Tired of resolving the same conflicts repeatedly during rebases or merges? Enable git rerere to remember your conflict resolutions.

# Enable rerere:
git config --global rerere.enabled true

# Git will automatically reuse conflict resolutions when possible.

This is especially helpful in long-running feature branches or complex merges.


Bonus 1: Workflows #

Git workflows define how you structure your development cycle. Here are a couple of advanced yet highly productive workflows.

1. Gitflow Workflow #

The Gitflow Workflow is a robust and feature-branch-driven approach. It uses two main branches for production and develop (master and develop) and other branches for features, releases, and hotfixes.

  • master: Always contains the production-ready code.
  • develop: The active development branch where features are merged.
  • feature branches: Created from develop, and once completed, merged back.
  • release branches: Created when preparing for a release and merged into both develop and master.
  • hotfix branches: Used for fixing bugs in production.
# Create a feature branch:
git checkout -b feature/awesome-feature develop

# Merge a completed feature back into develop:
git checkout develop
git merge --no-ff feature/awesome-feature

Gitflow ensures that your production and development branches remain stable. Also you can save some time using the tool git-flow

2. Forking Workflow #

This is commonly used in open-source projects where collaboration is key. The idea is to fork the main repository, make changes in your copy, and then submit a pull request for review.

  • Fork the main repo: You create your own copy on GitHub.
  • Clone the forked repo: Work on features independently.
  • Submit pull requests: Once done, create a pull request to the original repo.
# Clone your fork:
git clone git@github.com:your-username/project.git

# Add the original repository as an upstream:
git remote add upstream https://github.com/original-username/project.git

# Sync your fork with upstream changes:
git fetch upstream
git merge upstream/master

This is excellent for distributed collaboration and maintaining isolation between individual work and the main project.


Bonus 2: Tips #

  • Avoid “git reset” confusion: Use git reset --soft to keep changes staged, or git reset --hard to discard all changes.
  • git clean: Clean untracked files and directories:
    git clean -fd
    
  • git blame: Annotates who modified each line of a file:
    git blame <file>
    

Mastering these Git commands and workflows will greatly enhance your development speed, reduce errors, and improve collaboration!