DEV Community

Satvik Daga
Satvik Daga

Posted on

A Simple Introduction to git

Version Control Systems(VCS) are one of the most important things a developer needs to know about. Since its release in 2005, git has continued to be one of the most widely used VCS. So without further delay, lets get started with basics of git.

First thing to do is install git on whatever OS you are using. Let's see how it's done on Ubuntu.

$ sudo apt-add-repository ppa:git-core/ppa
$ sudo apt-get update
$ sudo apt-get install -y git
Enter fullscreen mode Exit fullscreen mode

Check that git is installed in your system. You should see something like this.

$ git --version
git version 2.20.1 (Apple Git-117)
Enter fullscreen mode Exit fullscreen mode

Great! Let's make your first commit.

Create a new project folder and initialize it as a git repository.

$ mkdir git-tutorial
$ cd git-tutorial
$ git init
Initialized empty Git repository in /home/sa.daga/src/demo/git-tutorial/.git/
Enter fullscreen mode Exit fullscreen mode

There are 3 different areas where code can exist in git:

  • Unstaged
  • Staged
  • Committed

Run the following command on the terminal in the git-tutorial directory.

$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
Enter fullscreen mode Exit fullscreen mode

git status command tells you which code is staged and which is unstaged.

Create a new file in the git-tutorial directory and again run git status.

$ echo "test1" > a.txt
$ cat a.txt
test1
$ git status
On branch master

No commits yet

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

    a.txt

nothing added to commit but untracked files present (use "git add" to track)
Enter fullscreen mode Exit fullscreen mode

As you can see, it shows that you have a file named a.txt in your directory and it is untracked. Untracked is git's way of saying that currently it does not care about the changes in this file. Changes in a.txt are currently in the unstaged area.

Note: Don't confuse untracked with unstaged. Files are untracked but changes are unstaged.

Now add this file to the staged area and again run git status.

$ git add a.txt
$ git status
On branch master

No commits yet

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

    new file:   a.txt
Enter fullscreen mode Exit fullscreen mode

a.txt has now moved to the staged area.
Let's modify a.txt and again run git status.

$ echo "test2" >> a.txt
$ cat a.txt
test1
test2
$ git status
On branch master

No commits yet

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

    new file:   a.txt

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:   a.txt
Enter fullscreen mode Exit fullscreen mode

As you can see, git status tells us that the a.txt has been modified. So the a.txt that contains test1 is in the staged area whereas change test2 is in the unstaged area.

Note: Output of git commands can contain information about what commands you can run next. Like in the output of above command, git displays commands that you can run if you want to unstage a staged change, add unstaged changes to staged area or discard changes in a file.

You can even see the modifications in each file by running git diff.

$ git diff
diff --git a/a.txt b/a.txt
index 9daeafb..8e042fb 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1,2 @@
 test1
+test2
Enter fullscreen mode Exit fullscreen mode

Output of git diff shows that test2 has been appended to a.txt in the next line of test1.
Let's add your first commit. Only staged changes are committed.

$ git commit -m "some commit message"
[master (root-commit) 4d75ac6] some commit message
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt
Enter fullscreen mode Exit fullscreen mode

git commit command is used to add a commit. Option -m is used to pass a commit message to the commit which describes what changes are being committed. Replace some commit message with a custom message which describes the changes being committed.

Note: A commit message is necessary to add a commit. If -m option is not passed along with git commit, an editor(usually vim or nano) is opened where you have to write a commit message.

Again run git status followed by git diff.

$ git status
On branch master
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:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ git diff
diff --git a/a.txt b/a.txt
index 9daeafb..8e042fb 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1,2 @@
 test
+git
Enter fullscreen mode Exit fullscreen mode

git status no longer shows no commits yet message. Also since the staged changes were committed, only unstaged changes are displayed in git status.
git diff displays the changes in each file with respect to staged changes if they exist else with respect to committed changes.

To see history of commits, you can run git log.

$ git log
commit 4d75ac6ce5c533b2ec7bac5e0e00e112b2d5d417 (HEAD -> master)
Author: dagasatvik10 <dagasatvik10@gmail.com>
Date:   Wed Jul 24 02:13:23 2019 +0530

    some commit message
Enter fullscreen mode Exit fullscreen mode

By default, with no arguments, git log lists the commits made in the project in reverse chronological order; that is, the most recent commits show up first.

Wrap up

Congrats on working your way through this tutorial! In it, we covered basic git commands like git init, git status, git add, git diff, git commit and git log.
Keep exploring more about git and if you have any issues, don't hesitate to ask.

This was my very first blog, so please provide me with feedback on what can I do to improve.

Top comments (0)