SlideShare a Scribd company logo
1
CSE 391
Lecture 9
Version control with Git
slides created by Ruth Anderson & Marty Stepp, images from http://git-scm.com/book/en/
http://www.cs.washington.edu/391/
2
Problems Working Alone
• Ever done one of the following?
ļ‚§ Had code that worked, made a bunch of changes and saved it, which
broke the code, and now you just want the working version back…
ļ‚§ Accidentally deleted a critical file, hundreds of lines of code gone…
ļ‚§ Somehow messed up the structure/contents of your code base, and want
to just ā€œundoā€ the crazy action you just did
ļ‚§ Hard drive crash!!!! Everything’s gone, the day before deadline.
• Possible options:
ļ‚§ Save as (MyClass-v1.java)
• Ugh. Just ugh. And now a single line change results in duplicating the entire
file…
3
Problems Working in teams
ļ‚§ Whose computer stores the "official" copy of the project?
• Can we store the project files in a neutral "official" location?
ļ‚§ Will we be able to read/write each other's changes?
• Do we have the right file permissions?
• Lets just email changed files back and forth! Yay!
ļ‚§ What happens if we both try to edit the same file?
• Bill just overwrote a file I worked on for 6 hours!
ļ‚§ What happens if we make a mistake and corrupt an important file?
• Is there a way to keep backups of our project files?
ļ‚§ How do I know what code each teammate is working on?
4
Solution: Version Control
• version control system: Software that tracks and manages changes
to a set of files and resources.
• You use version control all the time
ļ‚§ Built into word processors/spreadsheets/presentation software
• The magical ā€œundoā€ button takes you back to ā€œthe version before my last
actionā€
ļ‚§ Wiki’s
• Wiki’s are all about version control, managing updates, and allowing
rollbacks to previous versions
5
Software Version control
• Many version control systems are designed and used especially for
software engineering projects
ļ‚§ examples: CVS, Subversion (SVN), Git, Monotone, BitKeeper, Perforce
• helps teams to work together on code projects
ļ‚§ a shared copy of all code files that all users can access
ļ‚§ keeps current versions of all files, and backups of past versions
ļ‚§ can see what files others have modified and view the changes
ļ‚§ manages conflicts when multiple users modify the same file
ļ‚§ not particular to source code; can be used for papers, photos, etc.
• but often works best with plain text/code files
6
Repositories
• Repository (aka ā€œrepoā€): a location storing a copy of all files.
ļ‚§ you don't edit files directly in the repo;
ļ‚§ you edit a local working copy or ā€œworking treeā€
ļ‚§ then you commit your edited files into the repo
• There may be only one repository that all users share (CVS,
Subversion)
• Or each user could also have their own copy of the repository (Git,
Mercurial)
• Files in your working directory must be added to the repo in order
to be tracked.
7
What to put in a Repo?
• Everything needed to create your project:
ļ‚§ Source code (Examples: .java, .c, .h, .cpp )
ļ‚§ Build files (Makefile, build.xml)
ļ‚§ Other resources needed to build your project: icons, text etc.
• Things generally NOT put in a repo (these can be easily re-created
and just take up space):
ļ‚§ Object files (.o)
ļ‚§ Executables (.exe)
8
Repository Location
• Can create the repository anywhere
ļ‚§ Can be on the same computer that you’re going to work on, which might be
ok for a personal project where you just want rollback protection
• But, usually you want the repository to be robust:
ļ‚§ On a computer that’s up and running 24/7
• Everyone always has access to the project
ļ‚§ On a computer that has a redundant file system (ie RAID)
• No more worries about that hard disk crash wiping away your project!
• Options:
ļ‚§ attu, CSE GitLab, GitHub (do NOT use GitHub for homework!!!)
9
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.
• Do NOT use GitHub to store your homework!!
Question: Do I have to use GitHub to use Git?
Answer: No!
• you can use Git completely locally for your own purposes, or
• you can use the CSE GitLab server, or
• you could share a repo with users on the same file system (e.g. attu) as
long everyone has the needed file permissions.
10
CSE GitLab
Everyone in this course has been given an account on CSE GitLab
To use CSE GitLab:
1.Log onto CSE GitLab: https://gitlab.cs.washington.edu/
ļ‚§ IMPORTANT: If you have a CSENetID use that, otherwise use your UWNetID
2.Add an ssh key: https://gitlab.cs.washington.edu/help/ssh/README.md
ļ‚§ Follow the instructions in README.md which say to type:
ssh-keygen -t rsa -C "yourUWNetID@uw.edu" -b
4096
1.Just hit return to accept the default file location and you do not need a
password so hit return both times when prompted for a password
ļ‚§ Then type: cat ~/.ssh/id_rsa.pub
ļ‚§ Copy-paste the key to the 'SSH Keys' section under ā€˜Profile Settings'
in your user profile on CSE GitLab. Or go directly here:
https://gitlab.cs.washington.edu/profile/keys
11
Git
HTTP://XKCD.COM/1597/
12
Git Resources
• At the command line: (where <verb> = config, add, commit, etc.)
$ git help <verb>
$ git <verb> --help
$ man git-<verb>
• Free on-line book: https://git-scm.com/book/en/v2
• 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/
13
History of Git
• 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
14
Git uses a distributed model
Centralized Model Distributed Model
(CVS, Subversion, Perforce) (Git, Mercurial)
Result: Many operations are local
15
Ways to use Git
Using Git on your own
computer, one user
Using Git on multiple computers,
multiple users or one user on
multiple computers
Possible servers:
•CSE GitLab
•attu
•GitHub (NOT for homework!)
16
A Local Git project has three areas
Unmodified/modified
Files
Staged
Files
Committed
Files
Note: working directory sometimes called the ā€œworking treeā€, staging area sometimes called the ā€œindexā€.
17
Git file lifecycle
18
Basic Workflow
Basic Git workflow:
1.Modify files in your working directory.
2.Stage files, adding snapshots of them to your staging area.
3.Do a commit, which takes the files as they are in the staging area and
stores that snapshot permanently to your local Git directory (your local
copy of the repo).
•Notes:
1. If a particular version of a file is in your local git directory, it’s considered committed.
2. If it’s modified but has been added to the staging area, it is staged.
3. If it was changed since it was checked out but has not been staged, it is modified.
19
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 bugs@gmail.com
1.You can call git config –-list to verify these are set.
2.These will be set globally for all Git projects you work with.
3.You can set variables on a project-only basis by not using the --global flag.
2. The latest version of git will also prompt you that push.default is not set, you
can make this warning go away with:
$ git config --global push.default simple
• You can also set the editor used for writing commit messages:
$ git config --global core.editor emacs (it is vim by default)
vim tips: ā€œaā€ add, ā€œescā€ when done adding, ā€œwq:ā€ to save and quit
vim editor: http://www.gentoo.org/doc/en/vi-guide.xml
vim ref card: http://tnerual.eriogerg.free.fr/vimqrc.pdf
20
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 which you can ignore (used to hold
the staging area and your local repo)
Example: git clone git@gitlab.cs.washington.edu:rea/superTest.git
b) To create a Git repo in your current directory:
$ git init
This will create a .git directory in your current directory which you can ignore (used to
hold the staging area and your local repo).
Then you can commit files in your current directory into the local repo:
$ git add file1.java
$ git commit –m ā€œinitial project versionā€
21
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
22
Adding & Committing files
1. 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: To unstage a change on a file before you have committed it:
$ git reset HEAD filename
2. To move staged changes into the local repo we commit:
$ git commit –m ā€œFixing bug #22ā€
Note: You can edit your most recent commit message (if you have not pushed your
commit yet) using: git commit –-amend
Note: These commands are just acting on your local version of repo.
23
Use Good Commit Messages
HTTP://XKCD.COM/1296/
24
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)
• To see difference between your working directory and the staging
area (This shows what is modified but unstaged):
$ git diff
• To see difference between the staging area and your local copy of
the repo (This shows staged changes): (--staged is synonymous)
$ git diff --cached
25
After editing a file…
$emacs rea.txt
$ 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")
$ git status -s
M rea.txt  Note: M is in second column = ā€œworking treeā€
$ 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.
$ git diff --cached  Shows nothing, no modifications have been staged yet.
$
26
After adding file to staging area…
$ git add rea.txt
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: rea.txt
#
$ git status -s
M rea.txt  Note: M is in first column = ā€œstaging areaā€
$ git diff  Note: Shows nothing, no modifications that have not been staged.
$ 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.
27
Viewing logs
To see a log of all changes in your local repo:
•$ git log or
•$ git log --oneline (to show a shorter version)
1677b2d Edited first line of readme
258efa7 Added line to readme
0e52da7 Initial commit
•git log -5 (to show only the 5 most recent updates, etc.)
Note: changes will be listed by commitID #, (SHA-1 hash)
Note: changes made to the remote repo before the last time you
cloned/pulled from it will also be included here
28
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, then add and commit those changes 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)
29
Avoiding Common Problems
From CSE 331: http://courses.cs.washington.edu/courses/cse331/18wi/tools/versioncontrol.html#git-pitfalls
•Do not edit the repository (the .git directory) manually. It wasn't
designed for modifications by humans.
•Try not to make many drastic changes at once. Instead, make multiple
commits, each of which has a single logical purpose. This will minimize
merge conflicts. This is good coding practice in general.
•Always git pull before editing a file. It's easy to forget this. If you
forget, you may end up editing an outdated version, which can cause
nasty merge conflicts.
•Don't forget git push after you have made and committed changes.
They are not copied to the remote repository until you do a push.
30
Branching
To create a branch called experimental:
•$ git branch experimental
To list all branches: (* shows which one you are currently on)
•$ git branch
To switch to the experimental branch:
•$ git checkout experimental
Later on, changes between the two branches differ, to merge changes from
experimental into the master:
•$ git checkout master
•$ git merge experimental
Note: git log --graph can be useful for showing branches.
Note: These branches are in your local repo!
31
SVN vs. Git
• SVN:
ļ‚§ central repository approach – the main repository is the only ā€œtrueā€
source, only the main repository has the complete file history
ļ‚§ Users check out local copies of the current version
• Git:
ļ‚§ Distributed repository approach – every checkout of the repository is a
full fledged repository, complete with history
ļ‚§ Greater redundancy and speed
ļ‚§ Branching and merging repositories is more heavily used as a result
32
Wrap-up
• You *will* use version control software when working on projects,
both here and in industry
ļ‚§ Rather foolish not to
ļ‚§ Advice: just set up a repository, even for small projects, it will save you
time and hassle
• HW9 (Git) has more details and walks you through creating a Git
repo and adding to a shared repo.

