week 12
week 12
week 12
1
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…
2
Problems Working in teams
Whose computer stores the "official" copy of the project?
• Can we store the project files in a neutral "official" location?
Wiki’s
• Wiki’s are all about version control, managing updates, and allowing
rollbacks to previous versions
4
Software Version control
• Many version control systems are designed and used especially for
software engineering projects
examples: CVS, Subversion (SVN), Git, Monotone, BitKeeper, Perforce
not particular to source code; can be used for papers, photos, etc.
• but often works best with plain text/code files
5
version Control
• Version Control
6
Source Code Management
7
Version control tools
8
What is Git?
• Git is a popular version control system. It was created by Linus Torvalds in 2005, and has been
maintained by Junio Hamano since then.
• It is used for:
Tracking code changes
Tracking who made changes
Coding collaboration
• What does Git do?
Manage projects with Repositories
Clone a project to work on a local copy
Control and track changes with Staging and Committing
Branch and Merge to allow for work on different parts and versions of a
project
Pull the latest version of the project to a local copy
Push local updates to the main project
9
Working with Git
Initialize Git on a folder, making it a Repository
Git now creates a hidden folder to keep track of changes in that folder
When a file is changed, added or deleted, it is considered modified
You select the modified files you want to Stage
The Staged files are Committed, which prompts Git to store a permanent
snapshot of the files
Git allows you to see the full history of every commit.
You can revert back to any previous commit.
Git does not store a separate copy of every file in every commit, but keeps
track of changes made in each commit!
10
Why Git?
Git Install:
You can download Git for free from the following website:
https://www.git-scm.com/
12
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)
13
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.
14
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
• Options:
attu, CSE GitLab, GitHub (do NOT use GitHub for homework!!!)
15
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
16
Git Resources
• At the command line: (where <verb> = config, add, commit, etc.)
$ git help <verb>
$ git <verb> --help
$ man git-<verb>
17
Centralized vs Distributed
18
GiT Features
19
Git file lifecycle
20
GiT Installation
21
Basic Workflow
Basic Git workflow:
• Notes:
If a particular version of a file is in the git directory, it’s considered committed.
If it’s modified 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.
22
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)
• 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 23
Create a local copy of a
2.
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 files from the repo, and a .git directory
which you can ignore (used to hold the staging area and your actual repo)
Example: git clone
[email protected]:rea/superTest.git
25
Git operations & Commands
• Now, we use GiT for these two
files “home.html and page.html”
using GiT Initiate
• GiT init command (GiT named
folder is created in working
directory)
• Staging Area command
git add home.html
after changes in html file
run git diff command
• Now, we can track changes
26
Git operations & Commands
• “Git status” command will tell you the number of added
files in staging area
• How to push and pull files on Local and Remote
Directories from staging area?
git commit-a-m (push all files to local repository and an attached
message what,where and when to commit changes in each file-
track changes)
Then, push files to remote repository, for this sign up to GiT Hub
Create Repository with “New” option. https://github.com/join
New/name of repository/public or private/ Create (only one)
Use URL of Repository to push files from LR to RR.
27
Git operations & Commands
• Add Repository in Git using command (can add multiple..)
“git remote add <repository name> <repository url> ”
• example git remote add origin https://github.com....
• Now, push files from LR to RR, use the command
“git push origin master”
credentials for the first time
• Add a new branch using command
git branch fix
git branch command will show the total created branches
• fix and master
28
Branching
29
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
31
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.
$
32
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.
33
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)
34