Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro To Git

Intro To Git

An introduction to the version control system git. Given as the final tech talk leading up to HackRU in Fall 2013.

Avatar for Josh Matthews

Josh Matthews

October 11, 2013

Other Decks in Technology

Transcript

  1. “Version Control”? • Tracks changes made to a project. •

    Allows reverting to previous states. • Easier Collaboration between members / teams. • Keep code backed up.
  2. Early Version Control Systems • Per-file. • Only 1 user

    could edit a file at a time (locking). • No concept of remotes / central servers. • RCS, SCCS
  3. Centralized Version Control • Code base stored on central server.

    • Easier collaboration: developers can edit code locally and commit changes back to the central server for others to see. • Clients must connect to the server to check out revisions or create new ones. • Single point of failure. • CVS (1990), SVN (2000)
  4. Decentralized Version Control Systems • No central server ◦ Each

    client gets a full copy of the repository. ◦ Can make commits without a network connection. ▪ Slightly complicates revision ordering. • Git, Mercurial, Bazaar
  5. Git Basics $ git ➢ init - create new repository

    ➢ add - add changes to staging area ➢ commit - create a new commit with staged changes ➢ log - display log of commits ➢ checkout - checkout a specific commit ➢ status - info on current repository state
  6. Working With Remotes • A remote is just another computer

    with a copy of the repository. • Usually work with one “central” remote called “origin”. $ git ➢ clone - download a copy of a remote repository for the first time ➢ fetch - retrieve new changes from a remote ➢ pull - retrieve changes and update your local copy to match them ➢ push - upload your changes to the remote
  7. Github • Allows for easy creation of new projects and

    collaboration between team members. • Issue tracking, “social coding” • Unlimited public repositories for free. • 5 free private repos for 2 years for students. ◦ https://github.com/edu
  8. First Time Github Setup 1. Sign up for an account:

    http://github. com/signup 2. Generate and upload an SSH key: a. $ ssh-keygen b. Sign into github and go to https://github. com/settings/ssh c. Click “Add SSH key” and copy the contents of ~/.ssh/id_rsa.pub into the box. 3. Create your first repository with the “New repository” button on the home page.
  9. Merge Conflicts • Happens when multiple developers commit code out

    of sync. ◦ Also when working with branches. • Git creates a file with both versions side by side: <<<<<<< HEAD:mergetest This is my third line ======= This is a fourth line I am adding >>>>>>> 4e2b407f501b68f8588aa645acafffa0224b9b78:mergetest
  10. Resolving Merge Conflicts 1. See which files have conflicts with

    git status 2. Edit the files to merge the two sets of changes, and remove the lines added by git (“<<<<<<<”, “=======”, “>>>>>>>>”). 3. Add the files and create a new commit. 4. Push these changes back to the remote.
  11. The Graph Git stores your project history in a Directed

    Acyclic Graph (DAG). It can be visualized like this: A branch is just a pointer to a node in the graph. In this case we just have the default branch, “master”.
  12. Branching We can create additional branches and commit on them

    (git branch). When you make a commit on a branch, a new node is created and the branch changed to point to it. In this case, master can be merged with iss53 via a “fast forward”; it has a direct path.
  13. Diverging Branches If you make different commits to separate branches,

    they diverge. Think of it as an alternate timeline.
  14. Diverging Branches If you make different commits to separate branches,

    they diverge. Think of it as an alternate timeline.
  15. Merging Branches We can merge branches to get the changes

    from both. When we do this git tries to automatically create a commit that combines the changes from both branches, called a merge commit. If this fails we get a merge conflict, and must resolve it to continue.
  16. Rebasing Rewrites commits to create a linear history. Don’t do

    this on branches that have been pushed to a public repo.
  17. Rebase Conflicts During a rebase, git must rewrite each commit

    on top of existing ones. This can cause multiple merge conflicts along the way. To resolve these: 1. See which files are conflicted with git status 2. Modify them as in a merge conflict. 3. Add them with git add. 4. Continue the rebase with git rebase --continue 5. Repeat until rebase completes If you want to bail out you can always run “git rebase --abort” to get back to where you were before the rebase.
  18. Rebasing For A Better History • Rebasing a topic branch

    before merging into master will cause it to be a fast-forward merge, and create a cleaner project history. • “git pull” is just shorthand for “git fetch <remote> && git merge <remote>/<branch>”. You can have it try to rebase instead with “git pull --rebase”. This will also lead to cleaner project histories.
  19. Further Reading • Pro Git: http://git-scm.com/book (free) • Pragmatic Version

    Control Using Git: http: //pragprog.com (print and ebook)