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 checkout <tag name>
    • Now in "detached head state" (more on this next)

Detached Head State

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

Detached Head State

  • Options for temp branch
    • Update master to temp... WARNING: destructive!
      • git branch -f master temp
      • Fine if changes on master were bad
      • git checkout master
    • Or... merge it with HEAD
      • Find temp branch commit in reflog (ex: HEAD@{1})
        • git checkout master
        • git merge HEAD@{1}
  • temp is now reachable by master 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 master server client
Rebase Example 1

Rebase Example

git rebase --onto master server client
Rebase Example 2

Rebase Example

git checkout master
git merge client
Rebase Example 2

Rebase Example

git checkout master
git merge client
Rebase Example 3

Rebase Example

git rebase master server
Rebase Example 3

Rebase Example

git rebase master server
Rebase Example 4

Rebase Example

git checkout master
git merge server
git branch -d client
git branch -d server
Rebase Example 4

Rebase Example

git checkout master
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