DevRoadmap
Tools

Git Commands Every Developer Must Know (With Examples)

Git is the most important tool in a developer's daily workflow. This guide covers every command you'll actually use — from first commit to resolving merge conflicts — with real examples.

READ TIME 8 min read
CATEGORY Tools
Advertisement

Initial Setup

Before using Git, configure your identity. This information appears in every commit you make and is visible in team repositories.

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"  # use VS Code as editor

The Core Daily Workflow

# Start a new project
git init                    # initialize Git in current directory
git clone              # download an existing repo

# Check what's happening
git status                  # see changed/staged/untracked files
git log --oneline           # compact commit history
git diff                    # see unstaged changes
git diff --staged           # see staged changes (what will be committed)

# Stage and commit
git add .                   # stage ALL changed files
git add src/components/     # stage a specific folder
git add -p                  # interactively stage parts of files (very useful)

git commit -m "feat: add user authentication"
git commit --amend          # fix last commit message (before pushing)

# Undo things
git restore           # discard unstaged changes to a file
git restore --staged  # unstage a file (keep changes in working dir)
git revert     # create a new commit that undoes a previous one

Branching and Merging

# Branches
git branch                  # list all local branches
git branch feature/login    # create a new branch
git checkout -b feature/login  # create AND switch (classic)
git switch -c feature/login    # same thing, newer syntax

git switch main             # switch to main branch
git merge feature/login     # merge feature into current branch

# Delete branches
git branch -d feature/login     # delete (only if merged)
git branch -D feature/login     # force delete (even if not merged)

# Working with remotes
git push origin feature/login   # push branch to GitHub
git push -u origin feature/login # push and set upstream (first time)
git pull                    # fetch + merge remote changes
git fetch                   # fetch remote changes without merging

Working with GitHub

# Connect local repo to GitHub
git remote add origin https://github.com/username/repo.git
git remote -v               # verify remote connections

# Common push/pull patterns
git push                    # push to tracked remote branch
git pull --rebase           # pull and rebase instead of merge (cleaner history)

# Stashing — save work temporarily without committing
git stash                   # stash all changes
git stash pop               # restore latest stash
git stash list              # see all stashes

# Tags — mark important commits like releases
git tag v1.0.0              # create a tag
git push origin v1.0.0      # push a tag to GitHub

Resolving Merge Conflicts

Merge conflicts happen when two branches change the same lines of code. Git marks the conflict in the file and you resolve it manually.

<<<<<<< HEAD
const greeting = "Hello World";     // your version
=======
const greeting = "Hi World";        // incoming version
>>>>>>> feature/greeting

To resolve: delete the conflict markers (<<<<<<<, =======, >>>>>>>) and keep the code you want (either version, or a combination). Then git add the resolved file and git commit to complete the merge.

Best PracticeWrite commit messages in the imperative mood: "Add login feature" not "Added login feature". Follow the pattern: type: short description where type is feat, fix, docs, style, refactor, or chore. This is the industry-standard Conventional Commits format.
Advertisement