Git and Git Commands

Ankit
6 min readMar 27, 2024

--

Git is a free, open-source distributed version control system. It keeps track of projects and files as they change over time with the help of different contributors.

Version control is a system that records changes to a file, or set of files, over time so that you can recall specific versions later.

Git helps keep track of changes made to a code. If at any point during coding you hit a fatal error and don’t know what’s causing it, Git allows you to revert back to a stable state. It also helps you see what changes have been made to the code over time.

Repository

A repository (commonly referred to as repo) is a collection of source code. A repository has commits to the project or a set of references to the commits (i.e., heads).

Commits

A commit logs a change or series of changes that you have made to a file in the repository. A commit has a unique SHA1 hash which is used to keep track of files changed in the past. A series of commits comprises the Git history.

Working directory, staging area, and local repo

Every local repository has three different virtual zones. These are:

  • Working directory
  • Staging area
  • Commit area

The working directory is where new files are created, old files are deleted, or where changes are made to already existing files.

Once changes are made, they are added to the staging area. The staging area is also sometimes called the index.

Once the changes are complete​, the staging area will contain one or more files that need to be committed. Creating a commit will cause Git to take the new code from the staging area and make the commit to the main repository​. This commit is then moved to the commit area.

How to install GIT?

Check if GIT is installed or not?

You can check whether Git is installed and what version you are using by opening up a terminal window in Linux or Mac, or a command prompt window in Windows, and typing the following command:

git --version

If git is not installed in our system we will get following command:

-bash: git: command not found

'git' is not recognized as an internal or external command, operable program or batch file.

Installing Git on linux

The easiest way to install the linux on the linux is through the terminal.

Installing Git on Ubuntu 18.04 or Debian 10 You can use the apt package management tools to update your local package index. Afterwards, you can download and install the program:

sudo apt update
sudo apt install git

Setting up Git

Now that you have Git installed, you need to do a few things so that the commit messages that will be generated for you will contain your correct information.

The easiest way of doing this is through the git config command. Specifically, we need to provide our name and email address because Git embeds this information into each commit we do. We can go ahead and add this information by typing:

git config --global user.name "Ourr Name"
git config --global user.email "Ouremail@domain.com"

we can check all our configuration by using the following commands:

git config --list

we will get following this

user.name=Our Name
user.email=Ouremail@domain.com

we can also edit this by using the command

nano ~/.gitconfig

After this nano editor will be open with:

[user]
name = Our Name
email = Ouremail@domain.com

Once you’re done editing your file, you can exit nano by typing the Ctrl and x keys, and when prompted to save the file press y.

After all the configuration, we are ready to create a repository and use git inside them and can put our code on github or gitlab whereever we want. So now, we will clear/open the terminal and create/open a directory where we want to work on git

mkdir directory_name
cd directory_name

To intialise the Git in our directory we will use following commands:

git init

After this .git will get created in our directory which keep track of all our files and it’s also hidden. To check whether .git file is created in our directory or not we will use the command ls -a it will list all the files and folder present in our directory included .git files.

If we have two files in our repo like hello.js and readme.md .

To Regulate hello.js with git we will use

  1. git add hello.js :- Git will start tracking hello.js
  2. git stauts : we can check the status, i.e which files are in working area and which files are in staging area. After pressing enter we will get following details:
On branch master

No commits yet

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

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

From this we get to know that one file i.e hello.js in staging area and readme.md in working area, i.e untracked by Git . We can now send back hello.js to staging area by using the following commands:

git rm --cached hello.js
##we will get "rm hello.js" which means it's removed from status area.

Now if we check the status of file by using command git status , we will get following result

On branch master

No commits yet

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

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

3. git add . By using this command we can send all the files from working areas to staging area.We can check this by using git status and will get output:

On branch master

No commits yet

Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.js
new file: readme.md

4. git commit -m "commit message" — We will use this command to send our file from staging area to commit area. After getting commited we will get following output in the terminal.

[master (root-commit) d0a65df] message
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 hello.js
create mode 100644 readme.md

5. git log — This will give us information about all the commit history.

6. git diff commit_id1 commit_id2 — It shows all the difference between commit_id1 and commit_id2.

Now to push our code from local storage to Github or Gitlab we have to create one repository there. We can keep this repository public and in option to chose the README.md file we don’t keep it our repository and will create our own READEME.md when we need it.

After creating repository on Github we will now connect our local storage to Github through terminal by using following command:

7. git remote add origin github_repository_link — here origin is the name of our remote connection name.

Now we can Finally push our code to Github by using following commands:

8. git push origin master — here master is our branch name. We can use push as much branch as we want.

I know after you have one doubt that what’s branch here, so let me tell you definition first

Branches serve as an isolation mechanism, allowing developers to work on features, fixes, or experiments in parallel with the main codebase, typically without affecting the main or “master” branch until they’re ready to merge their changes.

From definition itself we can understand that master is default branch and whenever we want to add any new feature or experiments we use new branch so that it will not affect our the funtionalites of main branch.

We can create branch by two ways:

  1. In this method we will first create a branch with the feature_name so that we can understand it better and after that we have to using another command to go to that branch. The commands are
git branch feature_name
##after this
git checkout feature_name

2.Another method is we create and checkout to new branch in a single command line

git checkout -b feature_name
###git checkout is a command used to switch between branches in a Git repository.
##The -b option tells Git to create a new branch with the name provided right after this option.
##feature_ame is the name of the new branch we want to create and switch to.

We will code the feature property and after successful testing if it satisfy our needs we can merge it in our master branch.

git checkout main
##Since we have finished working on a new feature in a branch named feature1 and you want to merge these changes back into the main branch. First, we would switch to the main branch.
##after that we will use
git merge feature_name

--

--