34
git

gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge • Merges history of current HEAD and • “Fast-forward” = no changes on

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge  • Merges history of current HEAD and  • “Fast-forward” = no changes on

git

Page 2: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge  • Merges history of current HEAD and  • “Fast-forward” = no changes on

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

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

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
Page 6: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge  • Merges history of current HEAD and  • “Fast-forward” = no changes on

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

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
Page 9: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge  • Merges history of current HEAD and  • “Fast-forward” = no changes on

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

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

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

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

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

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

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

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

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

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

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

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

• 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

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

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
Page 25: gitiacs-courses.seas.harvard.edu/.../lectures/git_tutorial.pdfgit merge  • Merges history of current HEAD and  • “Fast-forward” = no changes on

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

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

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

• 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

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

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

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

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

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

Questions?