SlideShare a Scribd company logo
1 of 42
Download to read offline
Advanced Git Tutorial
   by Sarah Sharp
WARNING:
I am a unique snowflake
WARNING:
This tutorial may make you lazy
WARNING:
Some Git features are
    dangerous!
What is Git?


●   Distributed
●   Fast
●   Flexible
Git Basics
●       See Everyday Git Tutorial:
    –   http://www.kernel.org/pub/software/scm/git/doc
        s/everyday.html
●       My git commands:
    –   git add       - git commit
    –   git diff      - git log      - git show
    –   git push      - git pull
    –   git fetch     - git rebase
Naming commits
                <Commitish>
●   (Indirect) hash of repo files, current
    commit message, and ancestor commits.
●   HEAD refers to the last commit
●   ~ at the end means commit before that
      –   e.g. HEAD~
      –   ^ is roughly equivalent to ~
●   A branch points to a specific commit
●   see git rev-parse
Git Philosophy
Git Philosophy
●   Commit early, commit often
●   One commit represents one idea or one
    change.
    –   Makes it easy to read patches
    –   Easy to revert unwanted changes later
●   Your working directory, index, and local
    repo are your scratch pads.
Frequent Use   Infrequent Use
The Index, the staging area




      Front stage:             Back stage:
 Changes to be committed   Uncommited changes
                            and unadded files
Staging Changes
●   git add <file> – adds a file to the Index
●   git commit – commits added changes to
    the local repo
●   But what about files not added to the
    Index?
Staging Changes
●   git add <file> – adds a file to the Index
●   git commit – commits added changes to
    the local repo
●   But what about files not added to the
    Index?
    –   Answer: they aren't included in the commit.
●   Key idea: You can add and commit files
    separately from other files.
    –   This makes separating changes into small
        patches easier.
What changed?
 ●   git status
 ●   git diff


                        git diff HEAD
                                                       local
workspace
                                                    repository
            git diff   index    git diff --cached
Advanced Staging
Advanced Staging
●   git add --patch
    –   try the "split" option to split across hunks
●   git add -i
         –   Very powerful tool with lots of options
●   Key idea: You can add and commit
    different parts of a file separately.
Unstaging changes
●   Revert to the last commit
      –   git reset --hard HEAD
●   Remove all added changes from the index
      –   git reset --mixed HEAD
●   Remove some files added to index
      –   git add -i and choose revert, or
      –   git reset HEAD filename(s)
Viewing History
Viewing History
●   git log
●   git log <commit A>..<commit B>
    –   shows history after commit A, up to commit B
    –   can omit either commit
    –   e.g. `git log` `git log origin..` `git log ..v2.6.30`
●   git log -p
    –   shows log as a series of patches
●   git log --pretty=oneline –abbrev-commit
Viewing old files
●   Contents of a file at a particular commit
      –   git show <commitish>:<path to file>
●   Contents of a directory
      –   git show <commitish>:<directory>
Pointing Fingers:
   git blame
Pointing Fingers
●   git blame <file>
    –   show who committed each line
●   git blame <commit ID> <file>
    –   show the line history before that commit
Branches
Branches
●   Only one branch can be checked out
     –   trunk ~= master
●   show all branches
     –   git branch -a
●   switching branches
     –   git checkout name
●   creating new branches
     –   git checkout -b name <commit>
Advanced Branching
●   Merge branches with git merge
      –   creates a "merge commit"
●   Rebase current branch against branch B
      –   find a common ancestor commit
      –   apply commits from branch B
      –   apply commits from current branch
●   Apply a commit from one branch
      –   git cherry-pick
Interacting with other people
Interacting with other people
●   Creating a patchset, starting at commitA
      –   git format-patch -o directory commitA^
            --cc=<cced-email>
      –   use git send-email or `mutt -H <gitpatch>`
●   Applying a patch
      –   git am patchfile
      –   can also take a mailbox or maildir or stdin
