Skip to main content

GIT

Common Workflows

  • check repository status
    • shows staged, unstaged, and untracked files
git status
  • add a specific file
    • stages only the specified file for the next commit
git add <file>
  • add all changes
    • stages all modified, new, and deleted files in the current directory and below
git add .
  • commit changes
    • saves the staged changes as a snapshot with a message
git commit -m "commit message"
  • push to remote and set upstream
    • pushes branch to origin and sets it as the default upstream for future git push / git pull
    • The -u flag is short for --set-upstream.
      • Upstream is shared by BOTH git push and git pull.
# first time sets upstream
git push -u origin <branch-name>

# subsequent (as the above command has set upstream)
git push
  • pull latest changes
    • fetches changes from remote and merges them into the current branch
# git knows upstream from `git push -u branch-name` command
git pull

# use explicit current or any branch
git pull origin <branch-name>

Branch Management

  • create a new branch
    • creates a branch but does not switch to it
git branch <branch-name>
  • create a new branch and checkout
    • creates the branch and immediately switches to it
git checkout -b <branch-name>
  • switch branches (modern checkout alternative)
    • switches to an existing branch (clearer than checkout)
git switch <branch-name>

# Creates the branch if it doesn't exist
git switch -c feature/login
  • list local branches
    • shows all local branches and the current one
git branch
  • list all branches
    • shows local and remote branches
git branch -a
  • delete local branch
    • deletes branch if already merged
git branch -d <branch-name>
  • force delete local branch
    • deletes branch even if not merged
git branch -D <branch-name>
  • delete remote branch
    • removes branch from remote repository
git push origin --delete <branch-name>

Ways to Merge and Sync Branches

  • Merges a local branch into your current branch
git merge branch-name
  • Merges a remote-tracking branch into your current branch
    • You usually want to run git fetch first
git fetch origin
git merge origin/branch-name
  • Merges the upstream branch configured for your current branch
    • Runs git fetch + git merge automatically
    • Only works cleanly if an upstream is set (usually via git push -u)
git pull

#Same as
git fetch
git merge <upstream-branch>
  • Fetches branch-name from origin
    • Merges it into your current branch
    • Can pull any remote branch into your current branch
git pull origin branch-name

# Same as
git fetch origin branch-name
git merge origin/branch-name

Undoing & Fixing Mistakes

  • unstage a file
    • removes file from staging area but keeps local changes
git restore --staged <file>
  • discard local changes
    • reverts file to last committed state (changes are lost)
git restore <file>
  • amend last commit
    • modifies the last commit (message and/or content)
git commit --amend
  • revert a commit safely
    • creates a new commit that undoes a previous one (safe for shared branches)
git revert <commit-hash>
  • reset commit pointer (soft)
    • moves HEAD but keeps changes staged
git reset --soft <commit-hash>
  • reset commit pointer (hard)
    • completely discards commits and local changes
git reset --hard <commit-hash>

Others

  • cherry-pick a commit
    • applies a specific commit from another branch
git cherry-pick <commit-hash>
  • rebase vs merge
    • merge → preserves history, adds merge commit
    • rebase → rewrites history into a linear timeline
  • rebase current branch
    • replays current branch commits on top of main
git rebase main
  • pull with rebase
    • avoids merge commits when syncing with remote
git pull --rebase origin <branch-name>

Partial / Selective Operations

  • restore a folder from another branch
    • pulls files from another branch without switching branches
git restore --source <branch-name> <path>

example:

git restore --source feature/user-verifications backend/

Stash (Temporary SAVE)

  • stash changes
    • saves uncommitted work and cleans working directory
git stash "wip feature login"
  • list stashes
    • shows all saved stashes
git stash list
  • apply stash
    • Applies the most recent stash without removing it from the stash list.
git stash apply

# apply a specific stash:
git stash apply stash@{0}
  • apply and delete stash
    • Applies the most recent stash and then removes it from the stash list if the apply succeeds.
git stash pop

# specific stash
git stash pop stash@{0}

WorkTrees

  • Why use WorkTrees?
    • What are git worktrees, and why should I use them? - The GitHub Blog
    • A better alternative to the Git stash workflow, where you stash your current progress, create a new branch, make the necessary fixes, and then return to your previous work.
    • With worktrees, you never leave your current branch and never need to stash changes. Your editor context for the original feature remains completely untouched.
  • How to use them
    • Create a new sibling folder called hotfix-workspace based on main.
    • Check out a new branch called hotfix-bug.
    • Open that folder in a new editor window (or cd into it) and fix the bug.
      • Your original editor window remains exactly as you left it.
    • Once you have created a merge request and it has been merged, you can remove the temporary worktree.
git worktree add ../hotfix-workspace -b hotfix-bug main
cd ../hotfix-workspace
# ...fix fix fix...
git add .
git commit -m "fix broken submit button"
git push origin hotfix-bug
cd ../main-project
git worktree remove ../hotfix-workspace
  • Git WorkTrees Drawbacks
    • Dependency bloat: each worktree directory requires its own copy of project dependencies until removed (for example, node_modules), which can consume significant disk space.

Further Reading