DEV Community

Nima jahan bakhshian
Nima jahan bakhshian

Posted on

Practical Git Commands Every Developer Should Know

git commands

Git is the most widely used version control system in the world. Whether you’re working alone or collaborating with a team, knowing the right commands can save you hours of frustration and help you work more confidently.

Below is a practical collection of the most useful Git commands, organized by real-world workflows.


1. Getting Started

Creates a new Git repository in the current directory

git init
Enter fullscreen mode Exit fullscreen mode

Downloads an existing repository from a remote server and creates a local copy

git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode

Sets your identity so every commit is correctly attributed to you

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

2. Daily Workflow

Check the current status of your working directory

git status # shows which files are modified, staged, or untracked

git status -sb # the shorter and cleaner version

git add <file> # stage a specific file

git add . # stage all changes

# Show unstaged changes (working directory vs staging area)
git diff

# Show staged changes (staging area vs last commit)
git diff --staged

# Create a commit
git commit -m "feat: add image uploader"
Enter fullscreen mode Exit fullscreen mode

3. Working with Branches

Switching Branches with

git switch is the dedicated command for switching branches (safer and clearer than the old git checkout).

# Switch to an existing branch
git switch feature-branch

# Create a new branch and switch to it in one command
git switch -c new-feature

git branch # List all local branches
Enter fullscreen mode Exit fullscreen mode

4. Working with Remotes

Managing Remotes

# Show remote repositories with their fetch and push URLs
git remote -v

# List remote names only
git remote

# Remove a remote
git remote remove origin

# Add a new remote
git remote add origin <repository-url>

# Remove a remote
git remote remove <name>

# Fetch latest changes without merging
git fetch --all --prune
Enter fullscreen mode Exit fullscreen mode

5. Hunting Down Bugs

If a bug has appeared and you're not sure which commit introduced it, bisect uses binary search across your commit history

# Start a binary search to find which commit introduced a bug
git bisect start

# Mark the current commit as bad (contains the bug)
git bisect bad

# Mark a known good commit (for example version v1.0)
git bisect good v1.0

# used to end a Git bisection session, clean up the bisection state
# and return your working directory to the commit you were on before you started
git bisect reset
Enter fullscreen mode Exit fullscreen mode

6. Undoing Changes

Cleaning Up Untracked Files

# Dry-run: show which untracked files would be deleted (safe preview)
git clean -n

# Actually delete untracked files and directories
git clean -fd

# Discard changes in a specific file
git restore <file>

# Discard all local uncommitted changes (dangerous)
git restore .
git clean -fd

# Undo the last commit but keep the changes staged
git reset --soft HEAD~1

# Undo the last commit and unstage the changes
git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode

7. Temporarily Saving Work (Stash)

Always give a meaningful message with git stash push -m "...".

After a few days stash@{0}, stash@{1} become completely useless without messages.

# Temporarily save (stash) your uncommitted changes so you can work on something else
git stash

# Save your current uncommitted work (both staged + unstaged) with a clear message
# Very useful when you need to urgently switch branch or pull latest changes
git stash push -m "WIP: login form validation and dark mode toggle"

# Apply the most recent stash and remove it from the stash list
git stash pop

# List all saved stashes
git stash list

# Apply a specific stash (without removing it from the list)
git stash apply stash@{2}

# See what is inside a particular stash
git stash show -p stash@{1}

# Delete a specific stash after you're done with it
git stash drop stash@{1}

# Delete ALL stashes (use carefully)
git stash clear
Enter fullscreen mode Exit fullscreen mode

8. Attaching Notes to Commits

git notes lets you attach extra information to a commit, without changing the commit itself or its hash

git notes add -m "This commit caused a regression in v2.3" <commit-hash>
git log --show-notes
Enter fullscreen mode Exit fullscreen mode

9. A Clean Summary of Contributors

Instead of scrolling through a full commit log, shortlog gives you a ranked summary of who committed what, and how often a quick way to generate a contribution report without external tooling.

git shortlog
git shortlog -sn --all
git shortlog -sn --since="1 month ago"
Enter fullscreen mode Exit fullscreen mode

10. Inspect Any Single Commit in Detail

# Show the full details of a specific commit (message, author, date, and the full diff of changes)
git show <commit-hash>

# Show the content of a specific file as it existed in a particular commit
git show <commit-hash>:path/to/file
Enter fullscreen mode Exit fullscreen mode

Happy coding, and may your commits always be meaningful!

Top comments (0)