●   Pushing a new branch
      –   git push remote branch
Changing History
Changing History:
             DANGER, WILL ROBINSON!
●   After a commit, often you will find bugs
    –   could make a new bug fix commit
    –   or you could "amend" the previous commit
●   Fix your code
●   git add <file>
    –   This adds your code to the index
●   git commit --amend
    –   This modifies the commit in the local repo
    –   useful to have vim git-commit script installed
Changing History:
              DANGER, WILL ROBINSON!
●   A total history rewrite:
    –   git rebase -i <commit ID>
●   Can reorder commits
●   Can edit commits
●   Can "squash" one commit into another
●   May have merge conflicts
         –   edit files, resolve conflicts surrounded by
              <<<< and >>>>
         –   git add files, git rebase --continue
git rebase -i --dontscrewme
●   No such command
●   git rebase -i the safe way:
      –   git checkout -b master-rebase
      –   use `git rebase -i` to move one patch
      –   resolve any merge conflicts
      –   squash that patch using `git rebase -i`
      –   git diff master master-rebase
      –   git branch -M master master-old
      –   git branch -M master-rebase master
Git Hooks
Git Hooks
●       Hooks are scripts found in .git/hooks/
●       Enable them with chmod a+x <file>
●       Triggered by various git commands
    –   e.g. git commit, git push
    –   pre-commit, post-update
●       Examples
    –   shipped pre-commit hook checks for white
        space at the end of line, long lines, etc.
    –   Checking for swear words?
Git Hooks
●   Example post-update hook on remote repo:
       #!/bin/sh
       cd /home/sarah/blog
       unset GIT_DIR
       git-fetch origin
       git-reset --hard origin/master


●   Whenever I push to the remote repository, this
    goes into the server's checkout of my blog git
    repo and updates it unconditionally.
Setting up a
remote repository
Setting up a
           remote repository
●   Server needs git and sshd installed to use
    git+ssh to push to your repo
●   Server needs webDAV installed to allow
    push to your repo over https
●   http://github.com/ will host your repo
●   Next directions assume you have your
    own server with git installed
Setting up a
             remote repository
1. Make local repo, commit stuff, etc.
2. ssh to the server:
   GIT_DIR=/path/to/repo git init --shared
3. Next, tell the local repo about the server:
   git remote add origin git+ssh://hostname/path/to/repo
4. Push to the server from the local repo:
   git push origin master
5. Clean up the local repo so that you can pull from the
  remote server:
   git config branch.master.remote origin
   git config branch.master.merge refs/heads/master
Resources
●   Git work flow diagrams:
    http://osteele.com/archives/2008/05/my-git-workflow
●   The Tangled Working Copy:
    http://tomayko.com/writings/the-thing-about-git
●   http://github.com/
●   Kernel module examples at http://lwn.net/Kernel/LDD3/
●   vim git-commit script will display commit messages in a
    more useful manner. Script kept at vim.sourceforge.net.
       –   sudo aptitude install vim-scripts vim-addon-manager
       –   vim-addons install git-commit
Creative Commons
              Image Attributions
●   GIT picture:
    http://flickr.com/photos/29862082@N06/2908889599/
●   Snowflake:
    http://commons.wikimedia.org/wiki/Image:SnowflakesWilson
    Bentley.jpg
●   Danger:http://flickr.com/photos/dawvon/32305882/
●   Cat: http://flickr.com/photos/jamilsoni/118499378/
●   Remote: http://flickr.com/photos/markkelley/957631507/
●   Philosophy: http://flickr.com/photos/paullew/2442045767/
●   Branches: http://flickr.com/photos/shapeshift/136184752/
●   Interacting with other people:
    http://www.flickr.com/photos/exlibris/3222440467/
Creative Commons
              Image Attributions
●   Front stage:
    http://flickr.com/photos/69108241@N00/118040089/
