Git & Terminal Cheatsheet

Branching and Merging

Use this Git & Terminal reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Branches

Modern Git (2.23+) uses git switch for branches. git checkout still works and does the same thing, it just also has unrelated file-restore behavior.

git branch                      # list local branches (* marks current)
git branch -a                   # include remote-tracking branches
git branch -vv                  # show upstream and last commit per branch
git switch -c feature           # create AND switch to a new branch
git switch main                 # switch to an existing branch
git switch -                    # jump back to the previous branch
git switch -c hotfix abc1234    # branch off a specific commit
git checkout -b feature         # legacy equivalent of switch -c

Stash (Set Work Aside)

Switching branches with uncommitted changes? Stash them:

git stash                       # stash tracked changes
git stash -u                    # include untracked files
git stash push -m "wip: login"  # stash with a label
git stash list                  # stash@{0} is the newest
git stash pop                   # re-apply newest stash and drop it
git stash apply stash@{2}       # re-apply a specific stash, keep it
git stash show -p stash@{0}     # inspect a stash as a diff
git stash drop stash@{0}        # delete one
git stash branch fix-login stash@{0}   # turn a stash into a branch

Merging

git switch main             # go to the branch you want to merge INTO
git merge feature           # bring feature's commits into main
git merge --no-ff feature   # force a merge commit even when fast-forward is possible
git merge --squash feature  # collapse the whole branch into one staged change
git merge --abort           # bail out of a conflicted merge

Resolving Merge Conflicts

When both branches touched the same lines, the merge stops and marks the files:

<<<<<<< HEAD
your version (current branch)
=======
their version (branch being merged)
>>>>>>> feature

Workflow:

git status                          # lists "both modified" files
# edit each file, keep what you want, delete the <<< === >>> markers
git checkout --ours  path/file.txt  # or take YOUR side wholesale
git checkout --theirs path/file.txt # or take THEIR side wholesale
git add path/file.txt               # mark it resolved
git merge --continue                # finish the merge (or: git commit)
git merge --abort                   # or give up and restore pre-merge state

git mergetool opens a configured visual tool. During a rebase, --ours/--theirs are swapped relative to what you expect, because the rebase replays YOUR commits onto theirs.

Rebase

Rebase replays your branch's commits on top of another branch for a linear history:

git switch feature
git rebase main                 # replay feature's commits onto main
git rebase --continue           # after fixing a conflict
git rebase --abort              # give up, return to pre-rebase state

Interactive rebase rewrites your last N commits (squash, reorder, reword):

git rebase -i HEAD~3            # edit the last 3 commits
git commit --fixup abc1234      # mark a commit as a fix for an older one
git rebase -i --autosquash main # auto-arrange fixup commits
In the todo listEffect
pickkeep the commit as-is
rewordkeep it, edit the message
squashmeld into the previous commit, combine messages
fixupmeld into the previous commit, discard message
editpause to amend this commit
dropdelete the commit

The golden rule: never rebase commits that others may have pulled. Rebase your own unpushed or personal-branch work, then push with --force-with-lease.

Cherry-Pick

Copy specific commits onto the current branch:

git cherry-pick abc1234             # apply one commit here
git cherry-pick abc1234 def5678     # several
git cherry-pick abc1234^..def5678   # an inclusive range
git cherry-pick -x abc1234          # append "(cherry picked from ...)" to the message
git cherry-pick -n abc1234          # apply to working tree without committing
git cherry-pick --abort             # bail out of a conflicted pick

Renaming and Deleting

git branch -m old new               # rename a branch
git branch -d feature               # delete a merged local branch
git branch -D feature               # force-delete an unmerged branch
git push origin --delete feature    # delete the branch on the remote
git fetch --prune                   # drop local refs to deleted remote branches

git push origin :feature is the legacy remote-delete syntax, prefer --delete.

TaskCommand
New branchgit switch -c <name>
Switchgit switch <name>
Merge ingit merge <name>
Abort mergegit merge --abort
Linear updategit rebase main
Squash historygit rebase -i HEAD~N
Copy a commitgit cherry-pick <hash>
Delete remote branchgit push origin --delete <name>