Skip to content

Instantly share code, notes, and snippets.

@colinta
Last active February 8, 2017 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinta/ff23c68907356c68f2aaedfb44a9bfa6 to your computer and use it in GitHub Desktop.
Save colinta/ff23c68907356c68f2aaedfb44a9bfa6 to your computer and use it in GitHub Desktop.
My quick and dirty dive into git theory and tricks

GIT OBJECTS

.git/objects /f4/8011a40ba2f5b9f6af3b387e

  • This is the "heart" of git, but not the brains

  • key-value store (key == sha1!)

  • blobs

  • trees

  • refs

    • commits, branches, tag, HEAD

REWRITING HISTORY

Public or Private

  • private? Go nuts!

    git commit --amend git push --force origin master git rebase --onto origin/master

  • public? Use merge! It has benefits, I swear!


TORVALD'S OPINION

http://www.mail-archive.com/dri-devel@lists.sourceforge.net/msg39091.html

I want clean history, but that really means (a) clean and (b) history.

People can (and probably should) rebase their private trees (their own work). That's a cleanup. But never other peoples code. That's a "destroy history"

You must never EVER destroy other peoples history. You must not rebase commits other people did. Basically, if it doesn't have your sign-off on it, it's off limits: you can't rebase it, because it's not yours.

GOLDEN RULE OF REBASING

https://www.atlassian.com/git/tutorials/merging-vs-rebasing


REBASE THEORY

  * - * - A - B <- master
       \
        - C - D <- you are here

git rebase --onto master git rebase master

* - * - A - B <- master
                     \
                      - C - D <- now you are here

Linear history is nice to have:

* - * - A - B - C - D <- you
            ^ master

REBASE MASTERY

git rebase -i master

reorder (not a command)
  • move important commits to beginning
  • cleanup/whitespace to end
fixup
  • squashes and uses first commit message
squash
  • squashes and preserves commit messages
edit
  • finicky!
remove
  • maybe not as useful

    git rebase --abort git rebase --cont git rebase --skip


  MERGE THEORY
  * - * - A - B - master
       \
        - C - D <- you are here

git merge master

  * - * - A    -    B    <- master
           \         \
            - C - D - +  <- now you are here
  • merge commits are "nothing special" (but yes, they are)

  • traversing history through merge commits? compare commits!

    git log HEAD ^master # only show MY history, nothing that is in master


REFLOG, FSCK

There Be Dragons

  • git reflog

    • Records HEAD changes
  • git fsck --lost-found

    • Is pretty cool

GIT FU!

aka git log

  • personal fav:
git log
  --pretty=format:'%Cred%h%Creset - %Cgreen%ci%Creset%C(yellow)%d%Creset %s %Cblue(%cn)%Creset'
  # --pretty=format:'%h - %ci%d %s (%cn)'
  --abbrev-commit
  --date=relative
  --graph
aliased to

git ll

git log -p
git log --stat
git log --author 'Colin'
git log --author 'steam\|process255\|sean'

GIT FU!

aka aliases

~/.gitconfig
git config --global alias.st status

Really common, highly recommended:

  • st = status
  • br = branch
  • ci = commit
  • co = checkout

alias g=git # for the supremely lazy

g st
g co -  # checkout previous branch
g ci -am "your message here"  # commit all changes w/ commit message

GIT FU!

aka aliases

[alias]
  ignore = !([ ! -e .gitignore ] && touch .gitignore) | echo $1 >>.gitignore
  grab = ![[ -n "$1" ]] && git fetch origin $1:$1 || echo "You must specify a branch"

git ignore [file]
git grab master  # updates master, you don't change branches
# git fetch origin master:master
git rebase master

git my
# git log HEAD ^master

# all files being tracked
git ls-files

git diff --patience -w   # ignore whitespace
git diff --color-words   # show word changes (instead of line changes)
git diff --cached        # show changes *about* to be committed

LINKS!

https://git-scm.com/book/en/v2
https://www.atlassian.com/git/tutorials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment