Skip to content

Git Guide

aschey-forpeople edited this page Apr 30, 2025 · 2 revisions

Git Guide

Commits

  • Should be descriptive
  • Generally, try to commit often
  • Look before you commit - git status

Merge

  • Creates a separate commit - even if there are no conflicts

  • Merge commits are "special" because they have two parent commits

  • Resolve conflicts once at the very end

    merge

Rebase

  • Rewrites history

    • If a PR is already open, GitHub will show the diff after a force push
  • Replaces old timestamps

  • Don't do this if more than one person is working on a branch

  • Use git push --force-with-lease instead of git push --force

    • this prevents overwriting work on the remote branch that you don't have locally
  • Risk of losing changes if conflicts occur

    • git reflog may be able to save you
  • Commits are replayed on top of the current branch - resolve conflicts one commit at a time

    • git rebase and git rebase --continue
    • can be time consuming if you have a lot of commits to go through
  • git rebase -i - interactive

    rebase

Dealing with Conflicts

  • Look before you commit
  • Rebuild
  • Look at what's in the source branch in a different window
  • When in doubt, accept both and fix it manually
  • Another option - create a fresh branch off of master, cherry pick commits from your old branch

Cherry-pick

  • Example:
    • Good for moving a commit from one branch to another
    • Example: cherry-picking commit f
      • Before

        cherry-pick-before

      • After

        cherry-pick-after

Revert

  • Creates a new commit
  • Be careful if reverting a merge - https://www.kernel.org/pub/software/scm/git/docs/git-revert.html
    • "Reverting a merge commit declares that you will never want the tree changes brought in by the merge. As a result, later merges will only bring in tree changes introduced by commits that are not ancestors of the previously reverted merge. This may or may not be what you want."
  • git revert abc1234 - revert commit abc1234

Reset

  • Erases an existing commit
  • git reset abc1234 - remove all commits until we reach abc1234
  • git reset --hard HEAD~1 remove latest commit and wipe all changes
  • git reset --soft HEAD~1 remove latest commit and keep changes

(Images courtesy of Atlassian)

Clone this wiki locally