Since its inception in 2005 Git gained a huge traction and passed the borders of open source well into Microsoft developer's territory, see for example the success of Github. The Visual Studio 2012 Update 2 adds native support for Git in Visual Studio, but don't think that this will be enough to master it, as it's notoriously a though subject that scares the most consumed command line wizards.
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Git crash course for Visual Studio devs
1. itcampro@ itcamp13# Premium conference on Microsoft technologies
Git crash course for Visual
Studio devs
Alessandro Pilotti
Cloudbase Solutions
MVP ASP.Net / IIS
@alexpilotti
2. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
MobileHuge thanks to our sponsors!
3. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Why Git?
• Basics
• Visual Studio integration
• Remote repositories
• Branching
• Merging and rebasing
• Forks and github, bitbucket, etc
• Continuous deployment
• Gerrit
• Azure support
Agenda
4. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Why do we need an ENTIRE session on a VCS
instead of talking about code?
• Previous VCS are so simple:
– CVS
– Subversion
– Sourcesafe (D’oh)
• In short, it’s because there’s way more than
the usual commit / checkout thing
Why Git?
5. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Developed initially by Linus Torvalds in 2005
– Used for the Linux Kernel
– GPLv2
– Multiplatform (Linux, Windows, OSX, FreeBSD)
• Some design goals:
– Fast and efficient
– Support for non linear development
– Distributed
– Easy publishing
– Garbage collection
Git
6. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• In most cases, you just start work on a
project using it
– In my case, OpenStack
• Most OSS projects are hosted on github
– CodePlex supports Git as well
• Ease to branch, merge, fork
• Once you start using it, it’s hard to go back
Why should I use it?
7. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• http://msysgit.github.io/
– Command line
• http://windows.github.com/
• Visual Studio 2012 Update 2
• NetBeans
• Eclipse
• TortoiseGit
• etc
Windows clients
8. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• A Git repository is a directory tree containing
your project(s)
– A hidden directory “.git” contains all the Git stuff
• Start a repo from scratch:
– git init
• Cloned from an existing repo:
– git clone
https://github.com/cloudbase/cloudbase-init
Basics
9. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Files are either:
– Staged (git knows about them)
– Untracked (git doesn’t care about them)
• To stage a file / directory:
– git add path
• To unstage and delete a file:
– git rm path
• To unstage w/o deleting it:
– git rm –cached path
Staging files
10. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• As simple as:
– git commit –a –m “Don’t forget the commit
msg”
• To commit a file:
– git commit -m msg path
• Being a distributed VCS, a commit is only
local
• Do small commits, atomic and cohesive
– With a clear commit message
– A VCS is not a backup tool
Commit
11. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• You can look at the history for the current
branch with a simple:
– git log
– git log --pretty=oneline
• To see your staged (uncommitted) changes:
– diff format
– git diff
• To see the differences between two commits:
– git diff commit1 commit2
Git log, show and diff
12. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• With checkout you can get a file or a working
copy of your repository:
– E.g.: to undo your work to the latest commit:
– git checkout (entire repo)
– git checkout file (single file only)
• To checkout a file to a previous version:
– git checkout commitref file
– E.g.: to get the previous version:
• git checkout HEAD^ file
• git checkout HEAD^
checkout
13. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• git config --global user.name ”My Name”
• git config --global user.email myuser@mydomain.com
• git config --global color.ui true
• git config --global core.editor notepad++
– Note: add the editor to your path first (elevated console):
• SETX PATH “%PATH%;%ProgramFiles% (x86)Notepad++” /M
– Even better, getting used to vi is not so terrible
• git config --global core.autocrlf true
– Values: true, input, false
– Set true if you work on multiplatform projects
– See .gitattributes later for this as well
Global settings
14. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
MobileGit and Visual Studio 2012
15. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• VS 2012 Update 2 required
• Install Visual Studio Tools for Git
• Tools -> Options -> Source Control
– Choose “Microsoft Git provider”
• Open “Team Explorer”
– View -> Team Explorer
• You can use also regular git commands in
– Package Manager Console
Git and Visual Studio 2012
16. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Connect to Team Projects (socket icon)
– Clone
– Set the remote repo URL
– Specify a local path
VS 2012 Clone a repo
17. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• RC available now
• http://blogs.msdn.com/b/visualstudioalm/arc
hive/2013/05/07/release-candidate-to-visual-
studio-update-3-now-available.aspx
• Bug fixes
• Git TFS continuous integration support
VS 2012 Update 3
19. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
MobileTFS + Git
TFS supports GIT natively! http://bit.ly/Z1RkcV
Signup for a free account here:
https://tfs.app.visualstudio.com/_account/Signu
p
21. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• git contains all the info related to your repo
in “.git/”
• Your files (working copy), are simply a human
readable copy of the files
– Bare repositories have no working copy
• The Index contains info about staged files
• File contents are stored into blobs
• Other objects include trees (the filesystem
refresentation) and commits
• Every object is identified with a SHA1 hash
A bit of internals
22. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• You can refer to commits with their SHA1
hash or with a shorter unique prefix, e.g.:
– git log e46bf2165f29f579051b8e12ebe63e5512205610
– git log e46bf2
• Or you can refer to a commit using a
reference:
– every branch has a “head” identifying the last
commit
• git checkout branchname
Refs
23. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• HEAD is a special symbolic link pointing to the
head of the current branch
• You can do some “arithmetic” with refs:
– ^ points to the previous commit
– ~n points to the nth previous commit
– HEAD^^^ the 3rd commit before HEAD
– HEAD~3 (same as the above example)
• Note: use (quotes) “HEAD^” in the Windows
cmd prompt
Refs
24. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• A tag is simply a friendly name for a commit
– git tag v1.0 HEAD
• Annotated tags are stored as full objects:
– git tag –a v1.0 –m “Version 1.0” HEAD
• Don’t confuse tags with a branches
Tags
25. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Git is a distributed VCS
• You can “sync” your local copy with remote
copies anytime, by either “pull” or “push”
changes
• Git has no concept of a “server”
– Repos form a peer network
• It’s useful anyway to elect an “authoritative”
repository. E.g.:
– A repo on github, bitbucket, etc
– The project lead developer’s repo
Remote repositories
26. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• A remote is simple a name and an url
• List remotes:
– git remote
• Add a remote:
– git add origin https://github.com/openstack/nova.git
• Note: this will track all remote branches, use –t <branchname> to
limit
• Protocols to access the remote repo can be:
– Local filesystem, HTTP/HTTPS, git, rsync
– Local fileystem on Windows:
• file:///c/your/path/repo.git
Configuring remotes
27. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Remove a remote:
– git remote rm name
• Changing a remote url is a frequent
operation:
– git remote set-url <newurl> <remote_name>
• Cleaning stale remote tracking branches
– git remote prune <remote_name>
Configuring remotes
28. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• A bare repository doesn’t have a working
copy of your content
• Use it when you create an authoritative
“server” repo
• By convention bare repo folder names end it
“.git” e.g.: “myrepo.git”
• git init --bare
Bare repositories
29. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• A typical operation consists in cloning a
remote repository (will set the proper remote
as well):
– git clone <url>
• To update a local repository with remote
commits:
– git pull
• This is a shortcut for git fetch and git merge
FETCH_HEAD
Clone / pull
30. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Updating a remote repository consists in:
– git push <remote> <branch>
– git push origin master
• Note: you must first make sure that you
pulled all the remote commits first!
• More on merging / rebasing later!
Push
31. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Git was created to make branching and
merging as easy as possible.
• “master” is the default branch
• From an application lifecycle standpoint we
can define two types of branches:
– Long lived
• Typically major releases
– Short lived
• Bug fixes, dev experiments, etc
• Git makes no difference of course
Branches
32. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Creating a branch is simply:
– git chekout –b name
– Shortcut for git branch name and git checkout name
• Switch to a different branch:
– git checkout name
– This will also change the working directory contents to reflect
the branch commits!!
• To list existing branches:
– git branch
– The active branch is marked with “*”
• To remove a branch:
– git branch –d name
Branches
33. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
MobileHow does it look like?
A B
C
Dmaster
new-feature
E
F
experiment
master commits: A, B, D
new-feature commits: A, B, C, E
experiment commits : A, B, C, E, F
34. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• In most cases at some point you’d like to merge
the content of a branch into another
• For example, you develop a new feature and
once it’s done tested and reviewed you want to
merge it in the master branch
• git-merge merges a branch into the current one:
– git checkout master
– git merge new-feature
Merge
35. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
MobileHere’s how a merge looks like
A B
C
Dmaster
new-feature
E
F
experiment
G
master commits: A, B, D, C, E, G
36. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• What happens when the same file has been
changed in two branches?
• Git is very smart in applying different commit
patches to the same source when lines do
not overlap (e.g. 2 different methods)
• But sometimes you just have to handle
conflicts, it’s a hard life
Conflicts
37. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
git merge branch_name
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then
commit the result.
D’oh!
Edit the conflicting files…
git add files
git commit -a
Conflicts
38. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• You can also rollback the merge with:
– git merge –abort
• A merge happens also when you pull a remote branch
– The resolution process is the same
• Once the merge succeeds, you can delete the merged
branch if you don’t need it anymore
• You can also keep on working on the merged branch and
merge it again later
• When you merge a branch w/o conflicts (fast-forwarding)
by default a merge commit is not created. This can be
changed with git merge –no-ff etc
Merge notes
39. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Rebasing a branch consists in taking the
commits of another branch and applying
them on the current branch before any
commit added to the current branch.
• Typically this is used to bring a local branch
up to date with remote commits
• Merge vs rebase can create a bit of confusion
to users
Rebase
40. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
MobileRebase example
A B
C
Dmaster
new-feature
E
F
git checkout new-feature
git rebase master
Note: rebased commits have a new hash!
41. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
CONFLICT (content): Merge conflict in test3.txt
Failed to merge in the changes.
Patch failed at 0002 asdasdd
The copy of the patch that failed is found in:
/Users/anorex/Downloads/testgit/.git/rebase-apply/patch
When you have resolved this problem, run
"git rebase --continue".
If you prefer to skip this patch, run
"git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase -
-abort".
Rebase conflicts
• Similar to resolving a merge
42. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• What if you just one to pick a single commit
from another branch without having to
merge / rebase the entire branch?
• E.g. backporting a bug fix from v1.2 to v1.1
• git cherry-pick commit1
• git cherry-pick start..end
Cherry picking
43. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
MobileCherry-pick example
A B
C
Dmaster
new-feature
E
F
D
’
git checkout new-feature
git cherry-pick D
Note: cherry-picked commits have a new hash!
44. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Sometimes you might need to remove or reorder
commits (locally only don’t do that after a push!)
• This can be done interactively with git rebase:
• git rebase –i “HEAD~4”
• Just remove or reorder the lines in the editor:
– pick 37c449c My first commit
– pick 3982180 My second commit
– pick 6055a10 My last commit
Reordering and removing commits
45. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• There’s a good chance that your dev branch
commits will be a mess
• Git gives you a chance to clean them up before
pushing them upstream using git rebase, e.g.:
• git rebase –i HEAD~3
– pick 37c449c My first commit
– squash 3982180 My second commit
– squash 6055a10 My last commit
Squashing
46. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Changing the content of the last commit is a
frequent action:
– Typos
– Missing files
– Errors discovered after committing
• Just add --amend to the commit command:
• git commit –a --amend
Change the last commit
47. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Sometimes you might need to revert a
commit
• Removing a commit is in general a bad idea
unless the commit appear only locally, so
reverting is a good alternative
• git revert <commit>
• This action will generate a new commit
Revert
48. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• There are cases in which the only way to get
out of a messy local repository is to restore it
to a previous state
• git reset --soft <commit>
– Doesn’t modify index and working dir
• git reset --mixed <commit>
– Modifies the index but not the working dir
• git reset --hard <commit>
– Modifies the index AND the working dir
git reset
49. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• .gitignore is a special file in your repository
that contains rules for files and folders that
will not be tracked
• Example:
*.exe
*.dll
*.obj
*.pdb
.gitignore
50. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• On Window you might want to deal with
CRLF / LF on a per-project base
• in .gitattributes add:
• text eol=lf
.gitattributes
51. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Git provides support for SVN repositories as
well
• There’s quite a lot to discuss on the subject
• Just as an introduction:
• git svn clone <svn_url> <repo.git>
• git svn fetch
• git svn dcommit
Git and Subversion
52. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Hooks let you run custom code on given
events
• E.g.: for push events:
– pre-receive hook
– for each update ref:
• update hook
• update ref
– post-receive hook
– post-update hook
Hooks
53. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Hooks are scripts stored in .git/hooks/
• Each script has the name of the hook
– Must be executable (Unix)
• Parameters are passed in the command line
• On Windows, you can use Powershell
– But you need a MSYS script (sh, bash, etc)
#!/bin/bash
powershell -executionpolicy RemoteSigned -
File C:pathpost-receive.ps1 "$@" <
/dev/stdin
Writing hooks
54. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
function ProcessRef($old, $new, $ref) {
"old {0} new {1} ref {2}" -f ($old, $new, $ref) |
Out-File -append ”C:Testpost-receive.txt"
}
$lines = while(($line=[Console]::ReadLine())){$line}
foreach($line in $lines)
{
$values = $line.Split(" ")
$old = $values[0]
$new = $values[1]
$ref = $values[2]
ProcessRef $old $new $ref
}
Example Powershell post-receive
55. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Github did a tremendous job in popularizing Git,
becoming currently the “de facto” standard platform
for open source projects
• Very intuitive and rich web interface
• Fork and pull requests
• Rich APIs for continuous deployment and more
• BitBucket offers similar services, including a limited
number of free private repos
Github, Bitbucket & co.
57. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Anybody can fork a public repo
• Users with proper access can fork a private one
• In Github, forks are a good thing
• After committing code to your fork, you can
send a “pull request” to the maintainer of the
original repository
• The maintainer can review it, approve and
merge it
Fork and pull requests
58. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• A modern application lifecycle management
needs to consider a proper framework for:
– Code review
– Unit testing
– Continuous integration
• Integration system tests
– Continuous deployment
• Optionally / Ideally
• Git’s distributed nature helps a lot
Code review, unit testing and CI
59. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Gerrit is an open source code review solution
based on Git
– https://code.google.com/p/gerrit/
• Written in Java
• Multiplatform
Gerrit
61. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Git provides a very easy model to extend the
existing command set
• git review is the extension provide for Gerrit
integration
– https://pypi.python.org/pypi/git-review
– Requires Python
• Install on Windows with:
– easy_install.exe git-review
Git review workflow
62. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• git checkout -b newbranch
• git review -s
• git commit -a
• git-review
• Your commits is now available for review on the Gerrit
web site
• Jenkins and CI tools can be executed and vote
• Human review and approval occurs
• Jenkins and CI tools can be executed again
• The commits merge into the master branch
• New versions of the same patchset can be sent with:
• git commit -a --amend
• git review
Gerrit workflow
63. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
MobileGit and Azure web sites
64. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
MobileGit and Azure web sites
65. itcampro@ itcamp13# Premium conference on Microsoft technologies
Development &
Mobile
• Integrate Powershell with Git?
• http://www.hanselman.com/blog/PromptsAn
dDirectoriesEvenBetterGitAndMercurialWithP
owerShell.aspx
Posh-Git