Deep Dive Into Git(Part II)

Debajit Mallick
6 min readOct 16, 2020

--

In my previous blog, I discussed “what is Git?”. In this blog, I am going to share how you can get started with git commands. Before deep dive into git, firstly, we need to understand what is GitHub. So GitHub is a code hosting platform for collaboration and version control. There are many platforms same as GitHub like GitLab, Bitbucket etc.

Before getting started, we need to install git. To install git just go to the link and download git for your system. If you are using windows then you have to install git bash. If you are using Linux or Mac then the default terminal is fine for you. After that, you need to go to GitHub and create an account. In this article, we are mainly focusing on git commands. Now we can start our journey with basic git commands.

First, we need to understand some of the words that are often used in git.

  1. repository: A git repository is a .git folder inside a folder.
  2. sha/hash: It is a hexadecimal number which is used to identify different commits.
  3. directory: It is same as a folder. But when we navigate through terminal we use the term directory.
  4. master/ main: It is the name of the default branch in git.

Now, just open git bash if you are in Windows and terminal if you are in Mac or Linux. Every git command started with git keyword. So to check that git is installed properly in your system just run the following command:

git — version

check the version of git

It will show you the version of git you installed in your system. After that, you have to configure your name and mail at git bash. To do that run the command:

git config — global user.name “your name”

git config — global user.email “your email”

set up a username and email

Now you are ready to go. First, you have to create a directory. To create a directory using terminal use the following command:

mkdir folder-name

To enter inside a directory write the following command:

cd folder-name

create and enter inside the directory

After entering into the directory, now you have to initialize git to access all the features of it. use the following command:

git init

initialize empty git repo

It initializes an empty git repository. So now if you turn on hidden files and folder, you can find a .git folder inside that folder. Or you can use

ls -a

command and you can find one .git folder inside it. It is the place where git controls all of its features.

Now you have to insert some contents inside the directory. So just open that directory in your favourite editor(Ex: VScode, Atom, Sublime etc.) and write some codes inside it.

write some code inside the directory

After saving all the files. Now you need to stage all the changes inside the files. Because now the files are inside the directory but version control does not work now. Only the last saved version was shown now. To stage all the changes use:

git add file-name (to stage changes only inside that particular file) or,

git add . (to stage all changes inside the current directory)

staged all changes in the project

Now after staging all the files or changes inside the project you need to commit those changes. If you don’t commit those changes then only the last version is saved. To commit all the changes that you made use the following command:

git commit -m “commit message”

commit all changes

You can also add and commit both at the same time by using the command:

git commit -a -m “commit message”

After committing all the changes a sha or hash is generated for that particular commit. Now if you made some more changes then you can compare those changes with any committed versions by using the command:

git diff sha-of-one-commit sha-of-another-commit -p

compare two different commits

One of the most helpful git command is status. It gives you all the information about the current situation of your repository.

git status

check the current status of the repo

What is HEAD?

HEAD is a pointer which always points to the last committed version. So you can also use the HEAD in your commands. Ex:

git diff HEAD HEAD~1 -p

compare the last and the one before the last commit.

To list all the commits details use the following command:

git log

list all the commits details

Another useful command is show command. It is like a merge of git diff and git log. By using show you get all the details of a particular commit like author, date and so much more.

git show sha-of-the-commit

To see the commits in a particular file use:

git annotate file-name

show details of each line in a file

If you are using VScode then you can also you an extension named as git lens to get the same output in VScode.

Now if make some changes but not add them to go back to the last committed version use:

git checkout — filename

delete all the changes

If you add those changes and now want to go back, just use reset command:

git reset HEAD filename

unstage all changes

Now if you run the checkout command it takes you to the last committed version.

If you are in a stage when you commit those changes and find that you are doing something wrong then just go to a previous version write

git checkout sha-of-the-commit-you-want-to-go file-name

go to that particular version from the latest version

These are the basic commands of git. After learning about all these commands you are ready to go with GitHub and other complex commands of git. Just check out any cheatsheet of git for learning more.

--

--

Debajit Mallick

Frontend @P360 | Organizer @GDG Siliguri🚀 | Ex β-MLSA | Ex Hack Club Lead | Smart India Hackathon 2020 Winner & 2022 Mentor of Winner Team