Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Advanced and Intermediate Git

Advanced and Intermediate Git

Jeff Felchner

December 20, 2012
Tweet

More Decks by Jeff Felchner

Other Decks in Technology

Transcript

  1. What’s In This Talk? ‣ What’s here... ‣ Things that

    stumped me ‣ Things I wish I’d known sooner ‣ Things that increase my productivity ‣ What’s not here... ‣ Hand holding What’s In This Talk?
  2. I’m Overzealous What I Had To Leave Out ‣ Garbage

    Collection ‣ How and why SHAs work ‣ The .git directory ‣ How to sign your tags ‣ Why moving/ renaming and copying files is important ‣ Advanced Git aliases ‣ Working with multiple remotes ‣ Setting up your local machine as a Git server ‣ Details about how Git clones a repository and the differences between protocols ‣ Differences between .. ... in ref specs ‣ Differences between ^ and ~ ‣ Why ‘git push’ usually Just Works
  3. I’m Overzealous What I Had To Leave Out ‣ What

    is HEAD and how does Git keep track of it ‣ How to allow Git bisect to save your sanity ‣ How to use the reflog to get yourself out of a jam ‣ What’s the difference between a submodule and a subtree
  4. What I Expect ‣ clone ‣ push and pull ‣

    Create branches and basic merges ‣ Passing familiarity with ‘the stage’ ‣ commit but typically just using --all ‣ merge almost exclusively into master What I Expect
  5. Data Safety Data Safety ‣ Plumbing Commands ‣ hash-object ‣

    cat-file ‣ count-objects ‣ verify-pack ‣ write-tree ‣ read-tree ‣ commit-tree ‣ rev-list ‣ merge-base ‣ rev-parse ‣ update-index ‣ diff-index
  6. Rewrite History Merge Conflict Bad A Bevis and Butthead C

    B Beavis and Butthead Bevis and Butthead!!! D Beavis and Butthead!!!
  7. Rewrite History Merge Conflict Bad A Bevis and Butthead C

    B Beavis and Butthead Bevis and Butthead!!!
  8. C* Rewrite History Merge Conflict Bad A Bevis and Butthead

    Beavis and Butthead Beavis and Butthead!!! B
  9. C* D* Rewrite History The Rebase Command # from the

    command line $ git rebase --onto B A D A C B D
  10. Rewrite History Preserving Merges # from the command line $

    git rebase --onto B A E A C B D E C* D*
  11. Rewrite History Preserving Merges # from the command line $

    git rebase -p --onto B A E A C B D E C* D* E*
  12. Rewrite History Interactive Rebasing D B Bevis and Butthead Bevis

    and Butthead!!! Beavis and Butthead!!! C A # from the command line $ git rebase --interactive --onto A A D
  13. Rewrite History Interactive Rebasing # from the command line $

    git rebase --interactive --onto A A D pick B Add my favorite TV Show pick C Give it some awesome sauce pick D I can’t BELIEVE I MISSPELLED IT! edit B Add my favorite TV Show pick C Give it some awesome sauce pick D I can’t BELIEVE I MISSPELLED IT!
  14. Rewrite History Interactive Rebasing D B Bevis and Butthead Bevis

    and Butthead!!! Beavis and Butthead!!! C A E Beavis and Butthead
  15. Rewrite History Interactive Rebasing D B Bevis and Butthead Bevis

    and Butthead!!! Beavis and Butthead!!! C A E Beavis and Butthead F Beavis and Butthead!!!
  16. Recipes Moving Branch Refs Safely $ git reset --hard $MY_SHA

    # from the command line $ git checkout -B $MY_BRANCH $MY_SHA
  17. Recipes Removing a File from History # from the command

    line $ git filter-branch --index-filter ‘ git rm -rf --cached --ignore-unmatch $SECRET_FILE ‘ --prune-empty -- --all
  18. Recipes Remove Specific Contents From Files # from the command

    line $ git filter-branch --index-filter ' if [ -f $FILE_WITH_SECRET_STUFF ]; then sed -i ’s/$SECRET_STRING_TO_REMOVE//g’ $FILE_WITH_SECRET_STUFF; git add -A fi' -- --all
  19. Recipes Create the Repository for the Library # from the

    command line $ git clone file://path/to/my/proj/repo /new/path/to/my/lib/repo $ cd /new/path/to/my/lib/repo $ git filter-branch --subdirectory-filter my_lib_dir --prune-empty -- --all $ git clean -df $ git gc --agressive --prune=now
  20. Recipes Modify Authorship # the precision option $ git filter-branch

    --env-filter ' if [ "$GIT_AUTHOR_NAME" = "Edmund Dantes" ]; then GIT_AUTHOR_NAME = "The Count of Monte Cristo"; GIT_AUTHOR_EMAIL = "[email protected]"; fi if [ "$GIT_COMMITTER_NAME" = "Edmund Dantes" ]; then GIT_COMMITTER_NAME = "Sinbad the Sailor"; GIT_COMMITTER_EMAIL = "[email protected]"; fi ' -- --all
  21. Recipes Modify Authorship # the nuclear option $ git filter-branch

    --commit-filter ' if [ "$GIT_AUTHOR_NAME" = "Lila Crane" ]; then skip_commit "$@"; else git commit_tree "$@"; fi ' -- --all
  22. Recipes Remove Large Files # find the largest files in

    your repo $ git verify-pack -v .git/objects/pack/pack-\*.idx | sort -k 3 -n # get the name of those files $ git rev-list --objects --all | grep $SHA # remove the file like we discussed earlier
  23. Recipes Reformat Commit Messages # change your issue tracking system

    $ git filter-branch --msg-filter ' GIT_COMMIT = sed ’s/^#(\d{7})//g’ $GIT_COMMIT; PT_ID = $MY_SED_MATCH NEW_ID = id_conversion.txt | egrep "^$PT_ID" | cut -f 2 export GIT_COMMIT = "$GIT_COMMIT\n\nhttp://my_new_issue_tracker.com/ issue/$NEW_ID" ' -- --all