One of the most important tools of a good developer is version control software or VCS. While many exist, one of the most popular ones is git.

Git allows you to use to effectively manage your code, but to appreciate its true power, one must contribute to open source projects.

This post will talk about the most basic commands of git and how to set up a git repository.


\(\\\)

Repository

A repository, also called a “repo” is just a storage for software. A git repository for your project would just be a folder where your project is stored and tracked by git. Along with your project files, it will also contain the .git folder which allows you to use git.

Tracking

Git keeps track of all the files in your git repository. What that means is that any changes to a file in that repo will be noted by git. This will be explained in detail later.


\[\\\]

Lets get started by setting up a git repo. Fire up your terminal. We’ll create a folder called mygitproject and then move into that folder.

:~$ mkdir mygitproject
:~$ cd mygitproject

We will create a text file inside called trackchanges.txt with the text: This is the original file. After typing the line in the console, hit Ctrl + D to return to the command prompt.

:~$ touch trackchanges.txt
:~$ cat > trackchanges.txt
This is the original file
:~$ 

\(\\\)

Lets check if trackchanges.txt has the line as expected.

:~$ cat trackchanges.txt
This is the original file
:~$ 

Next you will have to let git know that it is supposed to track any files inside that. For this, we must use the command git init. This command creates the .git folder inside of our new git repo. Please read this answer on stackoverflow for more information.

:~$ git init 
Initialized empty Git repository in /home/mygitproject/.git/

\(\\\)

Done. Now if you check what’s inside our project folder, which iniially had only the trackchanges.txt file, you’ll notice the .git folder I was talking about. This is a hidden folder, and you must use the command ls -a to view it.

:~$ ls -a
.  ..  trackchanges.txt .git
\[\\\]

Moving on, now that we have a repo where we can use git, lets see what git has to say about our project.

Type the command git status

:~$ git status
On branch master

No commits yet

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

        trackchanges.txt

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

The output of the status command may seem difficult to understand at first. The first line tells us that we are currently on the master or the main branch. We have made no ommit as of now.

The next few lines allow the introduction of another concept — the staging area.

\[\\\]

The Staging Area

The three main states that your project folder or file will be in are:

  • Committed
  • Modified
  • Staged

Lets go through these in brief.

Committed

This means that your file’s changes have been acknowledged by git and they have been safely stored locally. What this means is that any further changes you make will be analyzed with respect to the last committed state of your project.

Modified

This is the state in which your file is when it has been modified but not committed yet. That is, the changes have not been stored yet.

Staged

Your file is staged when it has been changed, but not yet committed and the changes that will go into the next commit have been saved. The file will now be tracked. Its a bit confusing, but it’ll become clear through our ongoing example.

The Staging area is basically a file in your .git folder where the current changes (which are to be included in your next commit will be saved.)

Check this for more information.


\[\\\]

Coming back to our example, when the output of the status command said that the trackchanges.txt file was untracked, it meant that the file is not being tracked or followed by git.

To add files to the staging area — so that they are tracked by git — we must use the add command.

To add all the files, you can use the add -A command.

:~$ git add trackchanges.txt
:~$

\(\\\)

Lets check the status of our project again

:~$ git status 
On branch master

No commits yet

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

	new file:   trackchanges.txt

Git has detected the new file (trackchanges.txt) and will monitor any changes made to it.

Now, the final step: commit the file to our local database. What this would do is remove the file from the staging area and save all changes up to this point. Any changes made to the file after committing would be treated as fresh changes. To commit our file, we use commit command.

\[\\\]

Commit messages

Commit messages are like comments in your code. They allow you, and others to know what changes were made in that particular commit at a glance. Needless to say, they should be properly worded and accurate.

The latest version of git allows you to commit with an empty commit message — if you use the git commit --allow-empty-message -m ' command. Olders versions of git also had ways to do it. Read this if you are interested.

If you just use the git commit command, you will be taken to the nano text editor to enter a commit message. You can avoid that by using the m tag after git commit and specifying your commit message in double-quotes.

:~$ git commit -m "First commit!"
[master (root-commit) ae582ab] First commit!
 1 file changed, 1 insertion(+)
 create mode 100644 trackchanges.txt

And we are done! If you check the status of our project again, you’ll notice that the staging area is empty and there are no untracked files.

:~$ git status
On branch master
nothing to commit, working tree clean

This is a basic example of using git in our project.