1.1. What Is A Version Control System?: Source Code
1.1. What Is A Version Control System?: Source Code
1.1. What Is A Version Control System?: Source Code
Git
1.1. What is a version control system?
A version control system allows you to track the history of a collection of files
and includes the functionality to revert the collection of files to another
version. Each version captures a snapshot of the files at a certain point in
time. The collection of files is usually source code for a programming language
but a typical version control system can put any type of file under version
control.
The collection of files and their complete history are stored in a repository.
The user can copy an existing repository. This copying process is typically
called cloning in a distributed version control system and the resulting
repository can be referred to as clone.
Typically there is a central server for keeping a repository but each cloned
repository is a full copy of this repository. The decision which of the copies is
considered to be the central server repository is pure convention and not tied
to the capabilities of the distributed version control system itself.
Every clone contains the full history of the collection of files and a cloned
repository has the same functionality as the original repository.
Every repository can exchange versions of the files with other repositories by
transporting these changes. This is typically done via a repository running on
a server which is, other than the local machine of a developer, always online.
Git originates from the Linux kernel development and is used by many popular
Open Source projects, e.g. the Android or the Eclipse developer teams, as well
as many commercial organizations.
The core of Git was originally written in the programming language C, but Git
has also been re-implemented in other languages, e.g. Java, Ruby and Python.
If you want to delete a Git repository, you can simply delete the folder which
contains the repository.
Users with sufficient authorization can push changes from their local
repository to remote repositories. They can also fetch or pullchanges from
other repositories to their local Git repository.
For example, if you want to develop a new feature, you can create a branch
and make the changes in this branch without affecting the state of your files
in another branch.
Git supports that changes from different branches can be combined. This
allows the developer, for example, to work independently on a branch
called production for bugfixes and another branch called feature_123 for
implementing a new feature. The developer can use Git commands to combine
the changes at a later point in time.
For example, the Linux kernel community used to share code corrections
(patches) via mailing lists to combine changes coming from different
developers. Git is a system which allows developers to automate such a
process.
A standard Git repository contains the working tree (single checkout of one
version of the project) and the full history of the repository. You can work in
this working tree by modifying content and committing the changes to the Git
repository.
You need to mark changes in the working tree to be relevant for Git. This
process is called staging or to add changes to the staging area.
You add changes in the working tree to the staging area with the git
add command. This command stores a snapshot of the specified files in
the staging area.
The git add command allows you to incrementally modify files, stage them,
modify and stage them again until you are satisfied with your changes.
1.10. Committing to the repository
After adding the selected files to the staging area, you can commit these files
to permanently add them to the Git repository.Committing creates a new
persistent snapshot (called commit or commit object) of the staging area in
the Git repository. A commit object, like all objects in Git, are immutable.
The staging area keeps track of the snapshots of the files until the staged
changes are committed.
For committing the staged changes you use the git commit command.
The commit object points to the individual files in this commit via
a tree object. The files are stored in the Git repository as blobobjects and
might be packed by Git for better performance and more compact storage.
Blobs are addressed via their SHA-1 hash.
untracked: the file is not tracked by the Git repository, this means it was
neither staged, added to the staging area nor committed
dirty / modified: the file has changed but the change is not staged
3. Installation
4. Git Setup
4.1. Global configuration file
Git allows you to store global settings in the .gitconfig file located in the user
home directory. Git stores the committer and author of a change in each
commit. This and additional information can be stored in the global settings.
In each Git repository you can also configure the settings for this repository.
Global configuration is done if you include the --global flag, otherwise your
configuration is specific for the current Git repository.
You can also setup system wide configuration. Git stores theses values is in
the /etc/gitconfig file, which contains the configuration for every user and
repository on the system. To set this up, ensure you have sufficient rights, i.e.
root rights, in your OS and use the --system option.
The following configures Git so that a certain user and email address is used,
enable color coding and tell Git to ignore certain files.
4.2. User Configuration
Configure your user and email for Git via the following command.
You learn about the push command in Section 13.2, “Push changes to another
repository”.
If you want to query the global settings you can use the following command.
git config --global --list
# switch to home
cd ~/
Every Git repository is stored in the .git folder of the directory in which the
Git repository has been created. This directory contains the complete history
of the repository. The .git/config file contains the configuration for the
repository.
The following command creates a Git repository in the current directory.
All files inside the repository folder excluding the .git folder are the working
tree for a Git repository.
git status
5.6. Add files to the staging area
Before committing changes to a Git repository you need to mark those that
should be committed. This is done by adding the new and changed files to the
staging area. This creates a snapshot of the affected files.
Note
In case you change one of the files again before committing, you need to add it
again to the staging area to commit the new changes.
Afterwards run the git status command again to see the current status.
commit e744d6b22afe12ce75cbd1b671b58d6703ab83f5
Author: Lars Vogel <Lars.Vogel@gmail.com>
Date: Mon Feb 25 11:48:50 2013 +0100
Initial commit