Getting started with git

A tool to keep track of software development.

Aditya Kumar
6 min readAug 22, 2021

What is the version control system?.

A version control system which is also known as a source code manager is a tool that manages different versions of our source code. There are different types of version control systems like git, svn, etc. Git is the most popular and widely used VCS.

Installing git on Linux (ubuntu 20.04)

sudo apt-get install git

Setting configuration for our system

$ git config — global user. name “ full name”

$git config — global user.email “email”

$git config — global color.ui auto //Git output will be colorful

$git config — global core.editor “cod — wait” //Editor setting for VS code

Note:- There is space between config and — . Running the below command will show all configuration settings for our system.

Terminologies in git

Repository: A directory that contains our project work as well as our hidden .git repository.

Working directory: Files that we see in our computer file system. This is in contrast to the file that has been saved in the repo.

Staging area: This is a file in the git repo that stores information about what is going to be in our next commit.

Commit: This is a snapshot of our project in git. This takes picture of the file at that moment and stores reference to that snapshot.

SHA: Secure hash algorithm(SHA) is the ID number for each commit. A 40 character long string(0–9 and a-f). It is calculated on the basis of the content of the file.

Branch: This is a new line of development diverging from our main line of development. If we want to add a new feature to our software or fix some bug then branching is used so that our original code is not corrupted.

Merging: When adding a new feature improves our app or our bug is fixed. Then we merge this branch to the master branch.

Creating git repo.

Make a directory by mkdir command and cd to that directory. Then run the following command.

$git init

This will set up all necessary files and directories that git will use to keep track of everything. All the files store in a hidden directory called .git.

Type $ls -a in terminal it will show .git directory.

$ cd .git and $ls -a

All content of .git rep will be shown.

To see the content of the config file type $git config[space] — list

Committing to the repo.

Before committing to the repo we have to tell the git what we are going to commit. This process is called staging.

$ git add <file_name>

If we have staged accidently and want to unstage then run the following command.

$git rm — cached <file_name>

Now to commit to repo.

$git commit

The code editor will be prompted and write your commit message. If the code editor does not shows then configure your editor correctly. If the commit message is short just run the command

$git commit -m “ commit message”

Use $git status regularly to see the current state of a repository.

The git diff command.

If you modify some committed file then if you want to see the difference between modified files and then run the following command.

$git diff

Adding .gitignore file

If we want some private files to be not added to the repo. Then add a .gitignore file.

$touch .gitignore

Open the .gitignore file and add the file name to it to be ignored

Tagging, branching and merging.

Tagging is used to give tags to specific commits so that developers can identify the release version of the software. The tag will be attached to that commit even if more commits are added. In the following command.

The first command creates a tag, the second command shows the tag on the master or footer branch. The third command deletes the tag. The fourth command is used to add a tag to a given commit ID(SHA).

$git tag -a version1.0

$git log — decorate

$git tag -d version1.0

$ git tag -a <SHA>

Branching: If we want to add some feature or fix some bug then we make a branch so that our current development is not disrupted.

$git branch feature-branch

$git checkout feature-branch

In above the first command creates a branch and the second one is used to move to that branch. If we want to simultaneously create and switch to that branch then below command.

$git checkout -b feature-branch

Deleting branch: If we want to forcefully delete a branch before merging use the command

$git branch -D<branch-name>

If we want to delete a branch after merging use the command

$git branch -d <branch-name>

Merging: If we want to merge our branch with the master branch then add run the following command.

$git merge <branch-name>

Merge happens in two ways. First by fast forward merge or by regular merge. Fast forward merge happens when there is no further commit on the master branch. Regular merge happens when there is some development on the master branch. Branch merged with regular merge will have two lines of development between branching start and endpoint. To avoid merge commit rebasing is done before merging to have linear development. Rebasing is a way to rewrite history. Checkout to footer branch, run rebase command. Again checkout to master and run rebase command.

Merge conflict:

Merge conflict occurs when the same lines in a file are changed in the footer and master branch. So to avoid merge conflict only one developer should work on one file. Even if merge conflict occurs then first abort the merging by command.

$git merge — abort

Then accept any one of the changes in code. Either from a current branch or from the incoming branch. Then commit the file to the repo. Again try to merge.

Undoing changes.

If we have accidently committed any file or there is some mistake in any commit. There are several commands for undoing commits.

Amending the last commit: If there is a typo in the commit message then run the following command and the code editor opens to correct the commit message.

$git commit — amend

If we want to edit in any file. Then edit the file, save the file, stage the file and run the above command.

Reverting commit: If we want to revert any particular commit then provide SHA of that commit then commit will be reverted and a new commit will occur.

$git commit <SHA>

Using git reset command: git reset command should be used carefully. This command is used with different flags. If we use — mixed flat commit goes to the working directory. With — soft flag committed files go to the staging area and if we use — hard flag then committed file is moved to the trash of computer.

$ git reset — mixed HEAD~1

$git reset — soft HEAD~1

$git reset — hard HEAD~1

Reviewing the repo history

If we want to see the history of our commit following commands are used.

$git log

$git show

$git show <SHA>

$git log — oneline

$git log -p

The git log command shows all commits. The git show shows the last commit. The git log -p shows in a descriptive way, what was committed what changes were made.

--

--

Aditya Kumar
Aditya Kumar

Written by Aditya Kumar

Hi, I am Aditya. I studied at IIT BHU Varanasi , India. I am a Java developer. I partricipate in competitive programming.

No responses yet