Overview

Hello folks!!! In this new post, I’ll talk a little bit about one the most famous CVS nowadays. We’ll learn some basics commands that will help you to get up and running in no time using this tool and start version all your changes in your source code. So without further ado, let’s get started and enjoy the reading 🙂

 

What is Git?

Git is one of the most used SCM tools these days. It allows managing source code easily an in a convenient way so developers can work more collaborative in any project. It was created by Linus Torvalds to help manage the code of Linux Operating System, but it’s been spread widely across multiple other projects. Currently, most of the software companies out there use it as its main Concurrent Versioning System or CVS. It’s main features are :

  • It has a great performance.
  • Branching a merging system.
  • Has a middle staging area.
  • Its distributed.
  • Its Free and open source.

It provides a great shell to work with that support a set of commands that make work with git a great experience. Yes, you can use a GUI tool (if you’re on Mac or Windows) like Sourcetree, but come on, we always love the old trusty shell, isn’t? So for this post, we’re going to learn shell commands.

For every developer that works with any programming language either with a company or as an individual, learning this tool is a must. You can use free tools on the web like GitHub or Bitbucket that lets you version your code and work collaboratively with others developers.

For more info, you can visit Git page.

First things first, Install Git

To install Git is very simple. Depending on your OS you can do:

Linux

sudo apt install git

 

Windows or Mac

For these 2 OS, you just need to go to the specific Git page, download the installer and run it. Simple.

Mac

Windows

 

Basic commands to start working

After you install Git is time to start with some serious commands. These are the basic ones that you need to know to work fairly efficient with Git.

Init a local repository

To initialize a local Git repository,  something like my-git-repo, run :

git init --bare my-git-repo.git

you’ll receive a message like

Initialized empty Git repository in /my-git-repo.git/.git/

You’ll see that a folder ./git has been created, and if you look inside, it will have a bunch of files and directories. Don’t worry about that for now.

drwxrwxr-x 7 4096 nov 8 20:42 ./
drwxrwxr-x 3 4096 nov 8 20:42 ../
drwxrwxr-x 2 4096 nov 8 20:42 branches/
-rw-rw-r-- 1   92 nov 8 20:42 config
-rw-rw-r-- 1   73 nov 8 20:42 description
-rw-rw-r-- 1   23 nov 8 20:42 HEAD
drwxrwxr-x 2 4096 nov 8 20:42 hooks/
drwxrwxr-x 2 4096 nov 8 20:42 info/
drwxrwxr-x 4 4096 nov 8 20:42 objects/
drwxrwxr-x 4 4096 nov 8 20:42 refs/

 

The basic workflow for Git.

The most basic workflow to use Git is that you create a branch, make some changes, add them to the stage area, commit your changes, push them to the remote repo and then merge those into the main branch.

In Git you will have a main branch call master, where all the final code should exist. It’s not recommended that you work directly in this branch. The good way to do it is that you create your own working branch, make your changes an then merge that branch into master. There is also a complete workflow in Git called Git Flow, but we are not talking about it here, that’s a topic for another post. ;).

An overall view of the process may be like this

 

Now, the first thing to start working is that you need to clone the remote repository into your local machine. For this tutorial (and only for this tutorial), my-git-repo.git that you just create will act as the remote repo. Don’t worry, if you work with a real service like GitHub, these commands works exactly the same.

 

Clone the remote repo

To clone the repo just run

git clone my-git-repo.git/ my-git-local-repo

You’ll have a new my-git-local-repo folder. This is your local repo to work with. But this one will not have the master branch yet so let’s create it first. To do that we need to use steps 3, 4 and 5 listed in the diagram from before. Don’t worry we’ll cover all of the steps later on.

cd into my-git-local-repo and follow these steps, I’ll explain those after this.

touch README.md
git add .
git commit -m 'First Commit'

 

After this, you’ll have your master branch created. To confirm that run

git branch

you’ll see

* master

Ok, that was a lot of fun, isn’t? Now let’s go step by step for the actual workflow.

 

Working with Git

So, now that you have your local repo all set up, we’re going through each of the steps listed before.

 

Create a working branch (Step 2).

To create the branch where you should make your changes run

git checkout -b my-branch

Now you’ll see that you’re on my-branch. Run git branch to confirm that.

git branch
 master
* my-branch

 

Make changes to your branch (Step 3).

Now, let’s make some changes. We can modify the file we already created, the README.md file. Just open it and add some text, maybe a title. In my case I’ll add “My first change in Git”. save those changes and then run:

git status

This will show you the all the files that are modified. The output should be like this:

On branch my-branch
Changes not staged for commit:
	(use "git add <file>..." to update what will be committed)
	(use "git checkout -- <file>..." to discard changes in working directory)

	modified:	README.md

no changes added to commit (use "git add" and/or "git commit -a")

As you can see, file README.md show as modified. The next step is to add it in the Stage area to be able to commit those changes.

 

Add changes to the Staging area. (Step 4).

The Stage area is useful to arrange and group your commits to having some order or meaning. You can add all the files at once to make a unique commit with all the changes or you can select specific ones so you can be grouped as you like. To do so run these commands:

To add all at once:

git add .

To add specific files

git add README.md

After adding them, run git status again and you will see:

On branch my-branch
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   README.md

Your changes are ready to be committed. If you want to remove a file from the staging area just run:

git reset HEAD README.md

With this, the file is removed from stagging and can’t be committed.

 

Commit your changes. (Step 5).

Ok, now to commit your changes to your local repo just run this:

git commit -m 'Title added to the README file'

You should always put some meaningful message that describes the changes. The output of this command is:

[my-branch b1731a5] Title added to the README file
 1 file changed, 1 insertion(+)

 

Merge your branch into master. (Step 6).

Now you need to merge those changes into the master branch. First, you need to be in master branch, so run:

git checkout master

will output:

Switched to branch 'master'

To merge the changes from your working branch into master run:

git merge my-branch master

Output:

Updating c258b24..bb45198
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)

 

Push changes to the remote repo. (Step 7).

The final step is to push all your changes to the remote repo, in this case, is my-git-repo.git. last las command to run is:

git push origin master

A confirmation output should be printed.

Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (6/6), 462 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To /home/davidokun/Documents/Development/my-git-repo.git/
 * [new branch]      master -> master

 

To confirm that your changes are in the remote branch, just cd into my-git-repo.git and run:

git log

This will show you all commits, the first one when we create the repo (git init) and the one from your local repo.

commit bb451984de9600c8d624ebd5f10938d0192b5762
Author: dark <darkadmin@darksideofthedev.com>
Date:   Fri Nov 10 21:12:16 2017 -0500

    Title added to the README file

commit c258b241153ad0bb25351c1c0ff214beb7e1754c
Author: dark <darkadmin@darksideofthedev.com>
Date:   Fri Nov 10 21:11:06 2017 -0500

    First Commit

 

Summary

In this post, we had a general view of what is Git and what is used for. We learn the basic workflow involved when versioning your code changes to have a trace of the work you do. And finally, we go step by step through this process learning each command to execute each step.

Hope this helps you with your everyday coding and I encourage you to open a GitHub account and start versioning your code. :). Until the next time.