Skip to main content

Command Palette

Search for a command to run...

Ultimate Git Command : A Developer’s Reference

Updated
•6 min read•View as Markdown
Ultimate Git Command : A Developer’s Reference
C

Engineer Full Stack Developer with experience on platforms like website & Finio. I build scalable apps, craft APIs, optimize UIs, and contribute to open-source. Music fuels my code — I sing too! 🎵

Git is the cornerstone of modern version control, enabling developers to manage code changes, collaborate seamlessly, and maintain project integrity. Whether you're a beginner or a seasoned pro, having a handy Git command reference can significantly boost your productivity.

In this guide, we'll walk through the most essential Git commands, organized by functionality, to help you navigate your version control workflow with ease.

🔧 Setup & Initialization

Before diving into version control, configure Git and set up your project repository.

  • Set user information globally:

      git config --global user.name "Your Name"
      git config --global user.email "you@example.com"
    
  • Initialize a new repository:

      git init
    
  • Clone an existing repository:

      git clone [url]
    

📝 Stage & Snapshot

Manage your changes and prepare them for commit.

  • Check the status of your working directory:

      git status
    
  • Stage a file for commit:

      git add [file]
    
  • Unstage a file:

      git reset [file]
    
  • View changes not yet staged:

      git diff
    
  • View changes staged for the next commit:

      bashCopyEditgit diff --staged
    
  • Commit staged changes:

      git commit -m "Your commit message"
    

🌿 Branch & Merge

Work with branches to manage different lines of development.

  • List all branches:

      git branch
    
  • Create a new branch:

      git branch [branch-name]
    
  • Switch to a branch:

      git checkout [branch-name]
    
  • Create and switch to a new branch:

      git checkout -b [branch-name]
    
  • Merge a branch into the current branch:

      git merge [branch-name]
    

🔄 Synchronize

Keep your local repository in sync with remote repositories.

  • Add a remote repository:

      git remote add [alias] [url]
    
  • Fetch changes from a remote repository:

      git fetch [alias]
    
  • Merge changes from a remote branch:

      git merge [alias]/[branch]
    
  • Push changes to a remote repository:

      git push [alias] [branch]
    
  • Pull changes from a remote repository:

      git pull
    

🗂️ Track & Ignore

Manage files and prevent unwanted files from being tracked.Javatpoint

  • Remove a file from the repository:

      git rm [file]
    
  • Rename a file:

      git mv [old-name] [new-name]
    
  • List ignored files:

      git ls-files --ignored --exclude-standard
    
  • Create or edit a .gitignore file:

      touch .gitignore
    

Add patterns to .gitignore to exclude files from being tracked.


🧠 Inspect & Compare

Review changes and compare different versions of your project.

  • View commit history:

      git log
    
  • View commit history for a specific file:

      git log --follow [file]
    
  • Show changes between commits:

      git diff [commit1] [commit2]
    
  • Show changes in a commit:

      git show [commit]
    

🧳 Stash & Temporary Commits

Temporarily save changes that are not ready to be committed.

  • Save changes to a new stash:

      git stash
    
  • List all stashes:

      git stash list
    
  • Apply the most recent stash:

      git stash pop
    
  • Apply a specific stash:

      git stash apply [stash@{n}]
    
  • Drop a specific stash:

      git stash drop [stash@{n}]
    

🔁 Rewrite History

Modify commit history when necessary.

  • Reset to a previous commit (keeping changes):

      git reset [commit]
    
  • Reset to a previous commit (discarding changes):

      git reset --hard [commit]
    
  • Rebase current branch onto another branch:

      git rebase [branch]
    

If you have done with basic git commands than go through with advanced version control

🌳 Advanced Git: Mastering git worktree

When working on multiple branches simultaneously — say, reviewing a pull request while developing a new feature — switching branches repeatedly can disrupt your workflow or lead to unwanted merge conflicts. That's where git worktree becomes a powerful addition to your Git toolkit.

🔍 What is git worktree?

git worktree lets you create additional working directories attached to the same Git repository. Each directory has its own checked-out branch, allowing you to work on multiple branches in parallel — without switching context or using multiple clones.

This feature is especially useful when:

  • You want to test changes in one branch while working on another.

  • You're dealing with long-lived feature branches and frequent bugfixes or hotfixes.

  • You want a clean environment for testing without stashing or committing unready work.


📦 How It Works

Git stores your repository metadata in the .git directory. By default, this is tied to a single working directory. git worktree separates the Git data from the working directories, enabling multiple working trees to point to the same .git folder but check out different branches.


🔧 Common git worktree Commands

➕ Add a New Worktree

git worktree add ../my-feature-branch feature-branch
  • Creates a new directory (../my-feature-branch)

  • Checks out feature-branch in that directory

  • If feature-branch doesn’t exist yet, it will be created

📌 Pro Tip: Use relative paths to keep things organized in your project root.


📄 List Existing Worktrees

git worktree list

This command shows:

  • The path of each worktree

  • The branch checked out in each

  • Any status or HEAD info

Example output:

/project/main          abcd123 [main]
/project/feature-fix   efgh456 [feature-fix]

❌ Remove a Worktree

git worktree remove ../my-feature-branch

This:

  • Deletes the working directory (if it's safe to do so)

  • Removes the association from Git’s worktree list

💡 Note: This does not delete the branch — just the extra working directory.


🚀 Real-World Use Case Example

Let’s say you're working on the main branch and suddenly need to patch a bug in release-v1.0.

Instead of stashing your current work:

# While working on 'main'
git worktree add ../hotfix-v1 release-v1.0

cd ../hotfix-v1
# Make and commit your bugfix
git commit -am "Fix: Patch crash on load"

# Push it
git push origin release-v1.0

Meanwhile, your original main branch remains untouched in the first directory. No need to stash, switch, or risk conflicts.


⚠️ Caveats and Considerations

  • Detached HEAD Warnings: Don’t use git checkout inside a worktree for another branch. Use git worktree to manage all branch checkouts.

  • No Duplicate Branch Checkouts: A branch can only be checked out in one worktree at a time. If you try to check out a branch that's already in use, Git will block it.

  • Worktree paths must be separate: Avoid creating nested worktrees inside the parent repo folder.


🧠 Summary of Benefits

✅ Work on multiple branches at the same time
✅ Avoid losing uncommitted changes due to context switching
✅ Maintain cleaner, isolated environments for testing or deployment
✅ Faster than cloning the repo multiple times


Wrapping Up

Git is a powerful tool, and mastering it takes time. Whether you're committing your first change or juggling multiple branches using git worktree, this cheat sheet is designed to support you through every step.

Use it as your quick reference, integrate it into your daily workflow, and you'll soon feel at home with even the most advanced Git commands.

If you found this guide helpful, consider bookmarking it or sharing it with your team. Got questions or tips of your own? Drop them in the comments below!

“A developer without version control is like a chef without a recipe — things get messy fast.”