0% found this document useful (0 votes)
32 views19 pages

Lec04 Git

Uploaded by

oladejotamoo.to
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views19 pages

Lec04 Git

Uploaded by

oladejotamoo.to
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Lecture 4 - Git Basics

CS 3251
Intermediate Software Design
Prof. Graham Hemingway
Basic Intro to Git
We will:
• Discuss the basic Git model
• Pull/clone les from a repository on GitHub
• Edit les in your own local Git repo
• Push les to a repo on GitHub
fi
fi
fi
Register Your GitHub Account

Go to:
• https://vanderbilt.rosterlink.org
• On right side click “Link GitHub Account”
• You should get an email asking you to join
the course GitHub organization - accept it
Git History
• Linux development community

• Linus Torvalds in 2005

• Initial goals:
• Speed
• Support for non-linear development (thousands of
parallel branches)
• Fully distributed
• Able to handle large projects like Linux e ciently
ffi
Git Resources
• At the command line: (where verb = con g, add, commit, etc.)

$ git help <verb>

$ git <verb> --help

$ man git-<verb>

• Free on-line book: http://git-scm.com/book


• Git tutorial: http://schacon.github.com/git/gittutorial.html
• Reference page for Git: http://gitref.org/index.html
• Git website: http://git-scm.com/
• Git for Computer Scientists (http://eagain.net/articles/git-for-
computer-scientists/)
fi
Git uses a distributed model
Centralized Model Distributed Model

(CVS, Subversion, Perforce)

(Git, Mercurial)
Result: Many operations are local
Git Takes Snapshots
Subversion

Git
Git uses checksums
• In Subversion each modi cation to the central repo incremented
the version # of the overall repo.

• How will this numbering scheme work when each user has their
own copy of the repo, and commits changes to their local copy
of the repo before pushing to the central server?

• Instead, Git generates a unique SHA-1 hash – 40 character


string of hex digits, for every commit. Refer to commits by this
ID rather than a version number. Often we only see the rst 7
characters:

1677b2d Edited rst line of readme


258efa7 Added line to readme
0e52da7 Initial commit
fi
fi
fi
A Local Git project has three areas

Unmodified/modified Staged Committed


Files Files Files

Note: working directory sometimes called the “working tree”, staging area sometimes called the “index”.
Git le lifecycle
fi
Basic Work ow
Basic Git work ow:

1. Modify les in your working directory.

2. Stage les, adding snapshots of them to your staging


area.

3. Do a commit, which takes the les as they are in the


staging area and stores that snapshot permanently to
your Git directory.
Notes:
• If a particular version of a le is in the git directory, it’s considered committed.
• If it’s modi ed but has been added to the staging area, it is staged.
• If it was changed since it was checked out but has not been staged, it is
modified.
fi
fi
fi
fl
fi
fl
fi
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

Question: Do I have to use GitHub to use Git?


Answer: No!
• You can use Git completely locally for your own purposes, or
• You or someone else could set up a server to share les, or
• You could share a repo with users on the same le system, such as
we did for homework 9 (as long everyone has the needed le
permissions).
fi
fi
fi
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 con g –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 ag.

2. You can also set the editor that is used for writing commit
messages:
$ git config --global core.editor nano (it is vim by default)
fl
fi
Create a local copy of a repo
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 les from the repo, and a .git directory
(used to hold the staging area and your actual repo)

B) To create a Git repo in your current directory:


$ git init
This will create a .git directory in your current directory. Then
you can commit les in that directory into the repo:
$ git add file1.java
$ git commit –m “initial project version”
fi
fi
Committing les
• Therst time we ask a le 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 les at this point in time and adds it
to the staging area.

• To move staged changes into the repo we commit:


$ git commit –m “Fixing bug #22”

Note: To unstage a change on a le before you have committed it:


$ git reset HEAD -- filename
Note: To unmodify a modi ed le:
$ git checkout -- filename
Note: These commands are just acting on your local version of repo.
fi
fi
fi
fi
fi
fi
fi
Status and Diff
• To view the status of your les in the working directory
and staging area:
$ git status
$ git status –s //Short version of status

• To see what is modi ed but unstaged:

$ git diff

• To see staged changes:


$ git diff --cached
fi
fi
Viewing logs
To see a log of all changes in your local repo:
$ git log
$ git log --oneline //Show a shorter version

1677b2d Edited first line of readme


258efa7 Added line to readme
0e52da7 Initial commit

$ git log -5 //Show only the 5 most recent commits

Note: changes will be listed by commitID # (i.e. SHA-1 hash)


Note: changes made to the remote repo before the last time you
cloned/pulled from it will also be included here
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 shows diff of what is staged and what is
modified but unstaged
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
GitHub - Add a PAT
If username and password don’t work to clone your repo from
GitHub, you likely need to create a Personal Access Token (PAT).
Here are the steps:
1. Log into GitHub and click on your user icon in the upper right
2. Click on “Settings” near the bottom of this popup menu
3. Click on “Developer settings” at very bottom of left-side menu
4. Click “Personal access tokens” and then “Tokens (classic)”
5. From menu near the center of the screen click “Generate New Token” and then “Generate
New Token (classic)”
A) Give it any name you want
B) Set expiration date for as far as you are comfortable with (probably “No expiration”)
C) Check the “repo” box
D) Click “Generate token” at the bottom
6. Make sure to copy this long string of text somewhere safe - DO NOT LOSE IT
7. Use this string as the password for when you clone - username is still your GH username

You might also like