gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of...
Transcript of gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of...
![Page 1: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/1.jpg)
git
![Page 2: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/2.jpg)
What is git?• Distributed version control system (DVCS).
• Version control = history of development
• Distributed = everyone has a copy of history
• Written by Linus Torvalds for managing the Linux Kernel source.
• A way of talking about revisions in tree form.
![Page 3: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/3.jpg)
git for scientists• in addition to the article than the one I sent out.
online git tutorial• https://try.github.io
![Page 4: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/4.jpg)
git for computer scientists
• git stores “blobs” of data
• trees are directories of blobs, can point to subtrees (subdirectories)
• commits point to trees and other commits.
![Page 5: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/5.jpg)
![Page 6: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/6.jpg)
Separate commits with shared trunk = branches
![Page 7: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/7.jpg)
Four locations• Working directory: the files you work on.
• The Index: changes that are going to be added to local repository.
• Local Repository: revision history on your machine.
• Remote repository: revision history on another machine.
![Page 8: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/8.jpg)
![Page 9: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/9.jpg)
Each of the four places might have a different version! (And repos might have multiple versions!)
![Page 10: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/10.jpg)
Local vs. Remote• Two graphs of changes.
• Shared history? Shared trunk in the graph.
• “Repo” = repository.
![Page 11: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/11.jpg)
git init• Get a directory ready for being a git repository.
git clone <URL>• Clone a remote repository.
• Including branches (more on branches later)
![Page 12: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/12.jpg)
Working alone• “git init” a local repository
• While True: HACK HACK HACK git add files git commit -m “hack iteration N”
• I can go back in time, in case I make a mistake, to remember what I was doing, …
![Page 13: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/13.jpg)
git add <file>• Prepare <file> to be committed.
• Copies <file> to the Index (Index is local-only).
git commit -m “<Message>”• Everything in the Index -> Local Repo
• <Message> describes what was done.
![Page 14: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/14.jpg)
Most basic git usage
![Page 15: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/15.jpg)
master
• The default name of the initial branch.
![Page 16: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/16.jpg)
branches
• branches are just names for certain points in the revision history.
• The revision history on branches can diverge, which is where they get their name
![Page 17: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/17.jpg)
branches
Three branches.
![Page 18: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/18.jpg)
git branch <branch_name>• Create a new branch called <branch_name>, from
the current HEAD.
• HEAD = special name for the current active branch.
• Does not switch HEAD to new branch.
git checkout <branch_name>
• Switch HEAD to new branch, and update working directory.
![Page 19: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/19.jpg)
HEAD points to master.
If we run git checkout very_nice_feature…
HEAD
![Page 20: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/20.jpg)
HEAD points to very_nice_feature, and the Working Directory will have whatever files match that branch.
HEAD
![Page 21: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/21.jpg)
• Create and move HEAD to new branch.
git checkout -b <branch_name>
![Page 22: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/22.jpg)
branches
Branches can be merged.
![Page 23: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/23.jpg)
Using topic branches• Find a bug • git checkout -b fix_a_bug• Loop:
• hack hack hack • git add <modified files>• git commit -m “fix step #N”• test
• git checkout master• git merge fix_a_bug
![Page 24: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/24.jpg)
![Page 25: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/25.jpg)
git merge <branch>
• Merges history of current HEAD and <branch>
• “Fast-forward” = no changes on HEAD since <branch> diverged.
• If not “Fast forward” -> might run into merge conflicts (beyond the scope of this tutorial).
![Page 26: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/26.jpg)
Sharing your work
• Ready to put your work online?
• Create an online repo (e.g., on github)
• git remote add origin <URL> (once)
• git push origin master (each time you update)
![Page 27: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/27.jpg)
git remote• List your remotes (often only origin)
git remote add <name> <URL>
• Add a new remote (with a name, at URL)
![Page 28: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/28.jpg)
• Take any changes I’ve committed, and push them to a remote on that branch.
• Must be a fast forward.
git push <remote> <branch>
git fetch <remote>• Copy any changes from <remote> to my local
repository, but do not change any of my work.
![Page 29: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/29.jpg)
git merge <remote>/<branch>
• Merge changes from a remote branch (previously fetched) into current HEAD.
git pull <remote>/<branch>• combined fetch & merge.
• I prefer separate fetch & merge.
![Page 30: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/30.jpg)
git diff• Diffs between working directory and Index.
git diff <commit>• Diffs between working directory and <commit>.
git diff <c1> <c2>• Diffs between two commits.
![Page 31: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/31.jpg)
Safe fetch & merge
• git fetch origin
• git diff origin/master
• ok? git merge origin/master
• not ok? … something else… (often commit, then merge anyway, and deal with problems)
![Page 32: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/32.jpg)
Dealing with merge conflicts
• Basically, edit files with conflicts (list them with git status), fix the conflicts, then git add those files. git commit when done (no message).
• Don’t panic.
• Search Google.
• Ask a TF.
![Page 33: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/33.jpg)
gitk
• Useful tool, especially for seeing all the branches.
• gitk --all
![Page 34: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on](https://reader034.fdocuments.net/reader034/viewer/2022042212/5eb5390803fc53386264badd/html5/thumbnails/34.jpg)
Questions?