●   Back stage: http://flickr.com/photos/piotramigo/2561391320/
●   Hooks:http://www.flickr.com/photos/yabanji/3175297773/
●   Blame: http://flickr.com/photos/iandesign/1205496024/
●   Ballet: http://www.flickr.com/photos/oudeschool/3553416511/
●   Papyrus: http://flickr.com/photos/charlestilford/2548991271/
Thank you!
●   Sarah Sharp
●   @sarahsharp
●   http://sarah.thesharps.us

More Related Content

What's hot (20)

Git real slides
Git real slidesGit real slides
Git real slides
 
Introduction git
Introduction gitIntroduction git
Introduction git
 
Git in 10 minutes
Git in 10 minutesGit in 10 minutes
Git in 10 minutes
 
Git and github 101
Git and github 101Git and github 101
Git and github 101
 
Git training v10
Git training v10Git training v10
Git training v10
 
A Practical Introduction to git
A Practical Introduction to gitA Practical Introduction to git
A Practical Introduction to git
 
The everyday developer's guide to version control with Git
The everyday developer's guide to version control with GitThe everyday developer's guide to version control with Git
The everyday developer's guide to version control with Git
 
Git for beginners
Git for beginnersGit for beginners
Git for beginners
 
Advanced Git Presentation By Swawibe
Advanced Git Presentation By SwawibeAdvanced Git Presentation By Swawibe
Advanced Git Presentation By Swawibe
 
Github
GithubGithub
Github
 
GitHub Basics - Derek Bable
GitHub Basics - Derek BableGitHub Basics - Derek Bable
GitHub Basics - Derek Bable
 
Git basics to advance with diagrams
Git basics to advance with diagramsGit basics to advance with diagrams
Git basics to advance with diagrams
 
Basic Git Intro
Basic Git IntroBasic Git Intro
Basic Git Intro
 
Git basics
Git basicsGit basics
Git basics
 
Git and git flow
Git and git flowGit and git flow
Git and git flow
 
Git slides
Git slidesGit slides
Git slides
 
Git Started With Git
Git Started With GitGit Started With Git
Git Started With Git
 
Git and Github slides.pdf
Git and Github slides.pdfGit and Github slides.pdf
Git and Github slides.pdf
 
Introduction to Git and Github
Introduction to Git and GithubIntroduction to Git and Github
Introduction to Git and Github
 
Introduction to Git and Github
Introduction to Git and GithubIntroduction to Git and Github
Introduction to Git and Github
 

Viewers also liked

Advanced Git
Advanced GitAdvanced Git
Advanced Gitsegv
 
Getting Git Right
Getting Git RightGetting Git Right
Getting Git RightSven Peters
 
Quick Introduction to git
Quick Introduction to gitQuick Introduction to git
Quick Introduction to gitJoel Krebs
 
Energy Strategy Group_Report 2012 efficienza energetica
Energy Strategy Group_Report 2012 efficienza energeticaEnergy Strategy Group_Report 2012 efficienza energetica
Energy Strategy Group_Report 2012 efficienza energeticaEugenio Bacile di Castiglione
 
Secure PIN Management How to Issue and Change PINs Securely over the Web
Secure PIN Management How to Issue and Change PINs Securely over the WebSecure PIN Management How to Issue and Change PINs Securely over the Web
Secure PIN Management How to Issue and Change PINs Securely over the WebSafeNet
 
Alta White Paper D2C eCommerce Case Study 2016
Alta White Paper D2C eCommerce Case Study 2016Alta White Paper D2C eCommerce Case Study 2016
Alta White Paper D2C eCommerce Case Study 2016Patrick Nicholson
 
Diarrhea:Myths and facts, Precaution
Diarrhea:Myths and facts, Precaution Diarrhea:Myths and facts, Precaution
Diarrhea:Myths and facts, Precaution Wuzna Haroon
 