More Related Content

PPT
CSE 390 Lecture 9 - Version Control with GIT
PPTX
Git training (basic)
PPT
Git 101 - Crash Course in Version Control using Git
PPT
Introduction to Git
PPTX
Mini-training: Let’s Git It!
PDF
git.ppt.pdf
PPTX
1-Intro to VC & GIT PDF.pptx
PPTX
github ppt git ppt on git hub to know ab
CSE 390 Lecture 9 - Version Control with GIT
Git training (basic)
Git 101 - Crash Course in Version Control using Git
Introduction to Git
Mini-training: Let’s Git It!
git.ppt.pdf
1-Intro to VC & GIT PDF.pptx
github ppt git ppt on git hub to know ab

Similar to 391Lecture0909 Vision control of git.ppt (20)

PPT
Git is a distributed version control system .
PPT
GIT-FirstPart.ppt
PDF
Git hub
PPTX
Introduction to Git and Github
PPT
390a gitintro 12au
PPTX
sample.pptx
PPTX
Introduction to git hub
PDF
Git for folk who like GUIs
PPT
Git installation and configuration
PPTX
Luis atencio on_git
PPTX
Git 101
PPTX
GitHub Event.pptx
PPT
git2nvlkndvslnvdslnlknvdlnlvdsnlknsdvlkn.ppt
PPTX
IS - section 1 - modifiedFinal information system.pptx
PPTX
git and github-1.pptx
PPT
Git and GitHUB Explanation and simple coding for CLI
PDF
Mini git tutorial
PPTX
Git Basics for Software Version Management
PDF
git and github
PDF
GIT_Overview.
Git is a distributed version control system .
GIT-FirstPart.ppt
Git hub
Introduction to Git and Github
390a gitintro 12au
sample.pptx
Introduction to git hub
Git for folk who like GUIs
Git installation and configuration
Luis atencio on_git
Git 101
GitHub Event.pptx
git2nvlkndvslnvdslnlknvdlnlvdsnlknsdvlkn.ppt
IS - section 1 - modifiedFinal information system.pptx
git and github-1.pptx
Git and GitHUB Explanation and simple coding for CLI
Mini git tutorial
Git Basics for Software Version Management
git and github
GIT_Overview.
Ad

