17
What is Version Control? Git Structure Using Git Controlling your computer with a shell CS 2112 Lab: Version Control CS 2112 Lab: Version Control

CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

CS 2112 Lab: Version Control

CS 2112 Lab: Version Control

Page 2: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Version Control

What is Version Control?

You’re emailing your project back and forth with your partner. Anhour before the deadline, you and your partner both find differentbugs and work feverishly to correct them. When you try to submit,you find that you have two different versions of the code, and youdon’t have enough time to figure out who changed what, how tomerge them together into one final project, and what, if any, bugswere introduced along the way!

CS 2112 Lab: Version Control

Page 3: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Version Control

What is Version Control?

Version control allows multiple people to work on a projectsimultaneously by keeping versioned copies of each file in yourproject for each edit that you make. This makes it easy to:

I Revert files back to a previous state if you make a mistake.

I Look over any changes made by you or those you are workingwith.

I Recover any files if they are lost.

Distributed version control lets you store the different versions ofyour files on a remote server.

CS 2112 Lab: Version Control

Page 4: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Overview

Git!

Several widely used version control systems exist. Some of themost popular free ones are Git, Mercurial, and Subversion.Perforce is often used in industry. We will take a look at Git, whichis popular, freely available, and powerful. Subversion is simpler touse but less powerful; Mercurial is similar to Git; perhaps a littleslower. Eclipse has some support for projects that use Git, but it’ssafer to get Git at the command line.

CS 2112 Lab: Version Control

Page 5: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Overview

Setting Up a Repository

A couple of sites host free repositories, including:

I github.com (Git - though you will need to request a studentaccount for private repositories)

I bitbucket.org (Git and Mercurial)

I xp-dev.com (Subversion, Git, and Mercurial)

You will need to follow the specific instructions for whichever hostyou choose in order to initialize a Git project and add your newremote repository. (This will make more sense later!)

CS 2112 Lab: Version Control

Page 6: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Structure

The Local Repository

Your local repository contains 3 trees:

I Working Directory - This is where you will look at, modify,and add files to your project.

I Staging Area/Index - When you add a file (git add 〈file〉from the command line, or Add to Index in Eclipse), the file isadded to this second tree, the staging area. This is where youprepare the files that you would like to eventually commit toyour repository.

I Repository - When you commit the changes made to yourfiles (git commit by command line, Commit in Eclipse),changes made to the files in your staging area are added tothe repository as new versions of those files. You must specifya commit message with each commit to let others know whatyou have changed.

CS 2112 Lab: Version Control

Page 7: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Structure

The Local Repository

Figure: Local Repository Workflow

CS 2112 Lab: Version Control

Page 8: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Structure

The Remote Repository

I When you are ready for others on your team to be able to viewand pull your changes into their own local repositories, youcan push your committed changes to the remote repository.

I If anyone else has pushed changes to your remote repository,Git will make you pull those changes into your own localrepository before pushing any new changes.

I If you and your partner have been working on different partsof your code, Git will automatically merge in your partner’schanges smoothly. If you have been working on the samecode, however...

CS 2112 Lab: Version Control

Page 9: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Structure

The Remote Repository

Figure: Remote repository workflow

CS 2112 Lab: Version Control

Page 10: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Conflicts and Reverting

Merge conflicts

If you and your partner have both modified the same code, Git willbe unsure about which version you would like it to use - yours, ortheirs? To fix this, Git will ask you to resolve merge conflicts.

Figure: Eclipse Merge Conflict Tool

CS 2112 Lab: Version Control

Page 11: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Conflicts and Reverting

Merge conflicts

Once you have your Local File Workspace version of the file lookinghow you would like, you can right click on the file to mark it asmerged. You can then commit your merge, and push it as normal.

CS 2112 Lab: Version Control

Page 12: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Conflicts and Reverting

Git Workflow - Reverting a File

Figure: A Git WorkflowCS 2112 Lab: Version Control

Page 13: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Conflicts and Reverting

EGit Commands to Remember!

I Team → Add to Index — Add a file to your Index if youwant to track the changes you make in it, and commit thosechanges later.

I Team → Commit — Commit changes in your workingdirectory to your local repository; these changes will bepushed to the remote repository on the next Push command.

I Team → Synchronize Workspace — This will pull from theremote repository and make sure that your local repository isup to date (needed especially before a Push)

I Push or Team → Push Branch — Pushes all yourcommitted changes to the remote repository

CS 2112 Lab: Version Control

Page 14: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

The shell: a lower-level interface

A shell is a command-line interface to your computer’s operatingsystem. Less pretty than the GUI interface, more functional.

I Windows: cmd, Cygwin (Unix on Windows), or PowerShell

I Unix (Mac OS X, Linux): sh (bash), (t)csh

CS 2112 Lab: Version Control

Page 15: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Shell commands

A shell command consists of a program name followed by someoptiona command-line arguments. Spaces separate the commandand the arguments from each other.Examples:

I rm 〈filename〉 (Windows: del 〈filename〉) : remove a file

I ls 〈directory〉 (Windows: dir 〈directory〉): list contents ofdirectory

I echo 〈message〉 : print the message to standard output

I cat 〈filename〉 (Windows: type 〈filename〉) : print thecontents of the file.

I cd 〈directory〉 : change the current directory (shell built-incommand)

CS 2112 Lab: Version Control

Page 16: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Command context

Every command is executed with some context provided by theoperating system:

I The current directory in which the command runs. All filesaccesses are relative to this directory. Note: folders are knownas directories at the operating system level.

I A set of environment variables that can be looked up by therunning program.1

I Standard input and output devices. Normally standard inputreads from the shell’s input and output goes to the shell.However, it is possible to override these. The syntax >

〈filename〉 redirects output to the specified file.

1The ShellShock vulnerability exploits a bug in how the bash shell handlesenvironment variables.

CS 2112 Lab: Version Control

Page 17: CS 2112 Lab: Version ControlGit Structure Using GitControlling your computer with a shell Structure The Local Repository Your local repository contains 3 trees: I Working Directory

What is Version Control? Git Structure Using Git Controlling your computer with a shell

Pathnames and directories

I Files and directories are specified by pathnames: namesseparated by slashes (Unix, Cygwin) or backslashes(Windows).

I Pathnames starting with a slash (backslash) are absolute andstart from the root of the file system.

I The special directory “.” means the current directory, and“..” means the parent directory of the current directory.

I To go up a directory: cd ..I To go to the root directory: cd /I To list the current directory: ls ., or just ls.

CS 2112 Lab: Version Control