100% found this document useful (2 votes)
88 views

Introduction To Version Control With Git: Originally by Andreas Skielboe

This document provides an introduction to using version control with Git. It discusses the basics of Git including initializing repositories, tracking file changes, commits, and working with remote repositories. It also includes examples of typical workflows for setting up and using a Git repository locally and on a remote server.

Uploaded by

amitfegade121
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
88 views

Introduction To Version Control With Git: Originally by Andreas Skielboe

This document provides an introduction to using version control with Git. It discusses the basics of Git including initializing repositories, tracking file changes, commits, and working with remote repositories. It also includes examples of typical workflows for setting up and using a Git repository locally and on a remote server.

Uploaded by

amitfegade121
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Introduction to Version Control with Git

Originally by Andreas Skielboe

Dark Cosmology Centre


Niels Bohr Institute

Originally by Andreas Skielboe Git - Version Control System


License

Most images adapted from Pro Git by Scott Chacon and released under
license Creative Commons BY-NC-SA 3.0.

See http://progit.org/

Originally by Andreas Skielboe Git - Version Control System


Why Use Version Control?

A Version Control System (VCS) is an integrated fool-proof framework


for
Backup and Restore
Short and long-term undo
Tracking changes
Synchronization
Collaborating
Sandboxing
... with minimal overhead.

Originally by Andreas Skielboe Git - Version Control System


Local Version Control Systems

Conventional version control systems provides some of these features by


making a local database with all changes made to files.

Any file can be recreated by getting changes from the database and
patch them up.

Originally by Andreas Skielboe Git - Version Control System


Centralized Version Control Systems

To enable synchronization and collaborative features the database is


stored on a central VCS server, where everyone works in the same
database.

Introduces problems: single point of failure, inability to work offline.

Originally by Andreas Skielboe Git - Version Control System


Distributed Version Control Systems

To overcome problems related to centralization, distributed VCSs


(DVCSs) were invented. Keeping a complete copy of database in every
working directory.

Actually the most simple and most powerful implementation of any


VCS.

Originally by Andreas Skielboe Git - Version Control System


Git Basics

Git Basics

Originally by Andreas Skielboe Git - Version Control System


Git Basics - The Git Workflow

The simplest use of Git:


Modify files in your working directory.
Stage the files, adding snapshots of them to your staging area.
Commit, takes files in the staging area and stores that snapshot
permanently to your Git directory.

Originally by Andreas Skielboe Git - Version Control System


Git Basics - The Three States

The three basic states of files in your Git repository:

Originally by Andreas Skielboe Git - Version Control System


Git Basics - Commits

Each commit in the git directory holds a snapshot of the files that were
staged and thus went into that commit, along with author information.

Each and every commit can always be looked at and retrieved.

Originally by Andreas Skielboe Git - Version Control System


Git Basics - File Status Lifecycle

Files in your working directory can be in four different states in relation


to the current commit.

Originally by Andreas Skielboe Git - Version Control System


Git Basics - Working with remotes

In Git all remotes are equal.

A remote in Git is nothing more than a link to another git directory.

Originally by Andreas Skielboe Git - Version Control System


Git Basics - Working with remotes

The easiest commands to get started working with a remote are


clone: Cloning a remote will make a complete local copy.
pull: Getting changes from a remote.
push: Sending changes to a remote.

Note that Git will only let you push to bare repositories.

Fear not! We are starting to get into more advanced topics. So lets look
at some examples.

Originally by Andreas Skielboe Git - Version Control System


Git Basics - Advantages

Basic advantages of using Git:


Nearly every operation is local.
Committed snapshots are always kept.
Strong support for non-linear development.

Originally by Andreas Skielboe Git - Version Control System


Hands-on

Hands-on with Git


(here be examples)

Originally by Andreas Skielboe Git - Version Control System


Hands-on - First-Time Git Setup

Before using Git for the first time:


Pick your identity
$ git config --global user.name "John Doe"
$ git config --global user.email [email protected]

Check your settings


$ git config --list

Get help
$ git help <verb>

Originally by Andreas Skielboe Git - Version Control System


Hands-on - Initializing a working repository

If you want to do all of your development on cs1520.cs.pitt.edu, you can


simply make a new web directory and initialize a repository for your site.
Initializing a working repository
$ ssh <id>@cs1520.cs.pitt.edu
$ cd public/csweb/
$ mkdir git_example
$ cd git_example
$ git init

Originally by Andreas Skielboe Git - Version Control System


Hands-on - Adding files

With your local working copy you can make any changes to the files in
your working directory as you like. When satisfied with your changes you
add any modified or new files to the staging area using add:
Adding files to the staging area
$ git add <filepattern>

Originally by Andreas Skielboe Git - Version Control System


Hands-on - Commit

Finally to commit the files in the staging area you run commit supplying
a commit message.
Committing staging area to the repository
$ git commit -m <msg>

Note that so far everything is happening locally in your working


directory.

Originally by Andreas Skielboe Git - Version Control System


Hands-on - Example working repository workload

