Version control systems track changes to source code over time by keeping a complete history of file versions in a database. Version control provides benefits like a change history, branching and merging, and traceability. Git is a distributed version control system that allows developers to work offline and collaborate by sharing changes. Developers use branches to work independently on features or bugs, then merge branches to combine changes.
3. WHAT IS VERSION CONTROL?
Version control systems are a category of software tools that help a
software team manage changes to source code over time. Version
control software keeps track of every modification to the code in a
special kind of database.
▸ Benefits of version control
A complete long-term change history of every file
Branching and merging
Traceability
3
8. WHAT IS GIT?
▸ Git was initially designed and
developed in 2005 by Linux kernel
developers (including Linus
Torvalds) for Linux kernel
development.
▸ Having a distributed architecture.
Rather than have only one single
place for the full version history of
the software as is common in once-
popular version control systems
like CVS or Subversion.
8
10. GIT VS SUBVERSION PERFORMANCESeconds
0
350
700
1050
1400
PUSH/COMMIT PULL/CHECKOUT
GIT SUBVERSION GIT SUBVERSION
39.1
1,334.8
28.5104.5
Commit and checkout times of a repository of 2757 files and 428 directories, summing up to 26MB.
By: http://j.mp/1VY9KIl / CC BY-SA
10
11. GIT SECURITY
▸ Git has been designed with the integrity of managed
source code as a top priority. The content of the files as
well as the true relationships between files and directories,
versions, tags and commits, all of these objects in the Git
repository are secured with a cryptographically secure
hashing algorithm called SHA1.
▸ Some other version control systems have no protections
against secret alteration at a later date. This can be a
serious information security vulnerability for any
organization that relies on software development.
11
12. GIT IS A DE FACTO STANDARD
▸ The predominance of Git also means that many third party
software tools and services are already integrated with Git
including IDEs and tools like DVCS desktop clients:
▸ SourceTree (Windows/Mac)
▸ SmartGit (Windows/Linux/Mac)
▸ issue and project tracking software:
▸ JIRA
▸ and code hosting services
▸ GitHub
▸ Bitbucket
12
13. GIT VS SUBVERSION. GOOGLE TRENDS
Interest over Time (Git vs Subversion)
http://j.mp/23K41sb / Google Trends
13
15. SETTING UP A REPOSITORY
INSTALLING GIT
▸ Linux: apt-get/yum/dnf/zypper install git
▸ Windows and Mac OS X
https://git-scm.com/downloads
15
16. SETTING UP A REPOSITORY
git config
The git config command lets you configure your Git installation
from the command line. This command can define everything from
user info to preferences to the behavior of a repository.
‣ git config --global user.name <name>
‣ git config --global user.email <email>
‣ git config --global alias.<alias-name> <git-command>
‣ git config --system core.editor <editor>
‣ git config --list
16
17. SETTING UP A REPOSITORY
git init <directory>
‣ The git init command creates a new Git repository. It can be used
to convert an existing, unversioned project to a Git repository or
initialize a new empty repository. Most of the other Git
commands are not available outside of an initialized repository,
so this is usually the first command you’ll run in a new project.
‣ Executing git init creates a .git subdirectory in the project root,
which contains all of the necessary metadata for the repo. Aside
from the .git directory, an existing project remains unaltered
(unlike SVN, Git doesn't require a .git folder in every
subdirectory).
17
18. SETTING UP A REPOSITORY
git clone <repo>
‣ The git clone command copies an existing Git repository. This
is sort of like svn checkout, except the “working copy” is a full-
fledged Git repository—it has its own history, manages its own
files, and is a completely isolated environment from the original
repository.
‣ The original repository can be located on the local filesystem or
on a remote machine accessible via HTTP or SSH.
18
21. SAVING CHANGES - SNAPSHOTS, NOT DIFFERENCES
Storing data as changes to a base version of each file
Storing data as snapshots of the project over time
20
22. SAVING CHANGES - THE THREE STATES
▸ Committed
Committed means that the data is safely stored in your
local database.
▸ Modified
Modified means that you have changed the file but have
not committed it to your database yet.
▸ Staged
Staged means that you have marked a modified file in
its current version to go into your next commit snapshot.
21
23. SAVING CHANGES - THE THREE STATES
Working directory, staging area, and Git directory
22
24. SAVING CHANGES
git add <file/directory/.>
‣ The git add command adds a change in the working directory
to the staging area. It tells Git that you want to include updates
to a particular file in the next commit. However, git add doesn't
really affect the repository in any significant way—changes are
not actually recorded until you run git commit.
‣ In conjunction with these commands, you'll also need git
status to view the state of the working directory and the staging
area.
23
25. SAVING CHANGES - CHECKING THE STATUS OF YOUR FILES
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
24
26. SAVING CHANGES - CHECKING THE STATUS OF YOUR FILES
$ echo 'My Project' > README
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will
be committed)
README
nothing added to commit but untracked files present
(use "git add" to track)
25
27. SAVING CHANGES - CHECKING THE STATUS OF YOUR FILES
$ git add README
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: README
26
28. SAVING CHANGES
IGNORING FILES
▸ From time to time, there are files you don't want Git to
check in.
▸ If you create a file in your repository named .gitignore, Git
uses it to determine which files and directories to ignore,
before you make a commit.
$ cat .gitignore
*.[oa]
log/*.log
27
29. SAVING CHANGES
git commit
The git commit command commits the staged snapshot to the
local repository. Git will never change them unless you explicity
ask it to. Along with git add, this is one of the most important Git
commands. This command is nothing like svn commit.
$ git commit -m "Story 182: Fix benchmarks for speed"
[master 463dc4f] Story 182: Fix benchmarks for speed
2 files changed, 2 insertions(+)
create mode 100644 README
28
31. SAVING CHANGES
git checkout
The git checkout command serves three distinct functions:
checking out files, checking out commits, and checking out
branches
$ git checkout <branch>
$ git checkout <commit> <file>
$ git checkout <commit>
30
32. SAVING CHANGES
git revert <commit>
The git revert command undoes a committed snapshot. But,
instead of removing the commit from the project history, it figures
out how to undo the changes introduced by the commit and
appends a new commit with the resulting content.
31
33. SAVING CHANGES
git revert <commit>
The git revert command undoes a committed snapshot. But,
instead of removing the commit from the project history, it figures
out how to undo the changes introduced by the commit and
appends a new commit with the resulting content.
ResettingReverting
31
34. SYNCING
git fetch <remote> <branch>
‣ The git fetch command imports commits from a remote
repository into your local repo. The resulting commits are stored
as remote branches instead of the normal local branches that
we’ve been working with.
‣ Fetching is what you do when you want to see what everybody
else has been working on. Since fetched content is represented
as a remote branch, it has absolutely no effect on your local
development work.
32
35. SYNCING
git pull <remote>
‣ Merging upstream changes into your local repository is a
common task in Git-based collaboration workflows.
‣ You can think of git pull as Git's version of svn update. It’s an
easy way to synchronize your local repository with upstream
changes.
33
39. SYNCING
git push <remote> <branch>
‣ Pushing is how you transfer commits from your local repository
to a remote repo. It's the counterpart to git fetch, but whereas
fetching imports commits to local branches, pushing exports
commits to remote branches.
‣ After you’ve accumulated several local commits and are ready to
share them with the rest of the team
35
40. SYNCING
git tag -a <tag>
‣ Like most VCSs, Git has the ability to tag specific points in history
as being important. Typically people use this functionality to
mark release points (v1.0, and so on).
‣ Git uses two main types of tags: lightweight and annotated.
36
43. USING BRANCHES
git branch
‣ In Git, branches are a part of your everyday development
process. When you want to add a new feature or fix a bug—no
matter how big or how small—you spawn a new branch to
encapsulate your changes. This makes sure that unstable code is
never committed to the main code base
‣ A branch represents an independent line of development.
Branches serve as an abstraction for the edit/stage/commit.
‣ The git branch command lets you create, list, rename, and delete
branches
39
46. USING BRANCHES
git merge <branch>
‣ Merging is Git's way of putting a forked history back together
again.
‣ The git merge command lets you take the independent lines of
development created by git branch and integrate them into a
single branch.
41
49. COMPARING WORKFLOWS
CENTRALIZED WORKFLOW
44
Like Subversion, the Centralized
Workflow uses a central
repository to serve as the single
point-of-entry for all changes to
the project. Instead of trunk, the
default development branch is
called master and all changes
are committed into this branch.
This workflow doesn’t require
any other branches besides
master
50. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
SOMEONE INITIALIZES THE CENTRAL REPOSITORY
45
$ ssh user@host git init --bare /path/to/repo.git
51. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
EVERYBODY CLONES THE CENTRAL REPOSITORY
46
$ git clone ssh://user@host/path/to/repo.git
52. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
JOHN AND MARY WORK ON THEIR FEATURES
47
$ git status # View the state of the repo
$ git add <some-file> # Stage a file
$ git commit # Commit a file</some-file>
53. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
JOHN AND MARY WORK ON THEIR FEATURES
47
$ git push origin master
54. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
MARY TRIES TO PUBLISH HER FEATURE
48
$ git push origin master
55. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
MARY TRIES TO PUBLISH HER FEATURE
48
$ git push origin master
error: failed to push some refs to '/path/to/repo.git'
hint: Updates were rejected because the tip of your
current branch is behind
hint: its remote counterpart. Merge the remote changes
(e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push
--help' for details.
56. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
MARY REBASES ON TOP OF JOHN’S COMMIT(S)
49
$ git pull --rebase origin master
57. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
MARY REBASES ON TOP OF JOHN’S COMMIT(S)
49
$ git pull --rebase origin master
58. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
MARY RESOLVES A MERGE CONFLICT
50
CONFLICT (content): Merge conflict in <some-file>
59. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
MARY RESOLVES A MERGE CONFLICT
51
$ git status
60. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
MARY RESOLVES A MERGE CONFLICT
51
$ git status
# Unmerged paths:
# (use "git reset HEAD <some-file>..." to unstage)
# (use "git add/rm <some-file>..." as appropriate to
mark resolution)
#
# both modified: <some-file>
61. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
MARY RESOLVES A MERGE CONFLICT
52
#! /usr/bin/env ruby
def hello
<<<<<<< HEAD
puts 'hola world'
=======
puts 'hello mundo'
>>>>>>> master
end
hello()
62. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
MARY RESOLVES A MERGE CONFLICT
52
#! /usr/bin/env ruby
def hello
puts 'hola world'
end
hello()
$ git add <some-file>
$ git rebase --continue
$ git rebase --abort
63. COMPARING WORKFLOWS - CENTRALIZED WORKFLOW
MARY SUCCESSFULLY PUBLISHES HER FEATURE
53
$ git push origin master
64. COMPARING WORKFLOWS
FEATURE BRANCH WORKFLOW
54
The core idea behind the Feature
Branch Workflow is that all feature
development should take place in a
dedicated branch instead of the
master branch. This encapsulation
makes it easy for multiple developers
to work on a particular feature
without disturbing the main
codebase. It also means the master
branch will never contain broken
code, which is a huge advantage for
c o n t i n u o u s i n t e g r a t i o n
environments.
65. COMPARING WORKFLOWS - FEATURE BRANCH WORKFLOW
MARY BEGINS A NEW FEATURE
55
$ git checkout -b marys-feature master
$ git status
$ git add <some-file>
$ git commit
66. COMPARING WORKFLOWS - FEATURE BRANCH WORKFLOW
MARY MAKES THE CHANGES
56
$ git push -u origin marys-feature
68. COMPARING WORKFLOWS
GITFLOW WORKFLOW
58
The Gitflow Workflow defines a strict
branching model designed around
the project release. While somewhat
more complicated than the Feature
Branch Workflow, this provides a
robust framework for managing
larger projects. This workflow doesn’t
add any new concepts or commands
beyond what’s required for the
Feature Branch Workflow. Instead, it
assigns very specific roles to different
branches and defines how and when
they should interact.
by Vincent Driessen | http://j.mp/1YLriqr
69. COMPARING WORKFLOWS - GITFLOW WORKFLOW
HISTORICAL BRANCHES
59
The master branch stores the official release history, and the
develop branch serves as an integration branch for features.
70. COMPARING WORKFLOWS - GITFLOW WORKFLOW
FEATURE BRANCHES
60
Each new feature instead of branching off of master, feature
branches use develop as their parent branch
71. COMPARING WORKFLOWS - GITFLOW WORKFLOW
FEATURE BRANCHES
61
May branch off from:
‣ develop
Must merge back into:
‣ develop
Branch naming convention:
‣ anything except master, develop, release-*, or hotfix-*
72. COMPARING WORKFLOWS - GITFLOW WORKFLOW
RELEASE BRANCHES
62
Once develop has acquired enough features for a release, you
fork a release branch off of develop.
73. COMPARING WORKFLOWS - GITFLOW WORKFLOW
RELEASE BRANCHES
63
May branch off from:
‣ develop
Must merge back into:
‣ develop and master
Branch naming convention:
‣ release-*
74. COMPARING WORKFLOWS - GITFLOW WORKFLOW
MAINTENANCE BRANCHES
64
Maintenance or “hotfix” branches are used to quickly patch production
releases. This is the only branch that should fork directly off of master.
75. COMPARING WORKFLOWS - GITFLOW WORKFLOW
MAINTENANCE BRANCHES
65
May branch off from:
‣ master
Must merge back into:
‣ develop and master
Branch naming convention:
‣ hotfix-*
77. SVN TO GIT 67
http://john.albin.net/git/convert-subversion-to-git
1. Retrieve a list of all Subversion committers.
2. Clone the Subversion repository using git-svn.
3. Convert svn:ignore properties to .gitignore.
4. Push repository to a bare git repository.
5. Rename “trunk” branch to “master”
6. Clean up branches and tags.
7. Drink.