2. 2
Agenda
1. Git
1. Core concepts
2. Installation
3. Basic usage
2. Jenkins
1. What’s it? Why do we need it?
2. Terminologies
3. Jenkins architecture
3. Demo (integrate Jenkins with Git)
3. 3
1 What’s Git?
The most commonly used version control
system (VCS) today.
Distributed VCS, meaning your local copy of
code is a complete version control
repository.
These fully-functional local repositories
make it is easy to work offline or remotely.
4. 4
1.1 Core concepts
Repository
● Collection of files and their history organized in folders, branches, tags
● Is stored in a normal file system somewhere
● Can be on a local machine or a remote machine
● To work with GIT you will need a repo somewhere
● When creating a new one… it’s empty
5. 5
1.1 Core concepts (cont.)
Index (Staging area)
● Snapshot of files in the workspace.
● You add to the index files you change / remove etc.
Commit
● A snapshot of the workspace at some point
● Has unique revision number
● Knows the commit (commits) that it’s based on
10. 10
1.3 Basic usage - Create repository
Initialized a repo:
git init
Clone – creates a local repository by copying another (remote) repository
git clone <repo_url>
Clone existing repo into directory
git clone <repo_url> <directory_name>
11. 11
1.3 Basic usage - Working with files
• Add changes to index (staging area):
git add <file_path>
git add –A
• Unstage files:
git reset HEAD <file_path>
• Reset all changes:
git reset --hard
• Discard unstaged changes in a file:
git checkout <file_path>
• Commit:
• git commit –m “message”
• git commit –am “message”
12. 12
1.3 Basic usage - View changes
Show status of files:
git status
Show unstage changes:
git diff
git difftool
Show staged changes:
git diff --cached
git difftool --cached
13. 13
1.3 Basic usage - View history
Show log:
git log [-n] [--pretty] [--graph]
-n <number_of_commit>: show n commit
--pretty=<format>: display format. See pretty format here
--graph: show history in diagram
Ex: git log -n 10 --pretty=short—graph
There’re many GUI tools: SourceTree, GitHub Desktop, Git Extensions
14. 14
1.3 Basic usage - Branches
Branch is a pointer to a commit
A branch can point at other commits – it can move
A branch is a way to organize working histories
The default branch name in Git is master
There is 2 types of branch: local and remote.
Remote branches represent the state of a branch on the repository it is fetched
from
You cannot checkout a remote branch. You create a local pointer to it
15. 15
1.3 Basic usage - Branches
When doing a “commit” the current “branch” moves to point at the new
commit
C3 C2 C1bx
C3 C2 C1bx C4
16. 16
1.3 Basic usage - Branches
Here is a branch
Creating a new branch just adds another “pointer” to the same commit
C3 C2 C1bx
C3 C2 C1
bx
by
17. 17
1.3 Basic usage - Branches
Other branches are not affected by a commit
C3 C2 C1
bx
by
C3 C2 C1
bx
by
C4
18. 18
1.3 Basic usage - Branches (cont.)
List all local branches:
git branch
List all remote branches:
git branch –r
Create new branch:
git branch <branch_name>
Switch branch:
git checkout <branh_name>
Create new branch and switch to it:
git checkout -b <branch_name>
Delete branch:
git branch –d <name>
Safe operation, prevents deleting branch
unmerged.
git branch –D <name> :
Force delete branch
Rename branch
git branch –m <new_name>
19. 19
1.3 Basic usage - Merging
Merge the specified branch into
the current branch
● git merge <branch_name>
Merge the specified branch into
the current branch, but always
generate a merge commit:
● git merge --no-ff <branch_name>
20. 20
1.3 Basic usage - Resolving Conflicts
If the two branches you're trying to merge both changed the same part of the same
file => conflict => stop merge process
CONFLICT (content): Merge conflict in <file_paths>
Automatic merge failed; fix conflicts and then commit the result.
Using merge tool to resolve conflicts:
git mergetool : open merge tool so that you can resolve the conflicts manually.
git commit : commit merge change and finish merge action.
21. 21
1.3 Basic usage - Remote
Remotes are just symbolic
names
You can have as many as you
like
The default name is origin if
you’ve cloned
Remote-tracking branches are
locally immutable
My Machine The Server
C1
master
C0
cloneC1
origin/master
C0
master
22. 22
1.3 Basic usage - Fetch
Fetch – gets changes from a remote repository that you don’t already have.
Fetch gets the changes to the local repository but does not touch the index or
workspace.
After a fetch, usually a merge needs to take place
git fetch
git merge <remote>/<branch>
23. 23
1.3 Basic usage - Fetch (cont.)
My Machine
The Server
C1
C0
master
C2
C1
origin/master
C0
master
fetch
My Machine The Server
C1
C0
master
C2
C1
origin/master
C0
masterC2
24. 24
1.3 Basic usage - Pull
Pull is just a shortcut command to do:
1. Fetch from a remote
2. Merge changes from “remote branch” into the “remote tracking branch”
Sometimes the tool would also allow to “commit” merges for you right after the
pull (not part of pull, but a helper)
git pull
25. 25
1.3 Basic usage - Push
Will take local objects (commits, tags) which are required to make a remote
branch complete – and send them.
Will merge those local changes into the remote branch
Will only do a “fast-forward” merge (other merge type, if required, will fail the
push)
Push to existing branch
git push
Push new branch
git push –u <remote> <branch>
26. 26
1.3 Basic usage - Push (cont.)
My Machine The Server
C1
C0
master
C2
C1
origin/master
C0
master
C2
C3
My Machine The Server
C1
C0
master
C2
C1
origin/master
C0
master
C2
C3 C3
27. 27
1.3 Basic usage - Push (cont.)
If cannot do a fast-forward – will fail. Then, a fetch + merge (or pull) is required to
allow the push.
Once remote changes merged locally again a fast forward is possible and the push
would work
28. 28
1.3 Basic usage - Push (cont.)
My Machine The Server
C1
C0
master
C2
C1
origin/master
C0
master
C2
C3 C4
push
My Machine The Server
C1
C0
master
C2
C1
origin/master
C0
master
C2
C3
C4
C4
C5
29. 29
1.3 Basic usage - Push (cont.)
My Machine The Server
C1
C0
master
C2
C1
origin/master
C0
master
C2
C3
C4
C4
C5
push
My Machine The Server
C1
origin/master
C0
master
C2
C3
C4
C5
C1
C0
master
C2
C3
C4
C5
30. 30
2.1 What’s Jenkins?
Continuous integration server - detects changes
in Subversion, performs tasks, repeatedly (build,
test, deploy, package, etc.)
An open source continuous integration tool
written in Java.
It is a server-based system running in a servlet
container such as Apache Tomcat
31. 31
2.1 Why do we need?
To integrate more frequently, detect errors quicker, improve quality and reduce
cost
Co-ordinate the running of tasks as part of workflows.
Compile, test and package, deploy, script, verify
32. 32
2.2 Terminology
Job - a unit of work for a project
View - user defined collection of jobs or a workflow
Master - the central Jenkins master, does job scheduling
Slave - executes one or more jobs within slots (executors)
Workspace - the working area where a job is carried out
35. 35
2.3 Jenkins Features
Trigger a build
Get source code from repository
Automatically build and test
Generate report & notify
Deploy
Distributed build
Quickly becoming the standard for version control
You commit your work locally, and then sync your copy of the repository with the copy on the server. This paradigm differs from centralized version control where clients must synchronize code with a server before creating new versions of code.
Git views untracked and modified files similarly. Untracked means that the file is new to your Git project. Modified means that the file has been seen before, but has been changed, so is not ready to be snapshotted by Git. Modification of a file occurs in your working directory
When a file becomes staged, it's taken into the staging area. This is where Git is able to take a snapshot of it and store its current state to your local repository. This area is also known as the Index
Committed means that Git has officially taken a snapshot of the files in the staging area, and stored a unique index in the Git directory. The terms snapshotted and committed are very similar
Note that all of the commands presented below merge into the current branch. The current branch will be updated to reflect the merge, but the target branch will be completely unaffected