Javad Zahrabi

Git

Daily git plumbing — the moves I do without thinking.

Official docs

Status & inspect

  • git status

    What's staged, unstaged, untracked, and which branch you're on.

  • git status -sb

    Short + branch summary. Easier to grep in scripts.

  • git diff

    Unstaged changes vs. the index.

  • git diff --staged

    What's about to be committed.

  • git log --oneline --graph --decorate -20

    Compact branch graph for the last 20 commits.

  • git show <sha>

    Full commit message + diff for one commit.

  • git blame <file>

    Last commit that touched each line.

Stage & commit

  • git add <path>

    Stage a file or directory. -A stages everything.

  • git add -p

    Interactively stage hunks — invaluable for keeping commits focused.

  • git commit -m 'message'

    Commit the index with a one-line message.

  • git commit --amend

    Replace the last commit (message + tree). Don't do this after pushing without --force.

  • git restore --staged <file>

    Unstage without losing the change.

  • git restore <file>

    Throw away unstaged changes in a file.

Branches

  • git switch -c feature/x

    Create + check out a new branch from HEAD. Modern alternative to `checkout -b`.

  • git switch master

    Switch to an existing branch.

  • git branch -d feature/x

    Delete a local branch (-D to force when not merged).

  • git branch -vv

    List branches with their tracking remotes + ahead/behind counts.

  • git merge --no-ff feature/x

    Merge with a real merge commit so the branch stays visible in history.

Remotes & sync

  • git fetch

    Update remote-tracking refs. No tree/working-dir changes.

  • git fetch --prune

    Also delete remote-tracking branches that no longer exist on the server.

  • git pull --rebase

    Fetch + rebase your local commits on top of the new tip. Default this in ~/.gitconfig.

  • git push -u origin HEAD

    Push the current branch and set upstream tracking in one go.

  • git push --force-with-lease

    Safer force-push: refuses to clobber commits you haven't seen.

Rebase, reset & rescue

  • git rebase -i HEAD~5

    Edit/squash/reorder the last 5 commits. Don't rebase commits that are already shared.

  • git rebase master

    Move the current branch to start from the tip of master.

  • git reset --soft HEAD~1

    Undo the last commit but keep its changes staged.

  • git reset --hard origin/master

    Make the working tree exactly match origin/master. Destructive — careful.

  • git reflog

    Local history of every HEAD move. Lifesaver for un-deleting commits.

  • git stash

    Set aside uncommitted changes. `git stash pop` brings them back.

  • git cherry-pick <sha>

    Re-apply one commit on top of the current branch.