Create an HTML file, add it, change it, and commit the change
$ ssh <id>@cs1520.cs.pitt.edu
$ cd public/csweb/git_example/
$ nano example.html #create a simple "Hello World" page
$ git add example.html #start tracking example.html
$ git commit -m "adding the first file to the repo"
$ nano example.html #change the page a bit
$ git add example.html #stage the file for commit
$ git commit -m "small update to example.html"

Now you can view example.html from your browser at:

http://cs1520.cs.pitt.edu/ <id>/git example/example.html

Originally by Andreas Skielboe Git - Version Control System


Hands-on - Clone to submit

In this class, you will have to submit your assignments using Git.
The submission directory for each student is:

/afs/pitt.edu/home/n/l/nlf4/submissions/cs1520/<id>/<assignment>/

or, more simply:

˜nlf4/submissions/cs1520/<id>/<assignment>/

For this exercise, I’ve made an ”example” assignment, so you can submit
your git example repository (containing example.html) by cloning the
repository in the submission directory a follows (from cs1520.cs.pitt.edu):

Change to the appropriate submission directory and clone your repository


there
$ cd ~nlf4/submissions/cs1520/<id>/example
$ git clone ~/public/csweb/git_example/

Originally by Andreas Skielboe Git - Version Control System


Hands-on - Installing Git on your home machine

You may want to work on your code and projects from your own laptop
or desktop. Instructions for installing Git on various platforms (Linux,
OSX, Windows) can be found in the Book Pro Git:

http://git-scm.com/book/en/Getting-Started-Installing-Git

On Linux and OSX, you can simply open up a terminal and start using
Git.
On Windows, you’ll have to navigate to an appropriate directory, right
click, and then open up a Bash shell to use msysGit.

There are also a number of GUI interfaces to Git available:

http://git-scm.com/downloads/guis

Originally by Andreas Skielboe Git - Version Control System


Hands-on - Initializing a bare repository

As mentioned before, you can only push to bare repositories, so we’ll


need to set up a bare repository on the server so that you can push
changes from your home machine to cs1520.cs.pitt.edu.
Initializing a bare repository
$ ssh <id>@cs1520.cs.pitt.edu
$ mkdir -p repos/git_example.git
$ cd repos/git_example.git
$ git --bare init

Originally by Andreas Skielboe Git - Version Control System


Hands-on - Cloning repositories

Clone a version of your code for Apache to display.


Cloning a repository on the same machine
$ ssh <id>@cs1520.cs.pitt.edu
$ cd public/csweb/
$ git clone ~/repos/git_example.git
$ cd git_example

Clone a version to work on on your home machine.


Cloning a remote repository
$ git clone <id>@cs1520.cs.pitt.edu:repos/git_example.git
$ cd git_example

Originally by Andreas Skielboe Git - Version Control System


Hands-on - Bare repository architecture

You’ve now cloned two working repositories from the bare repository.

Originally by Andreas Skielboe Git - Version Control System


Hands-on - Push/Pull

To share your commits with the remote you invoke the push command:
Pushing local commits to the remote
$ git push

To recieve changes that other people have pushed to the remote server
you can use the pull command:
Pulling remote commits to the local working directory
$ git pull

And thats it.

Originally by Andreas Skielboe Git - Version Control System


Hands-on - Example bare repository workload

From the git example directory cloned on a local machine:


Creating and modifying an HTML file, but now commits are pushed to a
bare repository
$ nano example.html #create a simple "Hello World" page
$ git add example.html #start tracking example.html
$ git commit -m "adding the first file to the repo"
$ git push origin master #push the first file to the repo
$ nano example.html #change the page a bit
$ git add example.html #stage the file for commit
$ git commit -m "small update to example.html"
$ git push #push the update commit

Originally by Andreas Skielboe Git - Version Control System


Hands-on - Example bare repository workload

Pull changes from the bare repository in to web directory


$ ssh <id>@cs1520.cs.pitt.edu
$ cd public/csweb/git_example/
$ git pull

Now you can view example.html from your browser at:

http://cs1520.cs.pitt.edu/ <id>/git example/example.html

Originally by Andreas Skielboe Git - Version Control System


More advanced topics

Git is a powerful and flexible DVCS. Some very useful, but a bit more
advanced features include:
Branching
Merging
Tagging
Rebasing

Originally by Andreas Skielboe Git - Version Control System


Checkout these slides

The LATEX-source of the original slides is freely available on GitHub.

GitHub
$ git clone git://github.com/askielboe/into-to-git-slides.git

I’m hosting the LATEX-source for these slides within the CS dept.

Source for these slides


$ git clone <id>@cs1520.cs.pitt.edu:~nlf4/repos/git_slides.git

Originally by Andreas Skielboe Git - Version Control System


References

Some good Git sources for information:


Git Community Book (Pro Git)- http://book.git-scm.com/
Git Reference - http://gitref.org/
GitHub - http://github.com/
Git from the bottom up -
http://ftp.newartisans.com/pub/git.from.bottom.up.pdf
Understanding Git Conceptually -
http://www.eecs.harvard.edu/~cduan/technical/git/
Git Immersion - http://gitimmersion.com/

Originally by Andreas Skielboe Git - Version Control System

You might also like