Commands & workflows

0 visible

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.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
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).

Terminal
git config --global core.editor "code --wait"
git config --list

In plain English: Shows every Git setting currently in effect.

Terminal
git config --list
git config user.name

In plain English: Shows just your configured user name.

Terminal
git config user.name
git config user.email

In plain English: Shows just your configured email.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
git status
git status -s

In plain English: A shorter, one-line-per-file status view.

Terminal
git status -s
git log

In plain English: Lists past commits with messages, authors, and dates.

Terminal
git log
git log --oneline

In plain English: Compact history — one short line per commit.

Terminal
git log --oneline
git log --oneline --graph --all --decorate

In plain English: Shows branches and merges as a text diagram — great for understanding structure.

Terminal
git log --oneline --graph --all --decorate
git log -5 --oneline

In plain English: Shows only the five most recent commits.

Terminal
git log -5 --oneline
git show <commit-hash>

In plain English: Opens one specific commit and the exact changes it introduced.

Terminal
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.

Terminal
git add file.txt
git add .

In plain English: Stages every changed file in the current folder and subfolders.

Terminal
git add .
git add -u

In plain English: Stages only files Git already tracks — skips brand-new untracked files.

Terminal
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.

Terminal
git add -p
git commit -m "Describe the change"

In plain English: Saves staged changes as a permanent snapshot with a short message.

Terminal
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.

Terminal
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.

Terminal
git commit --amend
git commit --amend -m "New message"careful

In plain English: Changes only the message on your last commit.

Terminal
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.

Terminal
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.

Terminal
git diff
git diff --staged

In plain English: Shows what is staged and ready to commit. Same as --cached.

Terminal
git diff --staged
git diff --cached

In plain English: Alternate flag for staged changes.

Terminal
git diff --cached
git diff main..feature-branch

In plain English: Compares two branches to see what would change if you merged.

Terminal
git diff main..feature-branch
git diff <commit1> <commit2>

In plain English: Compares any two points in history.

Terminal
git diff <commit1> <commit2>
git diff --name-only

In plain English: Lists only file names that differ — no line-by-line detail.

Terminal
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.

Terminal
git branch
git branch -a

In plain English: Lists local and remote branches.

Terminal
git branch -a
git branch feature-name

In plain English: Creates a new branch but does not switch to it yet.

Terminal
git branch feature-name
git switch feature-name

In plain English: Moves you to another branch. Modern replacement for checkout.

Terminal
git switch feature-name
git checkout feature-name

In plain English: Older syntax — still common — to switch branches.

Terminal
git checkout feature-name
git switch -c feature-name

In plain English: Creates a branch and switches to it in one step.

Terminal
git switch -c feature-name
git checkout -b feature-name

In plain English: Older syntax for create-and-switch.

Terminal
git checkout -b feature-name
git branch -m new-branch-name

In plain English: Renames the branch you are currently on.

Terminal
git branch -m new-branch-name
git branch -d branch-name

In plain English: Deletes a merged local branch safely.

Terminal
git branch -d branch-name
git branch -D branch-namecareful

In plain English: Force-deletes a branch even if it is not fully merged.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
git remote set-url origin https://github.com/user/repo.git
git remote remove origin

In plain English: Removes the origin remote connection.

Terminal
git remote remove origin
git fetch

In plain English: Downloads new commits from the remote without changing your files yet.

Terminal
git fetch
git fetch --all

In plain English: Fetches from every configured remote.

Terminal
git fetch --all
git fetch --prune

In plain English: Removes stale remote-tracking branches that were deleted on the server.

Terminal
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.

Terminal
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.

Terminal
git push -u origin feature-name
git push origin main

In plain English: Pushes your main branch explicitly to origin.

Terminal
git push origin main
git pull

In plain English: Fetches and merges remote changes into your current branch. Same as fetch + merge.

Terminal
git pull
Pull = fetch + merge

In plain English: What pull does under the hood.

Terminal
git fetch
git merge
git pull --rebasecareful

In plain English: Replays your local commits on top of remote changes for a straighter history.

Terminal
git pull --rebase
Pull with rebase = fetch + rebase

In plain English: Equivalent steps when you pass --rebase.

Terminal
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.

Terminal
git push --force-with-lease
git push --forcecareful

In plain English: Overwrites remote history regardless. Can destroy teammates' work.

Terminal
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.

Terminal
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.

Terminal
git merge branch-name
git merge --abort

In plain English: Cancels a conflicted merge and returns to the pre-merge state.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
git add .
git rebase --continue
git rebase --abortcareful

In plain English: Stops the rebase and restores the branch to how it was before.

Terminal
git rebase --abort
git rebase -i HEAD~3careful

In plain English: Interactive rebase on the last three commits — squash, reorder, edit, or drop commits.

Terminal
git rebase -i HEAD~3
Interactive rebase actions

In plain English: Common verbs you pick in the rebase editor.

Terminal
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.

Terminal
git restore --staged file.txt
git reset HEAD file.txt

In plain English: Older way to unstage one file.

Terminal
git reset HEAD file.txt
git restore file.txtcareful