More from GevitaChinnaiah (20)

PPTX
Mobile Application and Developments.pptx
PPTX
White Box Testing on a Python Function.pptx
PPTX
Building a To-Do List App lecture 1.pptx
PPTX
Software Quality and Testing note 1.pptx
PPTX
Introduction to JavaScript DOM and User Input.pptx
PPTX
Introduction to Python and Basic Syntax.pptx
PPTX
Software Programming with Python II.pptx
PPT
a basic java programming and data type.ppt
PPTX
Install an Operating System.pptx
PPTX
Solve Stop Code Memory Management on Windows.pptx
PPTX
windows memory management.pptx
PPTX
Introduction to Database Management.pptx
PPT
OPERATING SYSTEM CHAPTER 3.ppt
PPTX
Lecture 3 React Native with Database.pptx
PPTX
Database Management System (DBMS).pptx
PPTX
Operating System chapter 1.pptx
PPTX
Lecture 2 Styling and Layout in React Native.pptx
PPTX
Lecture 1 Introduction to React Native.pptx
PPTX
Understanding Applications.pptx
PPTX
Operating System 2.pptx
Mobile Application and Developments.pptx
White Box Testing on a Python Function.pptx
Building a To-Do List App lecture 1.pptx
Software Quality and Testing note 1.pptx
Introduction to JavaScript DOM and User Input.pptx
Introduction to Python and Basic Syntax.pptx
Software Programming with Python II.pptx
a basic java programming and data type.ppt
Install an Operating System.pptx
Solve Stop Code Memory Management on Windows.pptx
windows memory management.pptx
Introduction to Database Management.pptx
OPERATING SYSTEM CHAPTER 3.ppt
Lecture 3 React Native with Database.pptx
Database Management System (DBMS).pptx
Operating System chapter 1.pptx
Lecture 2 Styling and Layout in React Native.pptx
Lecture 1 Introduction to React Native.pptx
Understanding Applications.pptx
Operating System 2.pptx
Ad

