Git: commands for basic usage that rescue your project

An introduction to Git. As simple as that.

Perhaps you have already encountered this word before. Perhaps you have been asked about Git during your job interview. Perhaps you still have no idea what it is about. If you still have no clue the article is for you.

What is git?

Git is a distributed version control system (VCS). Git is open source. It is used to store content. In most cases, it is used to store code. So, a VCS ensures a seamless and comfort code storage while multiple developers work in parallel. It also allows such features as branching and merging. In addition, git generally has one repository which is stored in a server and a local one that is stored in the computer of each developer. That ensures the code is not only kept on a central server, but the full copy of the code is available on all computers of each developer.

The difference between Git and other VCS lies in the way it saves the changes. Instead of depending on a centralized server, git enables people to work with the repository at the local level, not even depending on a VPN or Internet connectivity. It also protects from the loss of the source data, since everyone has a full copy on their local drives.

A long story of Git told short

Quite recently, in 2020, there was a small ironic quarrel between Quintagroup developers on how to pronounce the word ‘git’ (English pronunciation. The struggle is real.). Hopefully, they agreed on the right variant - [gɪt]. In 2005, there also was a quarrel about one VCS. But this time it was between the Linux kernel’s developers and the developers of BitKeeker, a commercial company that developed a software tool for distributed revision control of computer source code. For this time, they haven’t agreed. And it led to the development of a new version control system by Linus Torvald, the principal developer of Linux kernel.

Meaning “an offensive word for an unpleasant and annoying person” in British English, the word for git wasn’t chosen by chance as well. As Linus Torvald said:

“I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'.”

To make it even more sarcastic, Linus described the first version of the tool as "the stupid content tracker" and it remains the same up to today.

Git commands

If you feel interested in this tool then you definitely must know the most frequently used commands for git.

git init

How to use: git init [repository name]

$ git init demo
Initialized empty Git repository in /home/jsmith/demo/.git/

This command initializes a git repository for a new or existing project.

git add

How to use: git add [file]

$ ls
README.md features
$ git add features

This command adds file contents to the working directory.

Another variant: git add . - adds all files to the staging area.

$ git add .

git branch

How to use: git branch

$ git branch
* feature_a
  feature_b
  master

This command lists out all the branches in the current repository.

Other variants:

git branch [branch name] - creates a new branch

$ git branch feature_c
$ git branch
  feature_a
  feature_b
  feature_c
* master 

git branch -d [branch name] - deletes selected branch

$ git branch
  feature_a
  feature_b
* master
$ git branch feature_c
$ git branch
  feature_a
  feature_b
  feature_c
* master
$ git branch -d feature_c
Deleted branch feature_c (was d4d6891).
$ git branch
  feature_a
  feature_b
* master

git checkout

How to use: git checkout [branch name]

$ git checkout feature_b
Switched to branch 'feature_b'
$ git branch
  feature_a
* feature_b
  master

This command switches among different branches.

Another variant: git checkout -b [branch name] - creates a new branch and switches to it.

$ git checkout -b feature_c
Switched to a new branch 'feature_c'

git clone

How to use: git clone [url]

$ git clone http://localhost/jsmith/demo.git
Cloning into 'demo'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.

This command clones a repository into a new directory.

git commit

How to use: git commit -m “[Type in a sweet and understandable commit message]”

$ git commit -m 'Add features'
[master da418f4] Add features
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 features
 

This command records changes and sets it to a new commit object.

git diff

How to use: git diff

$ git diff
diff --git a/features b/features
index e69de29..69384dd 100644
--- a/features
+++ b/features
@@ -0,0 +1,3 @@
+feature a
+feature b
+feature c

This command shows you the changes that are not staged.

Additional variant: git diff [first branch] [second branch] - to show the difference between two branches.

$ git diff master feature_a
diff --git a/features b/features
index e69de29..7d5045c 100644
--- a/features
+++ b/features
@@ -0,0 +1 @@
+feature a 

git log

How to use: git log

$ git log
commit da418f4a4105211482229be5eabf7705cbc2ca9c (HEAD -> master)
Author: jsmith <jsmith@example.com>
Date: Fri Jan 24 12:14:37 2020 +0000

    Add features

commit 0b108fb9af1dc5302ec453b0de1465892a1745f8 (origin/master, origin/HEAD)
Author: jsmith <jsmith@example.com>
Date: Fri Jan 24 12:09:26 2020 +0000

    Initial commit 

This command lists the history of a certain branch.

git merge

How to use: git merge [branch name]

$ git merge feature_a
Updating da418f4..f0ee63d
Fast-forward
 features | 1 +
 1 file changed, 1 insertion(+)

This command is used to merges two branches - the one you want with the one you are working with.

git pull

How to use: git pull [repository link]

$ git pull http://localhost/jsmith/demo.git
From http://localhost/jsmith/demo
 * branch            HEAD       -> FETCH_HEAD
Already up to date.

This command downloads and merges new commits that someone else has pushed to a remote repository.

git remote

How to use: git remote add [variable name] [server link]

$ git remote add origin http://localhost/jsmith/demo.git
fatal: remote origin already exists.

This command checks what source you have or add a new remote server.

git reset

How to use: git reset [file]

$ git reset da418f4a4105211482229be5eabf7705cbc2ca9c
Unstaged changes after reset:
M features

This command is used to unstage the file, but at the same time, it preserves the file contents.

git stash

How to use: git stash save

$ git stash save
Saved working directory and index state WIP on master: f0ee63d Add feature a

This command is used to temporarily store all the modified and tracked files.

Another variant: git stash pop - restore the most recently stashed files.

$ git stash pop
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD ..." to unstage)

    new file:   extra_features

Dropped refs/stash@{0} (8b7919df5440179673a7bca8fefcc659703d8b6c)

git status

How to use: git status

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
     (use "git push" to publish your local commits)

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: features

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

This command shows the current status of a working tree.

git tag

How to use: git tag

$ git tag feature_a f0ee63d56ad227a00d77637b9c8cd58b676c1322
$ git tag
feature_a

This command creates, lists, deletes or verifies tags to certain commits.

Everyone fails sometimes

Even the most experienced and professional specialists may screw something up not talking about the young and newbie, especially after hours of diligent code writing. Here are some examples that happened to our developers during their work with git.

Try not to be messy

Keeping order is important. There was a case when one of our programmers created so many branches that he was unable to find out the necessary one. As he says, there two ways of solving this issue. Try hard to recall what each branch is about, or to start from the very beginning. Good luck with the last one!

Confidential information sent to the public

Security is a must at Quintagroup. But git happens! Once, one of our Python developers accidentally uploaded the AWS API key for the rating site, Alexa, into the open GitHub repository. Fortunately, the quick response of our CTO and the AWS staff helped to avoid the great tragedy. Fortunately.

The final dot

Everything may seem difficult and challenging at the beginning. But everything is possible if you take small steps. Stay with Quintagroup to receive the weekly dose of knowledge and contact us to receive a personal answer from our experts.

Connect with our experts Let's talk