In plain English: Throws away unstaged changes in one file — back to last commit.

Terminal
git restore file.txt
git checkout -- file.txtcareful

In plain English: Older syntax to discard changes in one file.

Terminal
git checkout -- file.txt
git restore .careful

In plain English: Discards all unstaged changes in the current directory.

Terminal
git restore .
git reset --mixed HEAD~1careful

In plain English: Moves branch back one commit; changes become unstaged edits in your working folder.

Terminal
git reset --mixed HEAD~1
git reset --soft HEAD~1careful

In plain English: Moves branch back one commit but keeps changes staged.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
git stash
git stash push -m "WIP before switching branches"

In plain English: Same as stash, but labels the stash so you remember why.

Terminal
git stash push -m "WIP before switching branches"
git stash -u

In plain English: Also stashes untracked files, not just tracked edits.

Terminal
git stash -u
git stash list

In plain English: Shows every saved stash.

Terminal
git stash list
git stash apply

In plain English: Re-applies the latest stash but keeps a copy in the stash list.

Terminal
git stash apply
git stash pop

In plain English: Applies the latest stash and removes it from the list.

Terminal
git stash pop
git stash apply stash@{2}

In plain English: Applies a specific stash by its index.

Terminal
git stash apply stash@{2}
git stash drop stash@{0}

In plain English: Deletes one stash entry.

Terminal
git stash drop stash@{0}
git stash clearcareful

In plain English: Deletes every stash.

Terminal
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.

Terminal
git tag
git tag v1.0.0

In plain English: Creates a lightweight tag at the current commit.

Terminal
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.

Terminal
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.

Terminal
git push origin v1.0.0
git push --tags

In plain English: Pushes all local tags.

Terminal
git push --tags
git tag -d v1.0.0

In plain English: Deletes a tag locally.

Terminal
git tag -d v1.0.0
git push origin --delete v1.0.0

In plain English: Deletes a tag on the remote.

Terminal
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.

Terminal
git clean -n
git clean -fcareful

In plain English: Deletes untracked files.

Terminal
git clean -f
git clean -fdcareful

In plain English: Deletes untracked files and folders.

Terminal
git clean -fd
git clean -fdxcareful

In plain English: Also deletes ignored files like build outputs and node_modules.

Terminal
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.

Terminal
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.

Terminal
git rm --cached file.txt
Stop tracking a folder

In plain English: Untrack a whole directory, update gitignore, commit.

Terminal
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.

Terminal
git grep "search term"
git blame file.txt

In plain English: Shows who last changed each line and in which commit.

Terminal
git blame file.txt
git log --grep="bug fix"

In plain English: Finds commits whose messages contain a phrase.

Terminal
git log --grep="bug fix"
git log -S "functionName"

In plain English: Finds commits that added or removed a specific string.

Terminal
git log -S "functionName"
git log -- file.txt

In plain English: Shows commit history that touched one file.

Terminal
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.

Terminal
git reflog
git reset --hard <reflog-hash>careful

In plain English: Jumps your branch back to an earlier reflog entry.

Terminal
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.

Terminal
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.

Terminal
git cherry-pick <commit-hash>
git cherry-pick <hash1> <hash2>

In plain English: Applies several commits in order.

Terminal
git cherry-pick <hash1> <hash2>
git cherry-pick --abort

In plain English: Cancels a conflicted cherry-pick.

Terminal
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.

Terminal
git clone --recurse-submodules https://github.com/user/repo.git
git submodule update --init --recursive

In plain English: Fetches submodules after a plain clone.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
git switch main
git pull
Update main (explicit)

In plain English: Safer two-step version — fetch first, then merge.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
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.

Terminal
git revert <commit-hash>
git push
Committed to main by mistakecareful

In plain English: Rescue work onto a branch and reset main.

Terminal
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.

Terminal
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.

Terminal
git status
# open conflicted files and fix them
git add .
git commit
Resolve rebase conflictscareful

In plain English: Fix, stage, continue — or abort.

Terminal
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.

Terminal
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.

Terminal
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.

Where am I and what changed?
Combo
git branch
git status
git diff
Save my work
Combo
git add .
git commit -m "Meaningful message"
Get latest code
Combo
git pull
# or:
git fetch origin
git merge origin/main
Start new work safely
Combo
git switch main
git pull
git switch -c feature/name
Review before committing
Combo
git status
git diff
git add -p
git diff --staged
git commit -m "Message"
Push a new branch
Combo
git push -u origin feature/name
Switch branches mid-work
Combo
git stash -u
git switch other-branch
Bring branch up to datecareful
Combo
git fetch origin
git rebase origin/main
Undo last local commit, keep workcareful
Combo
git reset --soft HEAD~1
Throw away all local changescareful
Combo
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 locally
git clean -fdSafer: Run git clean -n first (dry run)
git clean -fdxSafer: Never on a repo with local env or build dirs you need
git push --forceSafer: git push --force-with-lease
git rebaseSafer: Merge instead if others share your branch
git branch -DSafer: git branch -d after merge
git stash clearSafer: git stash drop stash@{n} one at a time
Copied.