Git Basics

Why Source Control?

 

  • When working on a team, multiple changes need to be merged into the same codebase. Else it's an incoherent mess.
  • We almost never get anything right on the first attempt. Need to back-track.
  • Humans love assigning blame. Find out who made what change and why.

Evolution of source control

  1. Ctrl-C + Ctrl-V : This is where it started. Rudimentary, but got the job done.
  2. Centralised source control like SVN & Perforce: Much better and brought a lot more sanity
  3. Distributed source control like Git & Mercurial: No Single Point of Failure & more learnings

When do we source control?

 

Always!!!

Salient Features of Git

Distributed

  • No single point of failure
  • Every client is a mirror of the repository
  • If the master fails, it can be re-created from all the clients

Salient Features of Git

Data Integrity

  • All the files are check-summed and stored as SHA-1 IDs.
  • All these IDs are saved in the /your/repo/path/.git folder. Go view them!

Salient Features of Git

Differential Data

  • In order to conserve space, only the diff of the data is stored and transmitted. The diff is then patched on the previous changes to re-create the complete file.
  • Allows doing & un-doing changes without fear.
  • At all points you can recover any & all previous changes.
  • Data is stored as a Linked List so you can traverse across nodes of the list easily

Install Git

Ubuntu

$ sudo apt-get update

$ sudo apt-get install git

Mac OS X

 

Windows

Configure Git

Identity

$ git config --global user.name "Test Name"

$ git config --global user.email "[email protected]"

 

This is important because it allows other people to identify you. Build systems like Jenkins, Bamboo etc. use this identification to do a lot more.

 

You can customise the identity for a particular repo as well

$ cd /your/path/to/the/repo

$ git config user.name "Another Name"

$ git config user.email "[email protected]"

Configure Git

Default Editor

$ git config --global core.editor vim

 

In order to merge changes and view them. Personally, I use Eclipse

$ git config --global merge.tool vimdiff

To view all the configurations

$ git config --list

All the changes are stored in ~/.gitconfig file. Go check them out.

Initialize Git Repo

Let's create a new repo

$ mkdir ~/projects/test-project

$ cd ~/projects/test-project

$ git init
Initialized empty Git repository in ~/projects/test-project/.git/

The command "git init" creates a .git/ folder in the folder. This is where all of git's data is stored. If you delete this folder, you'll delete all the history and tracking information.

 

All configurations pertinent to this repo is stored in .git/config file. Open it in your favourite editor and check it out.

Git Terminology

Untracked files

Staging Area

Local Git tree

Remote Git tree

Every file/change starts without git knowing about it.

If you want to commit a change, you would put it in this staging area to tell Git that it should be interested in this.

When you commit the change, only the local linked list is modified. This design allows for very powerful semantics (covered later)

Finally, the change is pushed to the remote repository. It is now available to other devs on the team.

Untracked files

All files start out being untracked. These are new files that are created in the folder.

Let's create a new file in our folder

 

$ echo "Initial content" > README.md

$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	README.md

nothing added to commit but untracked files present (use "git add" to track)

Git knows what it doesn't know :D

Staging Area

The staging area is a buffer area where you can add files/changes to files before actually committing them. This feature allows you to add files one-by-one and inspect the changes before actually committing it.

 

$ git add README.md

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   README.md

Git now knows that these changes will be committed going forward.

Local Git Tree

Git tries to keep changes locally unless specifically asked to share with everyone. When we commit a file, it's only changed in the local git tree.

$ git commit -m "Initial commit"
[master (root-commit) 29c5223] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

$ git status
On branch master
nothing to commit, working directory clean

The flag -m is followed by the commit message. This should be populated by the reason for the change. Other devs will see this commit message to understand why this change was made.

Remote Git Tree

If you haven't already, head to Github and create a project there. We'll push our changes to the world!

$ git push origin master