Skip to content

Git

New Branch

git switch -c $branch_name

Undo Last Commit to Staged

git reset --soft HEAD~1

Undo Last Commit to Unstaged

git reset --mixed HEAD~1

Reset to Remote

git fetch origin
git reset --hard origin/main

Undo Local Changes

index: backtrack

git reflog
# Observe the step you want to go back (e.g: HEAD@{3} or <SHA>)
git reset --hard 'HEAD@{1}'

Worktrees

# You can add variables to `.envrc`
export PRJ_WT="../myproject-worktrees"

mkdir -p "${PRJ_WT}"
# Create a new worktree
git worktree add -b hotfix/bug-123 "${PRJ_WT}/hotfix" origin/main
#                   ^ branch        ^ new dir         ^ from remote

# See all worktrees
git worktree list
# /home/user/myproject        a1b2c3d [main]
# /home/user/myproject-worktrees/hotfix   e4f5g6h [hotfix/bug-123]

# Clean up when done
cd ../myproject
git worktree remove "${PRJ_WT}/hotfix"     # safe: refuses if dirty
git worktree remove --force "${PRJ_WT}/hotfix"  # nuke anyway

# Prune stale references (e.g. folders deleted manually)
git worktree prune

# Move a worktree elsewhere
git worktree move "${PRJ_WT}/hotfix" ../new-location