patterns & practices Symposium 2013
Introducing Git version control into your team
Mark [email protected]
@mgroves84
Symposium 2013
Agenda Introduction What is Git? Git 101 Enabling Team Development Short vs. Long Lived Branches Deploying with Git Your Org uses TFS? Tools/Resources
WHO AM I?Mark GrovesPrincipal Program ManagerDeveloper Division
History
HistoryCreated by Linus Torvalds for work on the Linux kernel ~2005
HistoryCreated by Linus Torvalds for work on the Linux kernel ~2005Some of the companies that use git:
What is Git?
Git is aDistributedVersion Control System
OR
Git is a
DirectoryContent Management System
Git is a
history storage system
Tree
Git is a
content trackerStupid
How ever you think about it…
How ever you think about it…
Git is SUPER cool
DistributedEveryone has the complete history
Distributed
Everything is done offline
…except push/pull
Everyone has the complete history
Distributed
Everything is done offlineEveryone has the complete history
No central authority
…except by convention
Distributed
Everything is done offlineEveryone has the complete history
No central authorityChanges can be shared without a server
Centralized VC vs. Distributed VC
Central Server Remote Server
Branching
BranchingForget what you know from Central VC(…TFS, SVN, Perforce...)
BranchingForget what you know from Central VCGit branch is “Sticky Note” on a graph node
BranchingForget what you know from Central VCGit branch is “Sticky Note” on a graph nodeAll branch work takes place within the same folder within your file system.
BranchingForget what you know from Central VCGit branch is “Sticky Note” on the graphAll branch work takes place within the same folder within your file system. When you switch branches you are moving the “Sticky Note”
InitializationC:\> mkdir CoolProjectC:\> cd CoolProjectC:\CoolProject > git initInitialized empty Git repository in C:/CoolProject/.gitC:\CoolProject > notepad README.txtC:\CoolProject > git add .C:\CoolProject > git commit -m 'my first commit'[master (root-commit) 7106a52] my first commit 1 file changed, 1 insertion(+) create mode 100644 README.txt
Branches Illustrated
masterA
> git commit –m ‘my first commit’
Branches Illustrated
master
> git commit (x2)
A B C
Branches Illustrated
bug123
master
> git checkout –b bug123
A B C
Branches Illustrated
master
> git commit (x2)
A B CD E
bug123
Branches Illustrated
master
> git checkout master
A B CD E
bug123
Branches Illustrated
bug123
master
> git merge bug123
A B C D E
Branches Illustrated
master
> git branch -d bug123
A B C D E
Branches Illustrated
masterA B C D E
F Gbug45
6
Branches Illustrated
masterA B C D E
F Gbug45
6> git checkout master
Branches Illustrated
masterA B C D E
F G
> git merge bug456
H
bug456
Branches Illustrated
masterA B C D E
F G
> git branch -d bug456
H
Branches Illustrated
masterA B C D E
F Gbug45
6
Branches Illustrated
masterA B C D E
> git rebase master
F’ G’ bug45
6
Branches Illustrated
masterA B C D E
> git checkout master> git merge bug456
F’ G’ bug45
6
Branching Review
Branching ReviewQuick and Easy to create ‘Feature’ Branches
Branching Review
Local branches are very powerfulQuick and Easy to create ‘Feature’ Branches
Branching Review
Local branches are very powerfulQuick and Easy to create ‘Feature’ BranchesRebase is not scary
Software is a Team Sport
Sharing commits
My Local Repo
Tom’s Repo
Tracey’s Repo
Matt’s Repo
A B C
A B C A B C
A B C
Adding a Remote
Sharing commits
My Local Repo
Tom’s Repo
Tracey’s Repo
Matt’s Repo
A B C
A B C A B C
A B C
Remote RepoA B C
D
D
D
D
D
Setting up a Remote
Setting up a RemoteAdding a remote to an existing local repoC:\CoolProject > git remote add origin
https://git01.codeplex.com/coolprojectC:\CoolProject > git remote -vorigin https://git01.codeplex.com/coolproject (fetch)origin https://git01.codeplex.com/coolproject (push)
Setting up a RemoteClone will auto setup the remote
C:\> git clone https://git01.codeplex.com/coolprojectCloning into 'coolproject'...remote: Counting objects: 3, done.remote: Total 3 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (3/3), done.C:\> cd .\coolprojectC:\CoolProject> git remote -vorigin https://git01.codeplex.com/coolproject (fetch)origin https://git01.codeplex.com/coolproject (push)
Setting up a RemoteName remotes what you want
Setting up a RemoteName remotes what you wantOrigin is only a convention
Branches Illustrated
Amaster
B C D Ebug12
3
Branches Illustrated
Amaster
origin/master
B C D Ebug12
3
Branches Illustrated
AB C D E
master
bug123
origin/master
Branches Illustrated
AB C D E
master
bug123
F G
origin/master
origin/master
Branches Illustrated
AB C D E
master
bug123
> git checkout master
origin/master
Branches Illustrated
AB C D E
master
bug123
F G
> git pull origin
origin/master
Pull = Fetch + MergeFetch - updates your local copy of the remote branch
Pull essentially does a fetch and then runs the merge in one step.
Branches Illustrated
AB C D E
master
bug123
F G
origin/master
Branches Illustrated
AB C D E
master
bug123
F G
> git checkout bug123
origin/master
Branches Illustrated
AB’
C’
D’
E’
master
bug123
F G
> git rebase master
origin/master
Branches Illustrated
AB’
C’
D’
E’
master
bug123
F G
> git checkout master
origin/master
Branches Illustrated
Amaster
bug123
F G
> git merge bug123
B’
C’
D’
E’
origin/master
Branches Illustrated
Amaster
F G
> git push origin
B’
C’
D’
E’ bug12
3
origin/master
PushPushes your changes upstream
Git will reject pushes if newer changes exist on remote.
Good practice: Pull then Push
Branches Illustrated
Amaster
F G B’
C’
D’
E’ bug12
3
origin/master
Branches Illustrated
Amaster
F G
> git branch -d bug123
B’
C’
D’
E’
origin/master
Adding a Remote ReviewAdding a remote makes it easy to share
Pulling from the remote often helps keep you up to date
Short vs. Long-Lived BranchesLocal branches are short lived
Short vs. Long-Lived BranchesLocal branches are short livedStaying off master keeps merges simple
Short vs. Long-Lived BranchesLocal branches are short livedStaying off master keeps merges simpleEnables working on several changes at once
Short vs. Long-Lived BranchesLocal branches are short livedStaying off master keeps merges simpleEnables working on several changes at onceCreate
Commit
Merge
Delete
Short vs. Long-Lived BranchesGreat for multi-version work
Short vs. Long-Lived BranchesGreat for multi-version workFollow same rules as Master
Short vs. Long-Lived BranchesGreat for multi-version workFollow same rules as Master…Story branches
Short vs. Long-Lived BranchesGreat for multi-version workFollow same rules as Master…Story branchesIntegrate frequently
Short vs. Long-Lived BranchesGreat for multi-version workFollow same rules as Master…Story branchesIntegrate frequentlyPushed to Remotes
Branches Illustrated
Emaster
origin/master
Branches Illustrated
Emaster
origin/master
develop
> git branch develop
Branches Illustrated
Emaster
origin/master
develop
> git push origin develop
origin/develop
Branches Illustrated
Emaster
origin/master
develop
> git checkout develop
origin/develop
Branches Illustrated
Emaster
origin/master
F Gdevelo
p origin/develop
Branches Illustrated
Emaster
origin/master
F Gdevelo
porigin/develop
> git pull origin develop
Branches Illustrated
Emaster
origin/master
F Gdevelo
porigin/develop
> git checkout –b ideaidea
Branches Illustrated
Emaster
origin/master
F Gdevelo
porigin/develop
> git commitidea
H
Branches Illustrated
E
origin/master
F Gorigin/
develop idea
HI
master
develop
Branches Illustrated
E
origin/master
F Gorigin/
develop idea
H
master
develop
> git pull (at least daily)
I
Branches Illustrated
E
origin/master
F Gorigin/
develop idea
H
master
> git checkout develop
I
develop
Branches Illustrated
E
origin/master
F Gorigin/
develop idea
H
master
> git merge idea (fast forward merge)
I
develop
Branches Illustrated
E
origin/master
F Gorigin/
develop
H
master
> git branch –d idea
I
develop
Branches Illustrated
E
origin/master
F Gorigin/
develop
H
master
> git push origin develop
I
develop
Merge Flow vs. Rebase Flow
E
origin/master
F Gorigin/
develop
H
master
> git push origin develop
I
develop
Branches Illustrated – Merge Flow
E
origin/master
F Gorigin/
develop
H
master
> git checkout master
I
develop
Branches Illustrated – Merge Flow
E
origin/master
F Gorigin/
develop
H
master
> git merge develop
I
develop
J
Branches Illustrated – Merge Flow
E
origin/master
F Gorigin/
develop
H
master
> git push origin
I
develop
J
Branches Illustrated – Rebase Flow
E
origin/master
F Gorigin/
develop
H
master
> git checkout master
I
develop
Branches Illustrated – Rebase Flow
E
origin/master
F Gorigin/
develop
Hmaster
> git rebase develop
I’develo
p
I
Branches Illustrated – Rebase Flow
E
origin/master
F Gorigin/
develop
Hmaster
> git push origin
I’develo
p
Rebase FlowE
origin/master
F Gorigin/
develop
Hmaster
I’develo
p
E
origin/master
F Gorigin/
develop
H
masterI
develop
J
Merge Flow
Short vs. Long-Lived BranchesGreat for multi-version workFollow same rules as Master
…use Story branchesDefine your conventions
What branches do you want to share?
Branch per environment?
Deploying with Git
Deploying with GitDeveloper “FTP”
Deploying with GitDeveloper “FTP”Additional Branches pointing at:
Deploying with GitDeveloper FTPAdditional Branches pointing at:
Test, Staging , Production
Deploying with GitDeveloper FTPAdditional Branches pointing at:
Test, Staging , ProductionPost Commit Hooks Automate deployments
Cloud Providers – Git SupportAppHarborHerokuNodejitsuWindows Azure… to name a few
Simple Azure Deploy
C:\CoolProject > git remote add azure https://[email protected]/coolproject.gitC:\CoolProject > git remote -vazure https://[email protected]/coolproject.git (fetch)azure https://[email protected]/coolproject.git (push)origin https://git01.codeplex.com/coolproject (fetch)origin https://git01.codeplex.com/coolproject (push)C:\CoolProject > git push azure masterCounting objects: 3, done.Writing objects: 100% (3/3), 226 bytes, done.Total 3 (delta 0), reused 0 (delta 0)remote: New deployment received.remote: Updating branch 'master'.remote: Updating submodules.remote: Preparing deployment for commit id '7106a52771'.remote: Preparing files for deployment.remote: Deployment successful.To https://[email protected]/coolproject.git * [new branch] master -> master
Git DeploymentSimple workflow
Git DeploymentSimple workflowAdd Hooks to deploy on Commit
Git DeploymentSimple workflowAdd Hooks to deploy on CommitCan get more advanced
Git DeploymentSimple workflowAdd Hooks to deploy on CommitCan get more advancedAdd Build machines, push on success
Your Org uses TFS?
Your Org uses TFS? Sure Use Git-TFLocal workflow with Git
Your Org uses TFS? Sure Use Git-TFLocal workflow with GitPush to TFS as a Remote
Your Org uses TFS? Sure Use Git-TFLocal workflow with GitPush to TFS as a RemoteMulti-Platform and Open Source
Your Org uses TFS? Sure Use Git-TFLocal workflow with GitPush to TFS as a RemoteMulti-Platform and Open Sourcehttp://gittf.codeplex.com
Individual Developer WorkflowC:\CoolProject > git tf clone http://myserver:8080/tfs $/TeamProjectA/Main Make changes to the file in the Git repoC:\CoolProject > git commit -a -m "commit one" (commit changes locally) Make more changesC:\CoolProject > git commit -a -m "commit two"C:\CoolProject > git tf pull --rebaseC:\CoolProject > git tf checkin
Git-TF for larger teamsTFS
Tom’s Repo
Tracey’s Repo
Matt’s Repo
A B C A B C A B C
Shared Git RepoA B C
git tf clone
git push
git clone git clone
Git-TFLocal workflow with GitPush to TFS as a RemoteMulti-Platform and Open Sourcehttp://gittf.codeplex.com
http://Git-SCM.com
Tools / ResourcesPro Git (Book) http://www.git-scm.com/book
TortoiseGit (with TortoiseMerge) http://code.google.com/p/tortoisegitMsysgit (includes git-bash) http://code.google.com/p/msysgit
Posh-Git (for PowerShell users) http://github.com/dahlbyk/posh-git
GitScc (Visual Studio integration) http://gitscc.codeplex.com/
Windows Git Credential Store http://gitcredentialstore.codeplex.com/
GitHub for Windows http://windows.github.com/