Version Control With Git
Version Control With Git
1
Git
• Git is a free and open source distributed version control system
designed to handle everything from small to very large projects with
speed and efficiency.
2
Git History
• Came out of Linux development community
• Linus Torvalds, 2005
• Initial goals:
Speed
Support for non-linear development (thousands of parallel branches)
Fully distributed
Able to handle large projects like Linux efficiently
3
Git uses a distributed model
Subversion
Git
5
Git uses checksums
• In Subversion each modification to the central repo incremented
the version # of the overall repo.
6
A Local Git project has three areas
8
Git file lifecycle
9
Get ready to use Git!
1. Set the name and email for Git to use when you commit:
$ git config --global user.name “Bugs Bunny”
$ git config --global user.email [email protected]
• You can call git config –list to verify these are set.
• These will be set globally for all Git projects you work with.
• You can also set variables on a project-only basis by not using the
--global flag.
• You can also set the editor that is used for writing commit
messages:
$ git config --global core.editor emacs (it is vim by default)
10
Create a local copy of a repo
2. Two common scenarios: (only do one of these)
a) To clone an already existing repo to your current directory:
$ git clone <url> [local dir name]
This will create a directory named local dir name, containing a working copy
of the files from the repo, and a .git directory (used to hold the staging
area and your actual repo)
11
Git commands
command description
git clone url [dir] copy a git repository so you can add to it
git add files adds file contents to the staging area
git commit records a snapshot of the staging area
git status view the status of your files in the working
directory and staging area
git diff Show changes between commits, commit
and working tree, etc
git help [command] get help info about a particular command
git pull fetch from a remote repo and try to merge
into the current branch
git push push your new branches and data to a
remote repository
others: init, reset, branch, checkout, merge, log, tag
12
Committing files
• The first time we ask a file to be tracked, and every time before we
commit a file we must add it to the staging area:
$ git add README.txt hello.java
This takes a snapshot of these files at this point in time and adds it to
the staging area.
Note: These commands are just acting on your local version of repo. 13
Status and Diff
• To view the status of your files in the working directory and staging area:
$ git status or
$ git status –s
(-s shows a short one line version similar to svn)
14
After editing a file…
[rea@attu1 superstar]$ emacs rea.txt
[rea@attu1 superstar]$ git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: rea.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[rea@attu1 superstar]$ git status -s
M rea.txt Note: M is in second column = “working tree”
[rea@attu1 superstar]$ git diff Shows modifications that have not been staged.
diff --git a/rea.txt b/rea.txt
index 66b293d..90b65fd 100644
--- a/rea.txt
+++ b/rea.txt
@@ -1,2 +1,4 @@
Here is rea's file.
+
+One new line added.
[rea@attu1 superstar]$ git diff --cached Shows nothing, no modifications have been staged yet.
[rea@attu1 superstar]$
15
After adding file to staging area…
[rea@attu1 superstar]$ git add rea.txt
[rea@attu1 superstar]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: rea.txt
#
[rea@attu1 superstar]$ git status -s
M rea.txt Note: M is in first column = “staging area”
[rea@attu1 superstar]$ git diff Note: Shows nothing, no modifications that have not been staged.
[rea@attu1 superstar]$ git diff --cached Note: Shows staged modifications.
diff --git a/rea.txt b/rea.txt
index 66b293d..90b65fd 100644
--- a/rea.txt
+++ b/rea.txt
@@ -1,2 +1,4 @@
Here is rea's file.
+
+One new line added.
16
Viewing logs
To see a log of all changes in your local repo:
•$ git log or
•$ git log --oneline (to show a shorter version)
17
Pulling and Pushing
Good practice:
1.Add and Commit your changes to your local repo
2.Pull from remote repo to get most recent changes (fix conflicts if
necessary, add and commit them to your local repo)
3.Push your changes to the remote repo
To fetch the most recent updates from the remote repo into your local
repo, and put them into your working directory:
$ git pull origin master
To push your changes from your local repo to the remote repo:
$ git push origin master
Notes: origin = an alias for the URL you cloned from
master = the remote branch you are pulling from/pushing to,
(the local branch you are pulling to/pushing from is your current branch)
18
Branching
To create a branch called experimental:
•$ git branch experimental
To list all branches: (* shows which one you are currently on)
•$ git branch
Later on, changes between the two branches differ, to merge changes from
experimental into the master:
•$ git checkout master
•$ git merge experimental
19
Aside: So what is github?
• GitHub.com is a site for online storage of Git repositories.
• Many open source projects use it, such as the Linux kernel.
• You can get free space for open source projects or you can pay for
private projects.
20
Git Resources
• At the command line: (where verb = config, add, commit, etc.)
$ git help <verb>
$ git <verb> --help
$ man git-<verb>
21
Assignment 2
Please finish and take a snapshot for each of the following steps and paste it into the
report.
1.Download and install Git
2.Set user name and email
3.Clone or create a Git repository
4.Create a file
5.Add the file
6.Commit the file
7.Check status and log
8.Delete and commit the file
9.Push to repo
10.Pull from repo
22
Download & Install Git
23
Download & Install Git
24
Download & Install Git
25