git for the ASP.NET

Paul Litwin Fred Hutchinson Cancer Research Center

[email protected] @plitwin Slides & samples can be found here…

• http://tinyurl.com/DevInt2015Oct

Litwin for the ASP.NET Programmer 2 Session Itinerary

• Why distributed ? • git basics • Command line git • Using git from VS Code • Using git from Visual Studio • Branching and merging • Wrap up

Litwin Git for the ASP.NET Programmer 3 Why distributed version control? Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency

Created by Linux creator, Linus Torvalds

Litwin Git for the ASP.NET Programmer 5 The name git?

“I'm an egotistical bastard, and I name all my projects after myself. First Linux, now git.”

Linus Torvalds quote from 2007

Litwin Git for the ASP.NET Programmer 6 Centralized Version Control One repository using a client-server model

Litwin Git for the ASP.NET Programmer 7 Distributed Version Control Many repositories using peer to peer model

Litwin Git for the ASP.NET Programmer 8 Comparing centralized (tfs) vs distributed (git)

Attribute Centralized Distributed TFS, SVN, PVCS git, Repositories 1 Many

Model Client-server Peer-to-peer

Speed of common Slower Fast against local repo operations Redundancy of system None; single point of Redundancy built in failure Offline work More difficult Easy

Merging of changes When you check When you sync changes changes in (push/pull) Litwin Git for the ASP.NET Programmer 9 git Basics git Basics: Getting git

• Download from: https://git-scm.com/

Litwin Git for the ASP.NET Programmer 11 git Basics: How it works

Working Staging git repo Directory Area

stage

check out

modified staged committed

Litwin Git for the ASP.NET Programmer 12 Command line git Using Command Line

• Windows – use command prompt – you may need to add git to the path (find git.exe and add this folder to the system path) • Control Panel|(System and Security)*|System|Advanced System Settings|Environment Variables and select Path and click Edit * Windows 8/10 only • Mac – use terminal program

Litwin Git for the ASP.NET Programmer 14 Creating a local repo

• git init – creates a local repository in current folder by creating a hidden .git subfolder

Litwin Git for the ASP.NET Programmer 15 Lifecycle of Files

• git status – displays the status of your project’s files

Litwin Git for the ASP.NET Programmer 16 Basic commands

• Add files to staging area TIP: You can create rules which automatically ignore – git add certain types of files (e.g., • Commit files to local repo DLLs, .sln, .proj) by git by using a .gitignore file. The – git commit –m “message” ignored files will not be tracked/added/committed • Unstage changes – git reset • Check status of project files – git status • Display log of commits – git log

Litwin Git for the ASP.NET Programmer 17 Status and Differences

• Check status of project files – git status • Display log of commits – git log • between unstaged (but tracked) changes and staged changes – git diff • Diff between staged and last commit – git diff --staged Litwin Git for the ASP.NET Programmer 18 Working with a remote repo

• git clone url folder – makes a copy of a remote git repository from url into the local folder – also names the remote “origin” and sets it as default remote • git remote add name url – Creates link between current repo and named remote repo

Litwin Git for the ASP.NET Programmer 19 Exchanging changes with remote

• git pull – downloads remote changes and merges them into your repo • git push – uploads local committed changes to remote – git push –u origin master • pushes content to origin remote from master branch and remembers your remote and branch settings

Litwin Git for the ASP.NET Programmer 20 Litwin Git for the ASP.NET Programmer 21 Litwin Git for the ASP.NET Programmer 22 Using git from VSCode Code and git (1 of 3)

• VS Code can… – create a local repo – add files to staging area – commit files – pull/push to remote – show diffs – view git command history • VS Code cannot… – clone a repo

Litwin Git for the ASP.NET Programmer 24 Code and git (2 of 3)

Commit with refresh message

total # of changes

Change icons A – added M – modified D – deleted

Litwin Git for the ASP.NET Programmer U – untracked 25 Code and git (3 of 3)

context sensitive actions when rolling over file name

click on file name to see diff

Litwin Git for the ASP.NET Programmer 26 Using git from Visual Studio Using git from Visual Studio 2015

• Git also works with Visual Studio 2015 (and 2013) • Unlike VS Code, Visual Studio can init & clone • One big oddity about Visual Studio and git – There is no staging of changes – You commit directly (equivalent to git commit -a)

