Git for beginners

- 5 mins

What is Git ?

Git is a source code management system. To put it simply, there comes a need for management when there are more than one (even 100) of contributors writing code/contributing patches to a project. This where git kicks in and does the magic.

Git also provides you a mechanism of saving the “state” of the project. Saving the “state” means that you can take a snapshot of the project once you are done with your changes to related files. You can have a series of “snapshots” of the project which will enable you to keep different versions of the project without introducing any kind of redundancy i.e. you will have one copy of the project but still you can move back and forth between the states of the project.

Markdowm Image

Snapshot Concept (Courtesy: atlassian.com)

You’ll probably understand git better once you start using it. Let’s get started.

##Installation:

Well, I will assume that you are a programmer and have some familiarity to linux environment command line.

On Ubuntu/Debian based systems:

$> sudo apt-get install git

On Fedora/RHEL based systems:

$> dnf install git

###Setting up git

$ git config --global user.name "YOUR NAME"
$ git config --global user.email "YOUR EMAIL ADDRESS"

##Initialize a git directory We will first initialize a git directory. Initializing a git directory means telling the git that start tracking the changes in the directory.

$> mkdir test
$> cd test
$> git init     //initialize git to test directory 

###Create a demo file Go ahead and create a ‘demo.txt’ file in the ‘test’ directory we created above. This is just a example file for understanding how git works. After creating the file, enter the following line:

This is the first line of demo.txt 

Now go to the terminal and query the status of the directory. To do that:

$> cd test           //make sure you are in the 'test' directory
$> git status        // will output the status of your git directory

####Output: You will see the following output:

On branch master

Initial commit

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

	"demo.txt"

Congratulations ! You have learned your first git command. ‘demo.txt’ is listed in ‘Untracked files:’ because git is aware that a new file has to added to its directory. Isn’t that magic ? (You’ll see more magic by git later)

Go ahead and tell git that start tracking the file “demo.txt”.

$> git add demo.txt

Now, git will not only track each and every activity in the ‘test’ directory but also on the file demo.txt you just added.

You can again query the status of directory (git status) and see the output yourself. :)

###First Snapshot (Commit) Now let’s take the first snapshot of the directory. In technical terms, the snapshot is known as ‘commit’. It will save the state of our ‘test’ directory. For understanding purposes, I will go ahead and describe the state of our directory here:

The concerned git directory has the name 'test'
The directory has one file (in total)
The file name is demo.txt and has only one line.
The line in demo.txt is : "This is the first line of demo.txt"

This describes the basic snapshot of the directory.

###Saving the snapshot (git commit)

$> git commit -m "commit message: Initial commit"

Congratulations ! You have made your first commit. You can run ‘git log’ to see your commits.

##Further Steps Let’s recap what we done till now:

You’ll see the real magic of git now. Go ahead and edit demo.txt. I will append the following line in demo.txt

I have made my first commit.
This are the new lines after first commit.

Again query the status of directory : git status

###Output

modified:   demo.txt

To see what has changed since last commit (i.e. since last snapshot):

$> git diff

##Output

diff --git a/demo.txt b/demo.txt
index 182dab0..33c0668 100644
--- a/demo.txt
+++ b/demo.txt
@@ -1 +1,4 @@
 This is the first line of demo.txt
+I have made my first commit
+This are the new lines after the first commit.
+

If you want to save the latest snapshot (see above code block), go ahead with git add and git commit.

Therefore, play with the file in the directory. Add some more files, edit them and see the differences with “git diff” command. You’ll understand why git is magical ;-)

##Common workflow for git

The common workflow you’ll mostly use :

  1. Edit the files in the git directory
  2. git diff (to see the edits you made to directory)
  3. git add file1 file2 …
  4. git commit -m “commit_message”
  5. git log (to see all your commits)

The output of git diff here will be probably very small here as you are just using them from learning purposes. Here is a look how “git diff” makes life much more easier when you will be editing code of some project and see the edits you made.

Markdowm Image

Typical diff

The term “patch” is more or less the diff only.

Here is an example of a patch.

So this is what you are going to do 90% of the time while playing with source code of any projects. I would like to mention here that this is only the tip of the iceberg. There are lot more advanced things you can do with git which can help you become better programmer. :-)

Stay tuned. Happy hacking!

Umang Jain

Umang Jain

Makes the machines talk

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin google pinterest medium