Graduate Program KB

Git Notes

Stashing

  • Saves uncommitted work
    • git stash - stashes changes
    • git stash list - list changes
    • git stash show stash@{0} - shows contents of the stash
    • git stash apply - apply the last stash
    • git stash apply stash@{0} - apply a specific stash

Referencing

  • Branch is a pointer to a commit
  • HEAD is a pointer to the current branch, and what the parent of the next commit is
    • moves when you make a commit
    • moves when you checkout a new branch

Git Log

  • Searches for commits
    • git log --grep expression
      • eg. git log --grep=mail --author=nina --since=2.weeks searches for commits with mail in the name, committed by nina, in the past 2 weeks
    • git log diff-filter=filter --stat include or exclude files according to the filter
    • filters are:
      • R: Renamed
      • A: Added
      • M: Modified
      • D: Deleted

Git Show and Diff

  • git show commit - shows commits and contents
  • git show commit --stat - shows files changed in the commit
  • git show commit:file - look at a file in a different commit
  • Diff shows changes between commits, what's in the working area, what's between the staging area and repository
  • git diff - shows unstaged changes
  • git diff --staged - shows staged changes
  • git diff A B - shows changes on branch A, but not branch B
  • git branch --merged master - shows which branches have been merged to master
  • git branch --no-merged master - shows which branches have not been merged to master

Git Checkout

  • Changes head to point to new branch
  • Copies commit snapshot to be staged
  • Updates working area with branch contents
  • git checkout branch
  • git checkout file name - replaces working copy with staged copy

Git Clean and Reset

  • git clean - deletes untracked files
    • --dry-run - flag to see what would be deleted
      • -f - flag to perform deletion
    • -d - flag to clean directories
  • git reset commit - moves HEAD and current branch, resets the staging and working area
  • git reset ORIG_HEAD - undo git reset. Keeps previous HEAD commit saved to revert if you need to.
  • git revert - creates a new commit with opposite changes from specified commit. Does not change history

Rebasing

  • git rebase commit
  • Gives commit a new parent
  • Interactive Rebase opens editor with different options for your commit. Includes:
    • Squashing
    • Renaming commit messages
    • Pick to keep a commit
    • Edit to edit more than just the message
    • Fixup to squash two commmits but keep previous commit message
    • Exec to execute a command after picking a commit
  • git rebase --abort - cancel the rebase.