Commands & workflows
Beginner-safe default workflow (use ~90% of the time)
Check status, branch from updated main, commit small clear changes, push, then open a pull request. Learn PRs on GitHub →
Three zones
Working directory → Staging area → Commit history
edit git add git commit
Local ↔ remote
Local repo ↔ Remote repo git pull git push git fetch
Branch flow
main \ feature-branch Typical rhythm: git switch main → git pull → git switch -c feature/… → edit → git add . → git commit → git push -u origin feature/… → open pull request
1. Setup and identity
Tell Git who you are and set a few defaults before your first commit.
git config --global user.name "Your Name"
In plain English: Sets the name that appears on your commits, like signing your work.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
In plain English: Sets the email tied to your commits. Use the same email as your GitHub account when possible.
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
In plain English: Makes new repositories start with a branch named main instead of master.
git config --global init.defaultBranch main
git config --global pull.rebase false
In plain English: When you pull, Git merges by default instead of rebasing. A simple default for beginners.
git config --global pull.rebase false
git config --global core.editor "code --wait"
In plain English: Opens VS Code when Git needs you to write a longer message (for example during a rebase).
git config --global core.editor "code --wait"
git config --list
In plain English: Shows every Git setting currently in effect.
git config --list
git config user.name
In plain English: Shows just your configured user name.
git config user.name
git config user.email
In plain English: Shows just your configured email.
git config user.email
2. Starting a repository
Create a new project folder under Git, or copy an existing project from GitHub.
Create a new local repo
In plain English: Make a folder, step into it, and turn it into a Git project.
mkdir my-project cd my-project git init
git clone https://github.com/user/repo.git
In plain English: Downloads a copy of a remote repository to your computer.
git clone https://github.com/user/repo.git
Clone into a custom folder
In plain English: Same as clone, but puts the files in a folder name you choose.
git clone https://github.com/user/repo.git my-folder cd my-folder
git clone -b branch-name https://github.com/user/repo.git
In plain English: Clones only a specific branch instead of the default one.
git clone -b branch-name https://github.com/user/repo.git
3. Checking status and history
See what changed and browse past snapshots of your project.
git status
In plain English: Answers: which files changed, which are staged, and which branch you are on.
git status
git status -s
In plain English: A shorter, one-line-per-file status view.
git status -s
git log
In plain English: Lists past commits with messages, authors, and dates.
git log
git log --oneline
In plain English: Compact history — one short line per commit.
git log --oneline
git log --oneline --graph --all --decorate
In plain English: Shows branches and merges as a text diagram — great for understanding structure.
git log --oneline --graph --all --decorate
git log -5 --oneline
In plain English: Shows only the five most recent commits.
git log -5 --oneline
git show <commit-hash>
In plain English: Opens one specific commit and the exact changes it introduced.
git show <commit-hash>
4. Staging and committing
Choose what goes into the next snapshot, then save that snapshot with a message.
git add file.txt
In plain English: Stages one file — marks it ready for the next commit.
git add file.txt
git add .
In plain English: Stages every changed file in the current folder and subfolders.
git add .
git add -u
In plain English: Stages only files Git already tracks — skips brand-new untracked files.
git add -u
git add -p
In plain English: Walks you through changes hunk by hunk so you can stage only part of a file.
git add -p
git commit -m "Describe the change"
In plain English: Saves staged changes as a permanent snapshot with a short message.
git commit -m "Describe the change"
git commit -am "Describe the change"
In plain English: Stages all tracked modified files and commits in one step. Does not include new untracked files.
git commit -am "Describe the change"
Note: commit -am does not include new untracked files.
git commit --amendcareful
In plain English: Rewrites your most recent commit — useful right before you push.
git commit --amend
git commit --amend -m "New message"careful
In plain English: Changes only the message on your last commit.
git commit --amend -m "New message"
Add a forgotten file to last commitcareful
In plain English: Stage the missing file and fold it into the previous commit without changing the message.
git add forgotten-file.txt git commit --amend --no-edit
5. Comparing changes
See exactly what lines changed before you commit or merge.
git diff
In plain English: Shows unstaged edits — what you changed but have not added yet.
git diff
git diff --staged
In plain English: Shows what is staged and ready to commit. Same as --cached.
git diff --staged
git diff --cached
In plain English: Alternate flag for staged changes.
git diff --cached
git diff main..feature-branch
In plain English: Compares two branches to see what would change if you merged.
git diff main..feature-branch
git diff <commit1> <commit2>
In plain English: Compares any two points in history.
git diff <commit1> <commit2>
git diff --name-only
In plain English: Lists only file names that differ — no line-by-line detail.
git diff --name-only
6. Branches
Work on a separate line of development without disturbing main.
git branch
In plain English: Lists local branches. The current one is marked with an asterisk.
git branch
git branch -a
In plain English: Lists local and remote branches.
git branch -a
git branch feature-name
In plain English: Creates a new branch but does not switch to it yet.
git branch feature-name
git switch feature-name
In plain English: Moves you to another branch. Modern replacement for checkout.
git switch feature-name
git checkout feature-name
In plain English: Older syntax — still common — to switch branches.
git checkout feature-name
git switch -c feature-name
In plain English: Creates a branch and switches to it in one step.
git switch -c feature-name
git checkout -b feature-name
In plain English: Older syntax for create-and-switch.
git checkout -b feature-name
git branch -m new-branch-name
In plain English: Renames the branch you are currently on.
git branch -m new-branch-name
git branch -d branch-name
In plain English: Deletes a merged local branch safely.
git branch -d branch-name
git branch -D branch-namecareful
In plain English: Force-deletes a branch even if it is not fully merged.
git branch -D branch-name
7. Working with remotes
Connect your local repo to GitHub, GitLab, or another hosted copy.
git remote -v
In plain English: Shows remote names and their URLs.
git remote -v
git remote add origin https://github.com/user/repo.git
In plain English: Links your local repo to a remote named origin.
git remote add origin https://github.com/user/repo.git
git remote set-url origin https://github.com/user/repo.git
In plain English: Changes where origin points — for example after moving a repo.
git remote set-url origin https://github.com/user/repo.git
git remote remove origin
In plain English: Removes the origin remote connection.
git remote remove origin
git fetch
In plain English: Downloads new commits from the remote without changing your files yet.
git fetch
git fetch --all
In plain English: Fetches from every configured remote.
git fetch --all
git fetch --prune
In plain English: Removes stale remote-tracking branches that were deleted on the server.
git fetch --prune
8. Push and pull
Send your commits upstream or bring teammates' work down to your machine.
git push
In plain English: Uploads your current branch commits to the matching remote branch.
git push
git push -u origin feature-name
In plain English: First push of a new branch — also sets upstream so later pushes can be just git push.
git push -u origin feature-name
git push origin main
In plain English: Pushes your main branch explicitly to origin.
git push origin main
git pull
In plain English: Fetches and merges remote changes into your current branch. Same as fetch + merge.
git pull
Pull = fetch + merge
In plain English: What pull does under the hood.
git fetch git merge
git pull --rebasecareful
In plain English: Replays your local commits on top of remote changes for a straighter history.
git pull --rebase
Pull with rebase = fetch + rebase
In plain English: Equivalent steps when you pass --rebase.
git fetch git rebase origin/main
git push --force-with-leasecareful
In plain English: Overwrites the remote branch only if nobody else pushed since you last fetched — safer than --force.
git push --force-with-lease
git push --forcecareful
In plain English: Overwrites remote history regardless. Can destroy teammates' work.
git push --force
Prefer --force-with-lease instead.
9. Merging
Combine another branch into the one you are on.
Merge feature into main
In plain English: Classic flow: update main, merge your feature, push.
git switch main git pull git merge feature-name git push
git merge branch-name
In plain English: Brings another branch's commits into your current branch.
git merge branch-name
git merge --abort
In plain English: Cancels a conflicted merge and returns to the pre-merge state.
git merge --abort
git merge --no-ff feature-name
In plain English: Always creates a merge commit even when a fast-forward is possible — preserves branch history.
git merge --no-ff feature-name
10. Rebasing
Replay your commits on top of another branch for a linear history. Rewrites commits — use with care.
Rebase feature onto maincareful
In plain English: Move your branch to start from the latest main.
git switch feature-name git fetch origin git rebase origin/main
Continue after resolving conflictscareful
In plain English: After fixing conflict markers, stage files and tell Git to keep rebasing.
git add . git rebase --continue
git rebase --abortcareful
In plain English: Stops the rebase and restores the branch to how it was before.
git rebase --abort
git rebase -i HEAD~3careful
In plain English: Interactive rebase on the last three commits — squash, reorder, edit, or drop commits.
git rebase -i HEAD~3
Interactive rebase actions
In plain English: Common verbs you pick in the rebase editor.
pick use commit reword change commit message edit stop to amend commit squash combine with previous commit fixup combine and discard message drop remove commit
11. Undoing things safely
Unstage, discard, reset, or revert — pick the tool that matches whether work is shared.
git restore --staged file.txt
In plain English: Removes a file from the staging area but keeps your edits in the file.
git restore --staged file.txt
git reset HEAD file.txt
In plain English: Older way to unstage one file.
git reset HEAD file.txt
git restore file.txtcareful
In plain English: Throws away unstaged changes in one file — back to last commit.
git restore file.txt
git checkout -- file.txtcareful
In plain English: Older syntax to discard changes in one file.
git checkout -- file.txt
git restore .careful
In plain English: Discards all unstaged changes in the current directory.
git restore .
git reset --mixed HEAD~1careful
In plain English: Moves branch back one commit; changes become unstaged edits in your working folder.
git reset --mixed HEAD~1
git reset --soft HEAD~1careful
In plain English: Moves branch back one commit but keeps changes staged.
git reset --soft HEAD~1
git reset --hard HEAD~1careful
In plain English: Deletes the last commit and all its changes from your working tree.
git reset --hard HEAD~1
Dangerous. May be recoverable via reflog if recent.
git revert <commit-hash>
In plain English: Creates a new commit that undoes a past commit — safe for shared history.
git revert <commit-hash>
12. Stashing temporary work
Set unfinished work aside in a drawer so you can switch branches cleanly.
git stash
In plain English: Temporarily shelves uncommitted changes so your working tree is clean.
git stash
git stash push -m "WIP before switching branches"
In plain English: Same as stash, but labels the stash so you remember why.
git stash push -m "WIP before switching branches"
git stash -u
In plain English: Also stashes untracked files, not just tracked edits.
git stash -u
git stash list
In plain English: Shows every saved stash.
git stash list
git stash apply
In plain English: Re-applies the latest stash but keeps a copy in the stash list.
git stash apply
git stash pop
In plain English: Applies the latest stash and removes it from the list.
git stash pop
git stash apply stash@{2}
In plain English: Applies a specific stash by its index.
git stash apply stash@{2}
git stash drop stash@{0}
In plain English: Deletes one stash entry.
git stash drop stash@{0}
git stash clearcareful
In plain English: Deletes every stash.
git stash clear
13. Tags and releases
Mark important points in history — like v1.0.0 — for releases.
git tag
In plain English: Lists tags in the repository.
git tag
git tag v1.0.0
In plain English: Creates a lightweight tag at the current commit.
git tag v1.0.0
git tag -a v1.0.0 -m "Release v1.0.0"
In plain English: Creates an annotated tag with a message — better for releases.
git tag -a v1.0.0 -m "Release v1.0.0"
git push origin v1.0.0
In plain English: Publishes one tag to the remote.
git push origin v1.0.0
git push --tags
In plain English: Pushes all local tags.
git push --tags
git tag -d v1.0.0
In plain English: Deletes a tag locally.
git tag -d v1.0.0
git push origin --delete v1.0.0
In plain English: Deletes a tag on the remote.
git push origin --delete v1.0.0
14. Cleaning files
Remove untracked files Git is not managing. Read carefully before running.
git clean -n
In plain English: Dry run — shows what would be deleted without deleting anything.
git clean -n
git clean -fcareful
In plain English: Deletes untracked files.
git clean -f
git clean -fdcareful
In plain English: Deletes untracked files and folders.
git clean -fd
git clean -fdxcareful
In plain English: Also deletes ignored files like build outputs and node_modules.
git clean -fdx
Very dangerous. Can remove dependency folders and local build artifacts.
15. Tracking files and .gitignore
Tell Git which files to ignore and stop tracking secrets or build output.
Create .gitignore
In plain English: A text file listing paths Git should never track.
touch .gitignore # Common entries: node_modules/ .env .DS_Store dist/ build/ *.log
git rm --cached file.txt
In plain English: Stops tracking a file but leaves it on your disk.
git rm --cached file.txt
Stop tracking a folder
In plain English: Untrack a whole directory, update gitignore, commit.
git rm -r --cached folder-name/ git add .gitignore git commit -m "Update gitignore"
16. Finding things
Search code, blame lines, and hunt commits by message or content.
git grep "search term"
In plain English: Searches tracked files for text — like grep but respects Git's view of the repo.
git grep "search term"
git blame file.txt
In plain English: Shows who last changed each line and in which commit.
git blame file.txt
git log --grep="bug fix"
In plain English: Finds commits whose messages contain a phrase.
git log --grep="bug fix"
git log -S "functionName"
In plain English: Finds commits that added or removed a specific string.
git log -S "functionName"
git log -- file.txt
In plain English: Shows commit history that touched one file.
git log -- file.txt
17. Reflog: recover lost work
Git keeps a journal of where your HEAD and branches have been — a safety net after mistakes.
git reflog
In plain English: Lists recent branch movements — your undo history for local actions.
git reflog
git reset --hard <reflog-hash>careful
In plain English: Jumps your branch back to an earlier reflog entry.
git reset --hard <reflog-hash>
git branch rescue-branch <reflog-hash>
In plain English: Creates a new branch pointing at recovered work without moving your current branch.
git branch rescue-branch <reflog-hash>
18. Cherry-picking
Copy one specific commit from another branch onto your current branch.
git cherry-pick <commit-hash>
In plain English: Applies a single commit's changes here.
git cherry-pick <commit-hash>
git cherry-pick <hash1> <hash2>
In plain English: Applies several commits in order.
git cherry-pick <hash1> <hash2>
git cherry-pick --abort
In plain English: Cancels a conflicted cherry-pick.
git cherry-pick --abort
19. Submodules
Repos that embed other repos — common in larger projects. Optional until you need them.
git clone --recurse-submodules https://github.com/user/repo.git
In plain English: Clones a repo and its nested sub-repositories in one go.
git clone --recurse-submodules https://github.com/user/repo.git
git submodule update --init --recursive
In plain English: Fetches submodules after a plain clone.
git submodule update --init --recursive
git submodule add https://github.com/user/library.git path/to/library
In plain English: Embeds another repo at a path inside yours.
git submodule add https://github.com/user/library.git path/to/library
git submodule update --remote
In plain English: Updates submodules to the latest commit on their tracked branch.
git submodule update --remote
20. Common full workflows
End-to-end sequences you can copy when a single command is not enough.
New project to GitHub
In plain English: From empty folder to first push on main.
mkdir my-project cd my-project git init git add . git commit -m "Initial commit" git branch -M main git remote add origin https://github.com/user/repo.git git push -u origin main
Clone, edit, commit, push
In plain English: Day-one contributor flow on a feature branch.
git clone https://github.com/user/repo.git cd repo git switch -c feature/my-change # edit files git status git add . git commit -m "Add my change" git push -u origin feature/my-change
Update your local main branch
In plain English: Get the latest main from the server.
git switch main git pull
Update main (explicit)
In plain English: Safer two-step version — fetch first, then merge.
git switch main git fetch origin git merge origin/main
Create feature branch from latest main
In plain English: Start new work from an up-to-date main.
git switch main git pull git switch -c feature/new-thing
Update feature with merge
In plain English: Bring main into your feature branch via merge.
git switch feature/new-thing git fetch origin git merge origin/main
Update feature with rebasecareful
In plain English: Replay your commits on top of latest main.
git switch feature/new-thing git fetch origin git rebase origin/main git push --force-with-lease
Finish feature with merge
In plain English: Land the feature on main and delete the branch.
git switch main git pull git merge feature/new-thing git push git branch -d feature/new-thing
Fix mistake before committing
In plain English: Review and discard or unstage before commit.
git status git diff git restore file.txt # or unstage: git restore --staged file.txt
Fix last commit before pushingcareful
In plain English: Amend message or contents while still local.
git add . git commit --amend --no-edit # or change message: git commit --amend -m "Better commit message"
Undo a pushed commit safely
In plain English: Use revert — does not rewrite shared history.
git revert <commit-hash> git push
Committed to main by mistakecareful
In plain English: Rescue work onto a branch and reset main.
git branch feature/save-my-work git reset --hard origin/main git switch feature/save-my-work
Save work and switch branches
In plain English: Stash, switch, come back and pop.
git stash push -m "WIP" git switch other-branch # later: git switch original-branch git stash pop
Resolve merge conflicts
In plain English: Fix markers, stage, commit.
git status # open conflicted files and fix them git add . git commit
Resolve rebase conflictscareful
In plain English: Fix, stage, continue — or abort.
git status # fix files git add . git rebase --continue # abort if needed: git rebase --abort
Squash last 3 commitscareful
In plain English: Interactive rebase then force-push with lease.
git rebase -i HEAD~3 # keep first as pick, others squash or fixup git push --force-with-lease
Delete local and remote branch
In plain English: Clean up after merge.
git branch -d feature/old-branch git push origin --delete feature/old-branch
21. Most useful daily combinations
Short command chains for recurring situations. Expand any section above for detail.
git branch git status git diff
git add . git commit -m "Meaningful message"
git pull # or: git fetch origin git merge origin/main
git switch main git pull git switch -c feature/name
git status git diff git add -p git diff --staged git commit -m "Message"
git push -u origin feature/name
git stash -u git switch other-branch
git fetch origin git rebase origin/main
git reset --soft HEAD~1
git reset --hard git clean -fd
24. Commands to be careful with
These can destroy or rewrite work. Prefer the safer alternatives. Before risky ops: git branch backup-$(date +%Y%m%d) or git tag backup-before-reset.
git reset --hardSafer: Use revert for shared commits; soft/mixed reset locallygit clean -fdSafer: Run git clean -n first (dry run)git clean -fdxSafer: Never on a repo with local env or build dirs you needgit push --forceSafer: git push --force-with-leasegit rebaseSafer: Merge instead if others share your branchgit branch -DSafer: git branch -d after mergegit stash clearSafer: git stash drop stash@{n} one at a time