Enterprise workspaces - Extending SAP NetWeaver Portal capabilities
Enterprise workspaces - Extending SAP NetWeaver Portal capabilities Enterprise workspaces - Extending SAP NetWeaver Portal capabilities
Enterprise workspaces - Extending SAP NetWeaver Portal capabilities SAP Portal
 

Viewers also liked (14)

Advanced Git
Advanced GitAdvanced Git
Advanced Git
 
Getting Git Right
Getting Git RightGetting Git Right
Getting Git Right
 
Quick Introduction to git
Quick Introduction to gitQuick Introduction to git
Quick Introduction to git
 
Energy Strategy Group_Report 2012 efficienza energetica
Energy Strategy Group_Report 2012 efficienza energeticaEnergy Strategy Group_Report 2012 efficienza energetica
Energy Strategy Group_Report 2012 efficienza energetica
 
Secure PIN Management How to Issue and Change PINs Securely over the Web
Secure PIN Management How to Issue and Change PINs Securely over the WebSecure PIN Management How to Issue and Change PINs Securely over the Web
Secure PIN Management How to Issue and Change PINs Securely over the Web
 
"15 Business Story Ideas to Jump on Now"
"15 Business Story Ideas to Jump on Now""15 Business Story Ideas to Jump on Now"
"15 Business Story Ideas to Jump on Now"
 
Basics of Coding in Pediatrics Medical Billing
Basics of Coding in Pediatrics Medical BillingBasics of Coding in Pediatrics Medical Billing
Basics of Coding in Pediatrics Medical Billing
 
Credit cards
Credit cardsCredit cards
Credit cards
 
Alta White Paper D2C eCommerce Case Study 2016
Alta White Paper D2C eCommerce Case Study 2016Alta White Paper D2C eCommerce Case Study 2016
Alta White Paper D2C eCommerce Case Study 2016
 
cathy resume
cathy resumecathy resume
cathy resume
 
Information från Läkemedelsverket #5 2013
Information från Läkemedelsverket #5 2013Information från Läkemedelsverket #5 2013
Information från Läkemedelsverket #5 2013
 
Nt1310 project
Nt1310 projectNt1310 project
Nt1310 project
 
Diarrhea:Myths and facts, Precaution
Diarrhea:Myths and facts, Precaution Diarrhea:Myths and facts, Precaution
Diarrhea:Myths and facts, Precaution
 
Enterprise workspaces - Extending SAP NetWeaver Portal capabilities
Enterprise workspaces - Extending SAP NetWeaver Portal capabilities Enterprise workspaces - Extending SAP NetWeaver Portal capabilities
Enterprise workspaces - Extending SAP NetWeaver Portal capabilities
 

Similar to Advanced Git Tutorial

The Fundamentals of Git
The Fundamentals of GitThe Fundamentals of Git
The Fundamentals of GitDivineOmega
 
Pro git - grasping it conceptually
Pro git - grasping it conceptuallyPro git - grasping it conceptually
Pro git - grasping it conceptuallyseungzzang Kim
 
Git 入门与实践
Git 入门与实践Git 入门与实践
Git 入门与实践Terry Wang
 
Git 入门 与 实践
Git 入门 与 实践Git 入门 与 实践
Git 入门 与 实践Terry Wang
 
Introduction to Git
Introduction to GitIntroduction to Git
Introduction to GitAmit Mathur
 
Git and git workflow best practice
Git and git workflow best practiceGit and git workflow best practice
Git and git workflow best practiceMajid Hosseini
 
Git tech talk
Git tech talkGit tech talk
Git tech talkrazasayed
 
Hacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHubHacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHubDSC GVP
 
Git with the flow
Git with the flowGit with the flow
Git with the flowDana White
 
Getting some Git
Getting some GitGetting some Git
Getting some GitBADR
 

Similar to Advanced Git Tutorial (20)

The Fundamentals of Git
The Fundamentals of GitThe Fundamentals of Git
The Fundamentals of Git
 
Pro git - grasping it conceptually
Pro git - grasping it conceptuallyPro git - grasping it conceptually
Pro git - grasping it conceptually
 
