01 Version Control
01 Version Control
uOttawa.ca
Overview
• Overview:
– Key Concepts
– Installation and Setup
– GitHub Introduction
– Basic Git WorkFlow
– Utility Commands
2
THE BASICS
3
Source Control? Version Control?
4
Why Git?
5
Core Concepts
• Repository contains:
– Files
– History
– Special configurations
6
Core Concepts (cont’d)
• Remote Repository:
– The remote repository is just another repository of the same workspace,
on the remote server.
– Last step in the basic Git Workflow
• Master Branch
– Default branch that contains your changes
– Git provides a default branch named master
7
Git - Command Line
• Git was originally designed as a command line tool, and graphical support
came later.
• Features make it onto the command line before they are integrated into a
graphical client
• Documentation
• More power!
• Consistency:
– Terminal on Mac/Linux
– Git Bash on Windows
8
Git Installation
• Windows
– https://git-scm.com/download/windows
• Mac OS X
– https://git-scm.com/download/mac
– Or type git version on the terminal
• Linux
– Use your package manager to install git, e.g.: “apt-get install git”
9
GitHub – Create account
• Navigate to GitHub.com
– You can sign in or sign up.
– Select the 'Unlimited public
repositories' free plan if you
sign up.
– Follow the steps to verify
your account
10
GitHub – First repository
11
Prepare your local system
12
Cloning a Repository
• Confirm that a folder named “my_first_github” has been created and that
you have the README file . You can add a creation folder name after the
repository name to specify the name of the folder you’d like to create
13
Our First commit
14
Our First commit (cont’d)
15
Our First commit (cont’d)
16
Publishing changes to GitHub
17
Make Modifications to Files
1. Add, Commit and Push your changes. Commit message should be ‘Second
commit’.
18
Comparing two commits
• You can check the changes made by every commit
by going to your project page on Github ->
commits -> then selecting the commit you want to
analyze
• Portions
removed are
marked in
red and
those added
are marked
in green.
19
Pulling Remote Updates
• If someone else has made changes to your project and has sent them to
the remote server, in order to be able to synchronize, you need to run a
pull command
• Trying to push commits when the remote contains changes not visible
locally will cause the push to fail. You need to integrate the changes before
submitting your own.
• If there are not conflicts, using “git pull” will download the outstanding
changes without issue. Then, you will be able to push your own changes.
• Cases where multiple team members change the same files will be treated
in the following slides, during discussion of “merging”.
INTERMEDIATE CONCEPTS
21
Starting With an Existing Project
1. Consider a project where you already have source code. If you have a project in
your computer you want to use, make a copy of it in a separate folder. You can
also create a new folder, create a test text file for this example.
1. To place this folder under version control with Git, we use the init command.
git init
– At this point, Git has initialized an empty Git repository in the current folder.
3. Type git status to check the current state of the (local) repository.
22
Commit Untracked Files
Don’t push it! We still want to play with the staging area!
23
Logs!
24
Making changes to Staging area that will be
reverted
• Make any changes to the LICENSE.MD file (add some text to it)
25
Fully Reverting Changes (Backing out)
26
Push changes to GitHub
• Before pushing any changes remotely, you need to connect first your local copy
with you GitHub remote repository.
• Create a repository online for you to send your new local repository.
– Do not use the repo from the previous example.
• Type:
– git remote add origin your_url
– In my case:
“git remote add origin
https://github.com/wassimuottawa/github_from_existing.git”
27
ADVANCED CONCEPTS
28
Branches
• A branch is a timeline of
commits
29
Creating a Branch
• Running a “git branch <new branch name>” command will create a new
branch
30
Merging Scenarios
1. Fast-Forward:
• Simplest case
• No additional work has been detected on the Master (parent) Branch
31
Merging Scenarios (cont’d)
2. Automated
• Non-Conflicting states
• Preserves both Timelines
32
Merging Scenarios (cont’d)
3. Manual
• Conflicting Merge State
• Git is unable to automatically resolve any conflicts
• Merge conflicts must be resolved before doing the commit
• Some tools can assist in solving file conflicts. 3-Way file diff is useful
when two people make multiple changes to the same file.
33
Branching
• Now, modify the README file and add, commit and push it.
– git add .
– git commit –m “committing to another branch”
– git push origin Miguel
34
Merging
4. The merge is still local on your machine. If you want to push it to the
remote repository, do a “git push origin master” command
35
Example Conflict File after Merge
<<<<<<< HEAD
miguel
=======
felipe
>>>>>>> 42708f7a9040d44b00998d8846be0bed0fc5ca3a
• After editing the file to fix the conflicts (or using a conflict resolution tool),
Student 2 adds the file with conflict to the staging area, makes another
commit and successfully pushes his changes to the remote.
THANK YOU
37