Recently uploaded (20)

PPTX
Logistic Regression ml machine learning.pptx
PPTX
Measurement of Afordability for Water Supply and Sanitation in Bangladesh .pptx
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PPTX
Major-Components-ofNKJNNKNKNKNKronment.pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PDF
Report The-State-of-AIOps 20232032 3.pdf
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
Data-Driven-Credit-Card-Launch-A-Wells-Fargo-Case-Study.pptx
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
Challenges and opportunities in feeding a growing population
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
Global journeys: estimating international migration
PPTX
batch data Retailer Data management Project.pptx
PDF
Master Databricks SQL with AccentFuture – The Future of Data Warehousing
PDF
Oracle OFSAA_ The Complete Guide to Transforming Financial Risk Management an...
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
1_Introduction to advance data techniques.pptx
Logistic Regression ml machine learning.pptx
Measurement of Afordability for Water Supply and Sanitation in Bangladesh .pptx
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
Major-Components-ofNKJNNKNKNKNKronment.pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
STUDY DESIGN details- Lt Col Maksud (21).pptx
Report The-State-of-AIOps 20232032 3.pdf
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Data-Driven-Credit-Card-Launch-A-Wells-Fargo-Case-Study.pptx
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Challenges and opportunities in feeding a growing population
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Global journeys: estimating international migration
batch data Retailer Data management Project.pptx
Master Databricks SQL with AccentFuture – The Future of Data Warehousing
Oracle OFSAA_ The Complete Guide to Transforming Financial Risk Management an...
climate analysis of Dhaka ,Banglades.pptx
1_Introduction to advance data techniques.pptx