Git and github introduction
Git and github introductionGit and github introduction
Git and github introduction
 
Git 入门与实践
Git 入门与实践Git 入门与实践
Git 入门与实践
 
Git
GitGit
Git
 
Git 入门 与 实践
Git 入门 与 实践Git 入门 与 实践
Git 入门 与 实践
 
Advanted git
Advanted git Advanted git
Advanted git
 
Introduction to Git
Introduction to GitIntroduction to Git
Introduction to Git
 
Git and git workflow best practice
Git and git workflow best practiceGit and git workflow best practice
Git and git workflow best practice
 
Git Tech Talk
Git  Tech TalkGit  Tech Talk
Git Tech Talk
 
Git tech talk
Git tech talkGit tech talk
Git tech talk
 
Introduction to GIT
Introduction to GITIntroduction to GIT
Introduction to GIT
 
Hacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHubHacktoberfest intro to Git and GitHub
Hacktoberfest intro to Git and GitHub
 
Git with the flow
Git with the flowGit with the flow
Git with the flow
 
Git
GitGit
Git
 
Git introduction
Git introductionGit introduction
Git introduction
 
Git basic
Git basicGit basic
Git basic
 
Git github
Git githubGit github
Git github
 
sample.pptx
sample.pptxsample.pptx
sample.pptx
 
Getting some Git
Getting some GitGetting some Git
Getting some Git
 

More from Sage Sharp

Countering impostor syndrome culture
Countering impostor syndrome cultureCountering impostor syndrome culture
Countering impostor syndrome cultureSage Sharp
 
Countering impostor syndrome culture
Countering impostor syndrome cultureCountering impostor syndrome culture
Countering impostor syndrome cultureSage Sharp
 
Herding cats with django
Herding cats with djangoHerding cats with django
Herding cats with djangoSage Sharp
 
Open source 101 for students
Open source 101 for studentsOpen source 101 for students
Open source 101 for studentsSage Sharp
 
Small, smaller, smallest: A Tour of the Embedded World
Small, smaller, smallest: A Tour of the Embedded WorldSmall, smaller, smallest: A Tour of the Embedded World
Small, smaller, smallest: A Tour of the Embedded WorldSage Sharp
 
Linux Kernel Introduction
Linux Kernel IntroductionLinux Kernel Introduction
Linux Kernel IntroductionSage Sharp
 
Breaking into Open Source and Linux: A USB 3.0 Success Story
Breaking into Open Source and Linux: A USB 3.0 Success StoryBreaking into Open Source and Linux: A USB 3.0 Success Story
Breaking into Open Source and Linux: A USB 3.0 Success StorySage Sharp
 
Vampire Mice: How USB PM Impacts You
Vampire Mice: How USB PM Impacts YouVampire Mice: How USB PM Impacts You
Vampire Mice: How USB PM Impacts YouSage Sharp
 

More from Sage Sharp (8)

Countering impostor syndrome culture
Countering impostor syndrome cultureCountering impostor syndrome culture
Countering impostor syndrome culture
 
Countering impostor syndrome culture
Countering impostor syndrome cultureCountering impostor syndrome culture
Countering impostor syndrome culture
 
Herding cats with django
Herding cats with djangoHerding cats with django
Herding cats with django
 
Open source 101 for students
Open source 101 for studentsOpen source 101 for students
Open source 101 for students
 
Small, smaller, smallest: A Tour of the Embedded World
Small, smaller, smallest: A Tour of the Embedded WorldSmall, smaller, smallest: A Tour of the Embedded World
Small, smaller, smallest: A Tour of the Embedded World
 
Linux Kernel Introduction
Linux Kernel IntroductionLinux Kernel Introduction
Linux Kernel Introduction
 
Breaking into Open Source and Linux: A USB 3.0 Success Story
Breaking into Open Source and Linux: A USB 3.0 Success StoryBreaking into Open Source and Linux: A USB 3.0 Success Story
Breaking into Open Source and Linux: A USB 3.0 Success Story
 
