Nicola Chiapolini, January 26, 2015 1 / 36 git Tutorial Nicola Chiapolini Physik-Institut University of Zurich January 26, 2015 Based on talk by Emanuele Olivetti https://github.com/emanuele/introduction_to_git.git This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 License.
Nicola Chiapolini, January 26, 2015 2 / 36 Motivation to use Version Control Problem 1 Help! my code worked yesterday, but I can t recall what I changed. track modifications access old version Problem 2 We would like to work together, but we don t know how! concurrent editing merging development versions
Nicola Chiapolini, January 26, 2015 3 / 36 Outline Introduction Single developer + local repository Demo/Exercise: Single+Local Multiple developers + remote central repository Demo/Exercise: Multi+Remote/Shared Behind the Scenes
Nicola Chiapolini, January 26, 2015 3 / 36 Outline Introduction Single developer + local repository Demo/Exercise: Single+Local Multiple developers + remote central repository Demo/Exercise: Multi+Remote/Shared Behind the Scenes
Nicola Chiapolini, January 26, 2015 4 / 36 Survey: Version Control Q1: Have you heard about version control? Q2: Do you use a version control software (cvs, svn, hg, bzr, git)? Q3: How much experience do you have with git?
Nicola Chiapolini, January 26, 2015 4 / 36 Survey: Version Control Q1: Have you heard about version control? Q2: Do you use a version control software (cvs, svn, hg, bzr, git)? Q3: How much experience do you have with git?
Nicola Chiapolini, January 26, 2015 4 / 36 Survey: Version Control Q1: Have you heard about version control? Q2: Do you use a version control software (cvs, svn, hg, bzr, git)? Q3: How much experience do you have with git?
Nicola Chiapolini, January 26, 2015 5 / 36 Uses for git Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. Git Website checkpoints/backups/releases document developer effort collaboration across the globe for anything that s text code thesis/papers system config files (vcsh)...and everything else ( "gitify your life", git-annex )
Nicola Chiapolini, January 26, 2015 6 / 36 Version Control: Local Computer Checkout file Version Database Version 3 Version 2 Version 1 checkout working directory version database repository
Nicola Chiapolini, January 26, 2015 7 / 36 Version Control: Central Computer A Checkout file Server Version Database Version 3 Computer B Checkout file Version 2 Version 1
Nicola Chiapolini, January 26, 2015 8 / 36 Version Control: Distributed Computer A Checkout Version Database file Version 3 Version 2 Version 1 Computer B Checkout Version Database file Version 3 Server Version Database Version 3 Version 2 Version 1 Version 2 Version 1
Nicola Chiapolini, January 26, 2015 9 / 36 git: Help usage: git [OPTIONS] COMMAND [ARGS] The most commonly used git commands are: add Add file contents to the index commit Record changes to the repository diff Show changes between commits, commit and working tree, etc... git help <command> git help <concept> git status
Nicola Chiapolini, January 26, 2015 10 / 36 git: Introduce yourself git config --global user.name "Nicola Chiapolini" git config --global user.email "nchiapol@physik.uzh.ch"
Nicola Chiapolini, January 26, 2015 11 / 36 Outline Introduction Single developer + local repository Demo/Exercise: Single+Local Multiple developers + remote central repository Demo/Exercise: Multi+Remote/Shared Behind the Scenes
Nicola Chiapolini, January 26, 2015 12 / 36 Single+Local git: Init git init Creates an empty git repository. Creates the git directory:.git/ working directory staging area does not change your files
Nicola Chiapolini, January 26, 2015 12 / 36 Single+Local git: Init git init Creates an empty git repository. Creates the git directory:.git/ working directory staging area does not change your files
Nicola Chiapolini, January 26, 2015 13 / 36 Single+Local git: Add git add file1 [file2...] Adds new files for next commit Adds content from working dir for next commit DOES NOT add info on file permissions other than exec/noexec DOES NOT add directories per se. working directory staging area git add
Nicola Chiapolini, January 26, 2015 14 / 36 Single+Local git: Commit git commit [-m "Commit message."] Records changes from the staging area to. working directory staging area git commit
Nicola Chiapolini, January 26, 2015 15 / 36 Single+Local git: Direct Commit git commit file1 file2 [-m "Commit message."] Records all changes of file1, file2 from working dir and staging area to. working directory staging area git commit <filename> git commit -a[m "Commit message."] Records all changes in working dir and staging area. Be Careful!
Nicola Chiapolini, January 26, 2015 16 / 36 Single+Local git: Diff git diff [filename...] Shows changes between working directory and staging area working directory staging area git diff
Nicola Chiapolini, January 26, 2015 17 / 36 Single+Local git: Diff Staged How do I see what is staged? git diff --staged shows differences between staging area and last commit. working directory staging area git diff --staged
Nicola Chiapolini, January 26, 2015 18 / 36 Single+Local git: Logs Shows details of the commits. git log [--oneline] working directory staging area git log
Nicola Chiapolini, January 26, 2015 19 / 36 Single+Local git: Graphic Logs GUI to browse the git repository. gitk / gitg
Nicola Chiapolini, January 26, 2015 20 / 36 Single+Local git: Changing Version git checkout <file commit> working directory staging area git checkout <file>
Nicola Chiapolini, January 26, 2015 20 / 36 Single+Local git: Changing Version git checkout <file commit> working directory staging area git checkout <commit>
Nicola Chiapolini, January 26, 2015 21 / 36 Single+Local git: (Re)move. Warning: whenever you want to remove, move or rename a tracked file use git: git rm <filename> git mv <oldname> <newname> Remember to commit these changes! git commit -m "File (re)moved."
Nicola Chiapolini, January 26, 2015 22 / 36 Outline Introduction Single developer + local repository Demo/Exercise: Single+Local Multiple developers + remote central repository Demo/Exercise: Multi+Remote/Shared Behind the Scenes
Nicola Chiapolini, January 26, 2015 23 / 36 Outline Introduction Single developer + local repository Demo/Exercise: Single+Local Multiple developers + remote central repository Demo/Exercise: Multi+Remote/Shared Behind the Scenes
Nicola Chiapolini, January 26, 2015 24 / 36 multi+remote/shared git: Setup Computer A Checkout Version Database file Version 3 Version 2 Version 1 Computer B Checkout Version Database file Version 3 Server Version Database Version 3 Version 2 Version 1 Version 2 Version 1
Nicola Chiapolini, January 26, 2015 25 / 36 multi+remote/shared git: Clone git clone <URL> Creates two local copies of the whole remote repository. Remote (Server) Version Database Hint git remote -v shows name and URL of the remote repository.
Nicola Chiapolini, January 26, 2015 25 / 36 multi+remote/shared git: Clone git clone <URL> Creates two local copies of the whole remote repository. Local (Computer A) Remote (Server) working directory staging area remote/ origin/ git clone <url> Checkout Version Database Version Database Hint git remote -v shows name and URL of the remote repository.
Nicola Chiapolini, January 26, 2015 26 / 36 multi+remote/shared git: Commands Local Remote working directory staging area remote/ origin/ git add git commit git push git fetch git checkout git merge
Nicola Chiapolini, January 26, 2015 27 / 36 multi+remote/shared git: Fetch git fetch Updates origin from remote local, staging area and working dir not changed Local Remote working directory staging area remote/ origin/ git fetch
Nicola Chiapolini, January 26, 2015 28 / 36 multi+remote/shared git: Merge git merge combines changes from both sources Warning: can generate conflicts! Local Remote working directory staging area remote/ origin/ git merge git fetch + git merge = git pull
Nicola Chiapolini, January 26, 2015 29 / 36 multi+remote/shared git: Conflicts Conflict!... <<<<<<< yours:sample.txt Conflict resolution is hard; let's go shopping. ======= Git makes conflict resolution easy. >>>>>>> theirs:sample.txt...
Nicola Chiapolini, January 26, 2015 30 / 36 multi+remote/shared git: Resolving Conflicts 1. See where conflicts are: git diff 2. Edit conflicting lines. 3. Add changes to the staging area: git add file1 [...] 4. Commit changes: git commit -m "Conflicts solved."
Nicola Chiapolini, January 26, 2015 31 / 36 multi+remote/shared git: Push git push Updates remote. Requires fetch+merge first. Local Remote working directory staging area remote/ origin/ git push
Nicola Chiapolini, January 26, 2015 32 / 36 Outline Introduction Single developer + local repository Demo/Exercise: Single+Local Multiple developers + remote central repository Demo/Exercise: Multi+Remote/Shared Behind the Scenes
Nicola Chiapolini, January 26, 2015 33 / 36 Setting up a remote+shared repository. Share repository via ssh On remote server create bare+shared repository: mkdir newproject set up proper group permissions: chmod g+rws newproject cd newproject git --bare init --shared=group Everybody clones: git clone ssh://remote.com/path/newproject
Nicola Chiapolini, January 26, 2015 34 / 36 Outline Introduction Single developer + local repository Demo/Exercise: Single+Local Multiple developers + remote central repository Demo/Exercise: Multi+Remote/Shared Behind the Scenes
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Setup git init; git add [...]; git commit -m "A: init" a working dir staging area
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Setup git init; git add [...]; git commit -m "A: init" a HEAD working dir staging area
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Setup git commit -am "B" a b HEAD working dir staging area
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Setup git commit -am "C" a b c HEAD working dir staging area
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Branches git branch devel devel a b c HEAD working dir staging area devel
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Branches git checkout devel devel HEAD a b c working dir staging area devel
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Branches git commit -am "D" devel HEAD a b c d working dir staging area devel
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Branches git commit -am "E" devel HEAD d e a b c working dir staging area devel
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Branches git checkout devel d e a b c HEAD working dir staging area devel
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Branches git commit -am "F" devel a b c d e f HEAD working dir staging area devel
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Branches git merge devel devel a b c d e f g HEAD working dir staging area devel
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Setup git commit -am "C" a b c HEAD working dir staging area
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Tags git tag v1.0 a b c v1.0 HEAD working dir staging area
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Tags git commit -am "H" a b c v1.0 h HEAD working dir staging area
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Detached HEAD git checkout b HEAD a b c v1.0 h working dir staging area
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Detached HEAD git commit -am "J" HEAD j a b c v1.0 h working dir staging area
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Detached HEAD git commit -am "K" HEAD j k a b c v1.0 h working dir staging area
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Detached HEAD git checkout j k a b c v1.0 h HEAD working dir staging area
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Detached HEAD git commit -am "K" HEAD j k a b c v1.0 h working dir staging area
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Detached HEAD git checkout -b devel devel HEAD j k a b c v1.0 h working dir staging area devel
Nicola Chiapolini, January 26, 2015 35 / 36 Behind the Scenes: Detached HEAD git checkout devel j k a b c v1.0 h HEAD working dir staging area devel
Nicola Chiapolini, January 26, 2015 36 / 36 Questions? Understanding how git works: git foundations, by Matthew Brett: http://matthew-brett.github.com/pydagogue/foundation.html The git parable, by Tom Preston-Werner: http: //tom.preston-werner.com/2009/05/19/the-git-parable.html Excellent guides: Pro Git book: http://git-scm.com/book (FREE) git magic: http://www-cs-students.stanford.edu/~blynn/gitmagic/
Nicola Chiapolini, January 26, 2015 1 / 1 Behind the Scenes: Rebase git checkout devel devel HEAD a b c d e f working dir staging area devel
Nicola Chiapolini, January 26, 2015 1 / 1 Behind the Scenes: Rebase git rebase devel HEAD a b c f d e working dir staging area devel