Litwin Git for the ASP.NET Programmer 28 Connecting to a VSOnline git repo from Visual Studio 2015 (1 of 5) 1. Login to VS Online 2. From your home page, under Recent projects & teams, click New 3. At Create New Team Project dialog, enter project name and description. Select Git from the Version control drop-down and click Create Project 4. After project is created, click on View Code at annoying pop-up window

Litwin Git for the ASP.NET Programmer 29 Connecting to a VSOnline git repo from Visual Studio 2015 (2 of 5) 5. From Code Explorer page, click big button (or ) 6. Once in Visual Studio, click Open from Source Control from Start Page 7. Now, from Team Explorer window, click button on toolbar to open Connect tab

Litwin Git for the ASP.NET Programmer 30 Connecting to a VSOnline git repo from Visual Studio 2015 (3 of 5) 8. Right-click on project in visualstudio.com connection area and select Clone

Litwin Git for the ASP.NET Programmer 31 Connecting to a VSOnline git repo from Visual Studio 2015 (4 of 5) 9. Verify the path to the local repo and click Clone 10. Now create a new project in the repo by following instuct- ions at top of Connect window 11. Create new project, making sure that Add to source control checkbox is checked

Litwin Git for the ASP.NET Programmer 32 Connecting to GitHub repo (1 of 3) from Visual Studio 2015

• Step 1: – For existing GitHub repo, from Team Explorer Connect page, click Clone under GitHub – Or for new repo, from Team Explorer Connect page, click Create under GitHub

Litwin Git for the ASP.NET Programmer 33 Connecting to GitHub repo (2 of 3) from Visual Studio 2015 • Step 2: Select project from Local Git repos on Connect page of Team Explorer

Litwin Git for the ASP.NET Programmer 34 Connecting to GitHub repo (3 of 3) from Visual Studio 2015 • Step 3: If it’s a empty repo, click New under the Solutions section of the Team Explorer Home page. – If it’s an existing project, click Open instead

Litwin Git for the ASP.NET Programmer 35 Working with git from Visual Studio 2015

Litwin Git for the ASP.NET Programmer 36 Branching and merging Branching

• every git repo starts with master branch • git branch newBranch – creates branch named newBranch • git checkout newBranch – switches to branch newBranch – changes files in working directory to new branch • git checkout –b newestBranch – create & switch to newestBranch branch in 1 step

Litwin Git for the ASP.NET Programmer 38 Merging

• Two steps 1. git checkout branchToMergeTo 2. git branchToMergeFrom • Deleting a branch – git branch –d branchToDelete • Any merge conflicts will need to be resolved and the resulting changes committed

Litwin Git for the ASP.NET Programmer 39 More on branches

• git branch – no arguments; lists all branches • git branch --merged – lists all branches you have merged with • git branch --no-merged – lists all branches you have not merged with

Tip: Both Visual Studio and VS Code allow you to work with branches

Litwin Git for the ASP.NET Programmer 40 Branches & remotes

• gets a little more complicated – git fetch remote • download all history from remote but don’t integrate – git pull remote branch • download & merge remote commits into local branch – git push remote branch • send branch commits to remote branch Homework: Read chapter 3 (Git Branching) of https://git-scm.com/book/en/v2 for more on branching

Litwin Git for the ASP.NET Programmer 41 Stashing

• git stash – temporarily stashes away all tracked changes so you can switch to another branch • git stash apply – reapplies most recently stashed changes • git stash drop – deletes most recently stash • git stash pop – reapplies most recently stashed changes and deletes the stash

Litwin Git for the ASP.NET Programmer 42 Wrap Up Wrap Up

• git is a distributed source control repository that has a lot of power & a groundswell of developer community support • You can use git from – command line – VS Code – Visual Studio – other git GUI tools • I’ve only scratched the surface of git – See next slide for resources where you can learn more

Litwin Git for the ASP.NET Programmer 44 Resources

• Download git – http://git-scm.com • Various resources – https://git-scm.com/documentation • Free book -- Pro Git – https://git-scm.com/book/en/v2 • Interactive tutorial – https://try.github.io/ • Cheat sheets – http://www.git-tower.com/blog/git-cheat-sheet/ – http://ndpsoftware.com/git-cheatsheet.html – https://training.github.com/kit/downloads/github-git-cheat-sheet.pdf • Git GUI tools – Source Tree: https://www.sourcetreeapp.com/ – GitHub Desktop: https://desktop.github.com/

Litwin Git for the ASP.NET Programmer 45 Thank you!

Slides & samples can be found here…

• http://tinyurl.com/DevInt2015Oct

Litwin Git for the ASP.NET Programmer 46