Vampire Mice: How USB PM Impacts You
Vampire Mice: How USB PM Impacts YouVampire Mice: How USB PM Impacts You
Vampire Mice: How USB PM Impacts You
 

Recently uploaded

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Advanced Git Tutorial

  • 1. Advanced Git Tutorial by Sarah Sharp
  • 2. WARNING: I am a unique snowflake
  • 4. WARNING: Some Git features are dangerous!
  • 5. What is Git? ● Distributed ● Fast ● Flexible
  • 6. Git Basics ● See Everyday Git Tutorial: – http://www.kernel.org/pub/software/scm/git/doc s/everyday.html ● My git commands: – git add - git commit – git diff - git log - git show – git push - git pull – git fetch - git rebase
  • 7. Naming commits <Commitish> ● (Indirect) hash of repo files, current commit message, and ancestor commits. ● HEAD refers to the last commit ● ~ at the end means commit before that – e.g. HEAD~ – ^ is roughly equivalent to ~ ● A branch points to a specific commit ● see git rev-parse
  • 9. Git Philosophy ● Commit early, commit often ● One commit represents one idea or one change. – Makes it easy to read patches – Easy to revert unwanted changes later ● Your working directory, index, and local repo are your scratch pads.
  • 10. Frequent Use Infrequent Use
  • 11. The Index, the staging area Front stage: Back stage: Changes to be committed Uncommited changes and unadded files
  • 12. Staging Changes ● git add <file> – adds a file to the Index ● git commit – commits added changes to the local repo ● But what about files not added to the Index?
  • 13. Staging Changes ● git add <file> – adds a file to the Index ● git commit – commits added changes to the local repo ● But what about files not added to the Index? – Answer: they aren't included in the commit. ● Key idea: You can add and commit files separately from other files. – This makes separating changes into small patches easier.
  • 14. What changed? ● git status ● git diff git diff HEAD local workspace repository git diff index git diff --cached
  • 16. Advanced Staging ● git add --patch – try the "split" option to split across hunks ● git add -i – Very powerful tool with lots of options ● Key idea: You can add and commit different parts of a file separately.
  • 17. Unstaging changes ● Revert to the last commit – git reset --hard HEAD ● Remove all added changes from the index – git reset --mixed HEAD ● Remove some files added to index – git add -i and choose revert, or – git reset HEAD filename(s)
  • 19. Viewing History ● git log ● git log <commit A>..<commit B> – shows history after commit A, up to commit B – can omit either commit – e.g. `git log` `git log origin..` `git log ..v2.6.30` ● git log -p – shows log as a series of patches ● git log --pretty=oneline –abbrev-commit
  • 20.
  • 21. Viewing old files ● Contents of a file at a particular commit – git show <commitish>:<path to file> ● Contents of a directory – git show <commitish>:<directory>
  • 22. Pointing Fingers: git blame
  • 23. Pointing Fingers ● git blame <file> – show who committed each line ● git blame <commit ID> <file> – show the line history before that commit
  • 25. Branches ● Only one branch can be checked out – trunk ~= master ● show all branches – git branch -a ● switching branches – git checkout name ● creating new branches – git checkout -b name <commit>
  • 26. Advanced Branching ● Merge branches with git merge – creates a "merge commit" ● Rebase current branch against branch B – find a common ancestor commit – apply commits from branch B – apply commits from current branch ● Apply a commit from one branch – git cherry-pick
  • 28. Interacting with other people ● Creating a patchset, starting at commitA – git format-patch -o directory commitA^ --cc=<cced-email> – use git send-email or `mutt -H <gitpatch>` ● Applying a patch – git am patchfile – can also take a mailbox or maildir or stdin ● Pushing a new branch – git push remote branch
  • 30. Changing History: DANGER, WILL ROBINSON! ● After a commit, often you will find bugs – could make a new bug fix commit – or you could "amend" the previous commit ● Fix your code ● git add <file> – This adds your code to the index ● git commit --amend – This modifies the commit in the local repo – useful to have vim git-commit script installed
  • 31. Changing History: DANGER, WILL ROBINSON! ● A total history rewrite: – git rebase -i <commit ID> ● Can reorder commits ● Can edit commits ● Can "squash" one commit into another ● May have merge conflicts – edit files, resolve conflicts surrounded by <<<< and >>>> – git add files, git rebase --continue
  • 32. git rebase -i --dontscrewme ● No such command ● git rebase -i the safe way: – git checkout -b master-rebase – use `git rebase -i` to move one patch – resolve any merge conflicts – squash that patch using `git rebase -i` – git diff master master-rebase – git branch -M master master-old – git branch -M master-rebase master
  • 34. Git Hooks ● Hooks are scripts found in .git/hooks/ ● Enable them with chmod a+x <file> ● Triggered by various git commands – e.g. git commit, git push – pre-commit, post-update ● Examples – shipped pre-commit hook checks for white space at the end of line, long lines, etc. – Checking for swear words?
  • 35. Git Hooks ● Example post-update hook on remote repo: #!/bin/sh cd /home/sarah/blog unset GIT_DIR git-fetch origin git-reset --hard origin/master ● Whenever I push to the remote repository, this goes into the server's checkout of my blog git repo and updates it unconditionally.
  • 36. Setting up a remote repository
  • 37. Setting up a remote repository ● Server needs git and sshd installed to use git+ssh to push to your repo ● Server needs webDAV installed to allow push to your repo over https ● http://github.com/ will host your repo ● Next directions assume you have your own server with git installed
  • 38. Setting up a remote repository 1. Make local repo, commit stuff, etc. 2. ssh to the server: GIT_DIR=/path/to/repo git init --shared 3. Next, tell the local repo about the server: git remote add origin git+ssh://hostname/path/to/repo 4. Push to the server from the local repo: git push origin master 5. Clean up the local repo so that you can pull from the remote server: git config branch.master.remote origin git config branch.master.merge refs/heads/master
  • 39. Resources ● Git work flow diagrams: http://osteele.com/archives/2008/05/my-git-workflow ● The Tangled Working Copy: http://tomayko.com/writings/the-thing-about-git ● http://github.com/ ● Kernel module examples at http://lwn.net/Kernel/LDD3/ ● vim git-commit script will display commit messages in a more useful manner. Script kept at vim.sourceforge.net. – sudo aptitude install vim-scripts vim-addon-manager – vim-addons install git-commit
  • 40. Creative Commons Image Attributions ● GIT picture: http://flickr.com/photos/29862082@N06/2908889599/ ● Snowflake: http://commons.wikimedia.org/wiki/Image:SnowflakesWilson Bentley.jpg ● Danger:http://flickr.com/photos/dawvon/32305882/ ● Cat: http://flickr.com/photos/jamilsoni/118499378/ ● Remote: http://flickr.com/photos/markkelley/957631507/ ● Philosophy: http://flickr.com/photos/paullew/2442045767/ ● Branches: http://flickr.com/photos/shapeshift/136184752/ ● Interacting with other people: http://www.flickr.com/photos/exlibris/3222440467/
  • 41. Creative Commons Image Attributions ● Front stage: http://flickr.com/photos/69108241@N00/118040089/ ● Back stage: http://flickr.com/photos/piotramigo/2561391320/ ● Hooks:http://www.flickr.com/photos/yabanji/3175297773/ ● Blame: http://flickr.com/photos/iandesign/1205496024/ ● Ballet: http://www.flickr.com/photos/oudeschool/3553416511/ ● Papyrus: http://flickr.com/photos/charlestilford/2548991271/
  • 42. Thank you! ● Sarah Sharp ● @sarahsharp ● http://sarah.thesharps.us