Git

Advanced Usage

Git Logo

Tyler Veness

Tags

  • Want to easily retreive a commit later?
    • git tag <tag name> [commit hash]
  • Getting to the tagged commit later?
    • git switch <tag name>
    • Now in "detached head state" (more on this next)

Detached Head State

  • How to leave detached head state?
    • git switch main
    • Sets current HEAD to main's HEAD
  • Commits can be made here!
    • Saving commits
      • git branch temp
      • git switch temp

Detached Head State

  • Options for temp branch
    • Update main to temp... WARNING: destructive!
      • git branch -f main temp
      • Fine if changes on main were bad
      • git switch main
    • Or... merge it with HEAD
      • Find temp branch commit in reflog (ex: HEAD@{1})
        • git switch main
        • git merge HEAD@{1}
  • temp is now reachable by main at HEAD
    • git branch -d temp

Detached Head State

  • If you just want to edit the commit checked out
    • git rebase -i with edit mentioned next to commit
      • Triggers commmit --amend scenario
      • git rebase --continue

Contingencies

For if/when you screw up

Changing History

  • Messed up commit message?
    • git commit --amend
  • Forgot to add files or added wrong ones?
    • git add
    • git rm
    • git commit --amend
  • Want to change/reorder a bunch of commits?
    • git rebase -i

Rebasing

"Forward-port local commits to the updated upstream head"
– git rebase man page
  • git rebase -i
    • Clean up a billion commits that don't make sense apart?
      • Squash
      • Fixup
    • Reorder/remove commits line-by-line
  • WARNING: do not rebase commits pushed to a public repository

Interactive Rebase Example

$ git rebase -i HEAD~3

pick b65221c Adjusted RightLeftAuton and fixed auto-shifting
pick d9aaab3 changes autonomous claw angle
pick 380719d Added noop autonomous

# Rebase efb7586..380719d onto efb7586
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell

Rebase Example

git rebase --onto main client
Rebase Example 1

Rebase Example

git rebase --onto main client
Rebase Example 2

Rebase Example

git switch main
git merge client
Rebase Example 2

Rebase Example

git switch main
git merge client
Rebase Example 3

Rebase Example

git rebase --onto main server
Rebase Example 3

Rebase Example

git rebase --onto main server
Rebase Example 4

Rebase Example

git switch main
git merge server
git branch -d client
git branch -d server
Rebase Example 4

Rebase Example

git switch main
git merge server
git branch -d client
git branch -d server
Rebase Example 5

The Nuclear Option

The Nuclear Option

  • What if large binary files were committed deep within the history?
    • git filter-branch --tree-filter 'rm -f largeFile.out' HEAD
  • Can be a very bad idea
    • git filter-branch --tree-filter 'rm -rf *' HEAD

Resources