Git is an Open Source Distributed Version Control System, that came out of Linux development community. Git has a remote repository which is stored in a server and a local repository which is stored in the computer of each developer. This means that the code is not just stored in a central server, but the full copy of the code is present in all the developers’ computers. Git is a Distributed Version Control System since the code is present in every developer’s computer.
- Strong support for non-linear development
- Distributed development
- Compatibility with existent systems and protocols
- Efficient handling of large projects
- Cryptographic authentication of history
- Toolkit-based design
- Pluggable merge strategies
- Garbage accumulates until collected
- Periodic explicit object packing
A central repository stores everything in one place. In a centralized model, you work on the code while connected to the server itself. This maintains a single source of truth. The example of centralized model are CVS, Subversion and Perforce.
In a distributed model, every developer has their own repo. The example of distributed model are Git and Mercurial.
Git has two data structures:
- a mutable index (also called stage or cache) that caches information about the working directory and the next revision to be committed
- an immutable, append-only object database
Groups of Git commands are:
- Setup and branch management
init, checkout, branch
add, delete, rename, commit
- Get information
status, diff, log
- Create reference points
Introduce yourself to Git
By entering these lines (with appropriate changes) you can configure the Git with your names:
git config --global user.name "John Smith" git config --global user.email firstname.lastname@example.org
You only need to do this once.
If you want to use a different name/email address for a particular project, you can change it for just that project
cd to the project directory, use the above commands, but leave out the
Create a repository
For ceating a Git repository, go to the project directory and follow these steps:
- Type in
- This creates the repository (a directory named .git)
- You seldom (if ever) need to look inside this directory
- Type in
git add .
- The period at the end is part of this command!
- Period means “this directory”
- This adds all your current files to the repository
- Type in
git commit –m "Initial commit"
- You can use a different commit message, if you like
Clone a repository
For cloning a remote Git repository, you can use the
clone command, as follows:
git clone URL
these make an exact copy of the repository at the given URL into a directory with the same name under the current directory
git clone URL mypath
these make an exact copy of the repository at the given URL into the local path mypath
All repositories are equal, But you can treat some particular repository (such as one on Github) as the “master” directory Typically, each team member works in his/her own repository, and “merges” with other repositories as appropriate
Your top-level working directory contains everything about your project
- The working directory probably contains many subdirectories—source code, binaries, documentation, data files, etc.
- One of these subdirectories, named
.git, is your repository
At any time, you can take a “snapshot” of everything (or selected things) in your project directory, and put it in your repository
- This “snapshot” is called a commit object
- The commit object contains
- a set of files
- references to the “parents” of the commit object
- a unique “SHA1” name
- Commit objects do not require huge amounts of memory
You can work as much as you like in your working directory, but the repository isn’t updated until you
If you create new files and/or folders, they are not tracked by Git unless you ask it to do so
git add newFile1 newFolder1 newFolder2 newFile2
Committing makes a "snapshot" of everything being tracked into your repository
git commit –m "a message telling what you have done is required"
- This version opens an editor for you the enter the message
- To finish, save and quit the editor
Commits and graphs
A commit is when you tell git that a change (or addition) you have made is ready to be included in the project
When you commit your change to git, it creates a commit object:
- A commit object represents the complete state of the project, including all the files in the project
- The very first commit object has no “parents”
- Usually, you take some commit object, make some changes, and create a new commit object; the original commit object is the parent of the new commit object
- Hence, most commit objects have a single parent
- You can also merge two commit objects to form a new one
- The new commit object has two parents
Hence, commit objects form a directed graph
Git is all about using and manipulating this graph
Working with Local Repository
A head is a reference to a commit object. The "current head" is called HEAD (all caps).
Usually, you will take HEAD (the current commit object), make some changes to it, and commit the changes, creating a new current commit object. This results in a linear graph: A → B → C → …→ HEAD
You can also take any previous commit object, make changes to it, and commit those changes. This creates a branch in the graph of commit objects.
Also, you can merge any previous commit objects. This joins branches in the commit graph.
In git, "Commits are cheap". Do them as often you can. When you commit, you must provide a one-line message stating what you have done
- Terrible message: “Fixed a bunch of things”
- Better message: “Corrected the calculation of median scores”
Commit messages can be very helpful, to yourself as well as to your team members. You can’t say much in one line, so commit often.
Choose an editor
When you commit, git will require you to type in a commit message. For longer commit messages, you will use an editor. The default editor is probably vim. To change the default editor:
git config --global core.editor /path/to/editor
You may also want to turn on colors:
git config --global color.ui auto
Working with others
All repositories are equal (distributed model), but usually there are one central repository in the cloud. Here’s what you normally do for working in a team:
- Download the current HEAD from the central repository
- Make your changes
- Commit your changes to your local repository
- Check to make sure someone else on your team hasn’t updated the central repository since you got it
- Upload your changes to the central repository
and If the central repository has changed since you got it:
- It is your responsibility to merge your two versions
This is a strong incentive to commit and upload often!
- Git can often do this for you, if there aren’t incompatible changes
the typical workflow looks as follows:
git pull remote_repository
Get changes from a remote repository and merge them into your own repository
See what is the current status
- Work on your files (remember to
addany new ones)
git commit –m “changes description”