Git Commands
Setup
git config --global user.name "Your Name"
Set your name for all commits globally.
git config --global user.email "you@example.com"
Set your email for all commits globally.
git init
Initialize a new Git repository in the current directory.
git clone <url>
Clone a remote repository locally.
Basics
git status
Show the state of the working directory and staging area.
git add <file>
Stage a specific file for the next commit.
git add .
Stage all changed files in the current directory.
git commit -m "message"
Commit staged changes with a message.
git commit -m "message"
Commit staged changes with a message.
git commit --amend -m "New commit message"
Change the most recent commit message only.
git add <filename>
git commit --amend --no-edit
Add forgotten files to the last commit (no edit will keep the last commit).
Branching
git branch
List all local branches.
git branch <name>
Create a new branch.
git checkout <branch>
Switch to an existing branch.
git checkout -b <name>
Create and switch to a new branch in one step.
git merge <branch>
Merge the specified branch into the current branch.
git branch -d <name>
Delete a branch (only if fully merged).
git branch -D <name>
Force delete a branch regardless of merge status.
Merging a Branch Step by Step
The general pattern is that you want to merge branch-a into branch-b (branch-b is the one that receives the changes).
Step 1 — Switch to the destination branch (branch-b) and make sure it's up to date.
git checkout branch-b
git pull origin branch-b
Step 2 — Merge branch-a into it.
git merge branch-a
Step 3 — Push the updated branch-b.
git push origin branch-b
For example, merging a feature branch into main:
git checkout main
git pull origin main
git merge feature/my-feature
git push origin main
Resolving Merge Conflicts
A merge conflict happens when two branches both changed the same part of the same file, and Git doesn't know which version to keep.
Step 1 — When the conflict happens, Git will stop and tell you. Run this to see which files are affected.
git status
You'll see something like:
both modified: app.py
Step 2 — Open the conflicting file. Git inserts conflict markers directly into the code to show you both versions side by side.
<<<<<<< HEAD
print("Hello from main")
=======
print("Hello from feature-branch")
>>>>>>> feature-branch
Breaking this down:
<<<<<<< HEAD — everything below this line (until the =======) is what's on your current branch.
======= — the divider between the two versions.
>>>>>>> feature-branch — everything above this line (after the =======) is what's coming in from the branch you're merging.
Step 3 — Edit the file to the version you want. You can keep one side, keep both, or write something entirely new. Just make sure to delete all the conflict markers (<<<<<<<, =======, >>>>>>>). For example, if you want to keep both lines:
print("Hello from main")
print("Hello from feature-branch")
Or if you only want the feature branch version:
print("Hello from feature-branch")
Step 4 — Once you've edited the file, stage it to tell Git the conflict is resolved.
git add app.py
Step 5 — Complete the merge with a commit.
git commit
If things get too messy and you want to start over, abort the merge entirely. This puts everything back to before you ran git merge.
git merge --abort
Undoing Changes
git restore <file>
Discard unstaged changes in a file.
git restore --staged <file>
Unstage a file without discarding changes.
git reset HEAD~1
Undo the last commit, keeping changes unstaged.
git reset --hard HEAD~1
Undo the last commit and discard all changes. Irreversible.
git revert <commit>
Create a new commit that undoes a previous one. Safe for shared branches.
Remote
git remote -v
List remote connections with their URLs.
git remote add origin <url>
Add a remote named origin.
git push origin <branch>
Push a branch to the remote.
git push -u origin <branch>
Push and set the upstream so future pushes just need git push.
git pull
Fetch and merge changes from the remote into the current branch.
git fetch
Download changes from the remote without merging.
Inspection
git log
Show the full commit history.
git log --oneline
Compact commit history — one line per commit.
git log --oneline --graph --all
Visual branch graph of the entire history.
git diff
Show unstaged changes.
git diff --staged
Show changes that are staged but not yet committed.
git blame <file>
Show who last modified each line of a file.
Stashing
git stash
Temporarily save uncommitted changes and clean the working directory.
git stash pop
Re-apply the most recent stash and remove it from the stash list.
git stash list
Show all saved stashes.
git stash drop
Delete the most recent stash without applying it.