391Lecture0909 Vision control of git.ppt

  • 1. 1 CSE 391 Lecture 9 Version control with Git slides created by Ruth Anderson & Marty Stepp, images from http://git-scm.com/book/en/ http://www.cs.washington.edu/391/
  • 2. 2 Problems Working Alone • Ever done one of the following? ļ‚§ Had code that worked, made a bunch of changes and saved it, which broke the code, and now you just want the working version back… ļ‚§ Accidentally deleted a critical file, hundreds of lines of code gone… ļ‚§ Somehow messed up the structure/contents of your code base, and want to just ā€œundoā€ the crazy action you just did ļ‚§ Hard drive crash!!!! Everything’s gone, the day before deadline. • Possible options: ļ‚§ Save as (MyClass-v1.java) • Ugh. Just ugh. And now a single line change results in duplicating the entire file…
  • 3. 3 Problems Working in teams ļ‚§ Whose computer stores the "official" copy of the project? • Can we store the project files in a neutral "official" location? ļ‚§ Will we be able to read/write each other's changes? • Do we have the right file permissions? • Lets just email changed files back and forth! Yay! ļ‚§ What happens if we both try to edit the same file? • Bill just overwrote a file I worked on for 6 hours! ļ‚§ What happens if we make a mistake and corrupt an important file? • Is there a way to keep backups of our project files? ļ‚§ How do I know what code each teammate is working on?
  • 4. 4 Solution: Version Control • version control system: Software that tracks and manages changes to a set of files and resources. • You use version control all the time ļ‚§ Built into word processors/spreadsheets/presentation software • The magical ā€œundoā€ button takes you back to ā€œthe version before my last actionā€ ļ‚§ Wiki’s • Wiki’s are all about version control, managing updates, and allowing rollbacks to previous versions
  • 5. 5 Software Version control • Many version control systems are designed and used especially for software engineering projects ļ‚§ examples: CVS, Subversion (SVN), Git, Monotone, BitKeeper, Perforce • helps teams to work together on code projects ļ‚§ a shared copy of all code files that all users can access ļ‚§ keeps current versions of all files, and backups of past versions ļ‚§ can see what files others have modified and view the changes ļ‚§ manages conflicts when multiple users modify the same file ļ‚§ not particular to source code; can be used for papers, photos, etc. • but often works best with plain text/code files
  • 6. 6 Repositories • Repository (aka ā€œrepoā€): a location storing a copy of all files. ļ‚§ you don't edit files directly in the repo; ļ‚§ you edit a local working copy or ā€œworking treeā€ ļ‚§ then you commit your edited files into the repo • There may be only one repository that all users share (CVS, Subversion) • Or each user could also have their own copy of the repository (Git, Mercurial) • Files in your working directory must be added to the repo in order to be tracked.
  • 7. 7 What to put in a Repo? • Everything needed to create your project: ļ‚§ Source code (Examples: .java, .c, .h, .cpp ) ļ‚§ Build files (Makefile, build.xml) ļ‚§ Other resources needed to build your project: icons, text etc. • Things generally NOT put in a repo (these can be easily re-created and just take up space): ļ‚§ Object files (.o) ļ‚§ Executables (.exe)
  • 8. 8 Repository Location • Can create the repository anywhere ļ‚§ Can be on the same computer that you’re going to work on, which might be ok for a personal project where you just want rollback protection • But, usually you want the repository to be robust: ļ‚§ On a computer that’s up and running 24/7 • Everyone always has access to the project ļ‚§ On a computer that has a redundant file system (ie RAID) • No more worries about that hard disk crash wiping away your project! • Options: ļ‚§ attu, CSE GitLab, GitHub (do NOT use GitHub for homework!!!)
  • 9. 9 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. • Do NOT use GitHub to store your homework!! Question: Do I have to use GitHub to use Git? Answer: No! • you can use Git completely locally for your own purposes, or • you can use the CSE GitLab server, or • you could share a repo with users on the same file system (e.g. attu) as long everyone has the needed file permissions.
  • 10. 10 CSE GitLab Everyone in this course has been given an account on CSE GitLab To use CSE GitLab: 1.Log onto CSE GitLab: https://gitlab.cs.washington.edu/ ļ‚§ IMPORTANT: If you have a CSENetID use that, otherwise use your UWNetID 2.Add an ssh key: https://gitlab.cs.washington.edu/help/ssh/README.md ļ‚§ Follow the instructions in README.md which say to type: ssh-keygen -t rsa -C "[email protected]" -b 4096 1.Just hit return to accept the default file location and you do not need a password so hit return both times when prompted for a password ļ‚§ Then type: cat ~/.ssh/id_rsa.pub ļ‚§ Copy-paste the key to the 'SSH Keys' section under ā€˜Profile Settings' in your user profile on CSE GitLab. Or go directly here: https://gitlab.cs.washington.edu/profile/keys
  • 12. 12 Git Resources • At the command line: (where <verb> = config, add, commit, etc.) $ git help <verb> $ git <verb> --help $ man git-<verb> • Free on-line book: https://git-scm.com/book/en/v2 • 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/
  • 13. 13 History of Git • 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
  • 14. 14 Git uses a distributed model Centralized Model Distributed Model (CVS, Subversion, Perforce) (Git, Mercurial) Result: Many operations are local
  • 15. 15 Ways to use Git Using Git on your own computer, one user Using Git on multiple computers, multiple users or one user on multiple computers Possible servers: •CSE GitLab •attu •GitHub (NOT for homework!)
  • 16. 16 A Local Git project has three areas Unmodified/modified Files Staged Files Committed Files Note: working directory sometimes called the ā€œworking treeā€, staging area sometimes called the ā€œindexā€.
  • 18. 18 Basic Workflow Basic Git workflow: 1.Modify files in your working directory. 2.Stage files, adding snapshots of them to your staging area. 3.Do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your local Git directory (your local copy of the repo). •Notes: 1. If a particular version of a file is in your local git directory, it’s considered committed. 2. If it’s modified but has been added to the staging area, it is staged. 3. If it was changed since it was checked out but has not been staged, it is modified.
  • 19. 19 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] 1.You can call git config –-list to verify these are set. 2.These will be set globally for all Git projects you work with. 3.You can set variables on a project-only basis by not using the --global flag. 2. The latest version of git will also prompt you that push.default is not set, you can make this warning go away with: $ git config --global push.default simple • You can also set the editor used for writing commit messages: $ git config --global core.editor emacs (it is vim by default) vim tips: ā€œaā€ add, ā€œescā€ when done adding, ā€œwq:ā€ to save and quit vim editor: http://www.gentoo.org/doc/en/vi-guide.xml vim ref card: http://tnerual.eriogerg.free.fr/vimqrc.pdf
  • 20. 20 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 which you can ignore (used to hold the staging area and your local repo) Example: git clone [email protected]:rea/superTest.git b) To create a Git repo in your current directory: $ git init This will create a .git directory in your current directory which you can ignore (used to hold the staging area and your local repo). Then you can commit files in your current directory into the local repo: $ git add file1.java $ git commit –m ā€œinitial project versionā€
  • 21. 21 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
  • 22. 22 Adding & Committing files 1. 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: To unstage a change on a file before you have committed it: $ git reset HEAD filename 2. To move staged changes into the local repo we commit: $ git commit –m ā€œFixing bug #22ā€ Note: You can edit your most recent commit message (if you have not pushed your commit yet) using: git commit –-amend Note: These commands are just acting on your local version of repo.
  • 23. 23 Use Good Commit Messages HTTP://XKCD.COM/1296/
  • 24. 24 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) • To see difference between your working directory and the staging area (This shows what is modified but unstaged): $ git diff • To see difference between the staging area and your local copy of the repo (This shows staged changes): (--staged is synonymous) $ git diff --cached
  • 25. 25 After editing a file… $emacs rea.txt $ 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") $ git status -s M rea.txt  Note: M is in second column = ā€œworking treeā€ $ 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. $ git diff --cached  Shows nothing, no modifications have been staged yet. $
  • 26. 26 After adding file to staging area… $ git add rea.txt $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: rea.txt # $ git status -s M rea.txt  Note: M is in first column = ā€œstaging areaā€ $ git diff  Note: Shows nothing, no modifications that have not been staged. $ 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.
  • 27. 27 Viewing logs To see a log of all changes in your local repo: •$ git log or •$ git log --oneline (to show a shorter version) 1677b2d Edited first line of readme 258efa7 Added line to readme 0e52da7 Initial commit •git log -5 (to show only the 5 most recent updates, etc.) Note: changes will be listed by commitID #, (SHA-1 hash) Note: changes made to the remote repo before the last time you cloned/pulled from it will also be included here
  • 28. 28 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, then add and commit those changes 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)
  • 29. 29 Avoiding Common Problems From CSE 331: http://courses.cs.washington.edu/courses/cse331/18wi/tools/versioncontrol.html#git-pitfalls •Do not edit the repository (the .git directory) manually. It wasn't designed for modifications by humans. •Try not to make many drastic changes at once. Instead, make multiple commits, each of which has a single logical purpose. This will minimize merge conflicts. This is good coding practice in general. •Always git pull before editing a file. It's easy to forget this. If you forget, you may end up editing an outdated version, which can cause nasty merge conflicts. •Don't forget git push after you have made and committed changes. They are not copied to the remote repository until you do a push.
  • 30. 30 Branching To create a branch called experimental: •$ git branch experimental To list all branches: (* shows which one you are currently on) •$ git branch To switch to the experimental branch: •$ git checkout experimental Later on, changes between the two branches differ, to merge changes from experimental into the master: •$ git checkout master •$ git merge experimental Note: git log --graph can be useful for showing branches. Note: These branches are in your local repo!
  • 31. 31 SVN vs. Git • SVN: ļ‚§ central repository approach – the main repository is the only ā€œtrueā€ source, only the main repository has the complete file history ļ‚§ Users check out local copies of the current version • Git: ļ‚§ Distributed repository approach – every checkout of the repository is a full fledged repository, complete with history ļ‚§ Greater redundancy and speed ļ‚§ Branching and merging repositories is more heavily used as a result
  • 32. 32 Wrap-up • You *will* use version control software when working on projects, both here and in industry ļ‚§ Rather foolish not to ļ‚§ Advice: just set up a repository, even for small projects, it will save you time and hassle • HW9 (Git) has more details and walks you through creating a Git repo and adding to a shared repo.

Editor's Notes

  • #11: HTTP://XKCD.COM/1597/
  • #13: Initial goals were to support development of Linux
  • #14: http://git-scm.com/book/en/Getting-Started-About-Version-Control
  • #22: Note: To unstage a change on a file before you have committed it: $ git reset HEAD filename Note: To unmodify a modified file: $ git checkout -- filename
  • #23: HTTP://XKCD.COM/1296/
  • #24: Git diff HEAD will show diff between working tree and staging area vs. the repo
  • #28: $ git remote -v origin https://github.com/rea2000/santalist.git (fetch) origin https://github.com/rea2000/santalist.git (push)