Git : A beginners Guide

I am back after a long time. :). In this article i am going to talk about Git. I faced some problem when i started using git hence i am writing this article to provide you some basic stuff about Git to start with.

In this article i will cover below aspect of Git

  1. Get code from remote repository(In this case GitHub)
  2. Create a new local branch
  3. Ignore unnecessary files
  4. Add & Commit new/changed files to newly created local branch
  5. Push changes to remote repository
  6. Pull/Merge code from Remote repository


Git Setup

In order to work with Git. First you need to setup Git on your local system(PC/MAC). Please download and install appropriate Git based on your OS. Or follow the guide as mentioned(Linux).

Once done with installation, check if everything works properly by checking git version on terminal.

git --version

You should get an output like below.

git version 2.14.3 (Apple Git-98)


Git Clone

Once git setup is done. Next step would be to get code from remote repository. In this situation from Github. You can execute clone command for this purpose.

Without authentication

git clone /path/to/remote/repository

With Authentication

git clone [email protected]:path/to/remote/repository


git clone


Cloning into 'Spring-Boot'...
remote: Counting objects: 25, done.
remote: Total 25 (delta 0), reused 0 (delta 0), pack-reused 25
Unpacking objects: 100% (25/25), done.


Git Branch

Unless specified explicitly default branch will be master. In normal situation you wont make changes directly to master branch. So first step would be to create a branch and commit all the changes into that branch and push the changes to remote repository.

To check what is the current branch for the code you cloned, execute “git branch”. It will show below output.

* master

To create a new branch use “git branch branch_name”.

git branch jbt_branch

Check status again by executing “git branch”.

* master

Here * means current branch is master. To switch to newly created branch execute

git checkout jbt_branch

Check status : “git branch”

* jbt_branch

Now * is pointing to jbt_branch and not master. So now what ever you commit in the code will go into this new branch and not master.


Git Ignore

Now your code is pointing to new branch and you are ready to commit the changes. But there are unwanted file which you don’t want to commit. And you want git to ignore all these files while committing. You need to create .gitignore file containing list of files that needs to be ignored.

You can create this file globally or locally. When you know that certain type of files needs to be ignored all the time irrespective of repository create this file in home folder of user, else create in current workspace.

I have done some changes in the code i checkedout. See all the files that are modified by executing below code.

git status

In my case output is like this

On branch jbt_branch
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:   src/main/java/groupId/

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


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

I have done changes only in one file(, but git displayed one more file spring-boot-sample.iml(modified coz of IDE), which i don’t want to be checked in. So i will created .gitignore file with this file name.

vi .gitignore

Once created, check status again

On branch jbt_branch
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:   src/main/java/groupId/

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

As you can see git is ignoring other file now. And if you commit file now only one file will be committed.


Git Commit

Next step would be commit the changes using command “git commit“. But if you execute this command git will complain.

On branch jbt_branch
Changes not staged for commit:
  modified:   src/main/java/groupId/

no changes added to commit

So first you need to add the file to stage by running “git add .“. It will add all the file in stage. Now check status.

JBT$ git status
On branch jbt_branch
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

  modified:   src/main/java/groupId/

It is now added stage and color is changed to green. Now commit the code.

JBT$ git commit
[jbt_branch 0d898a0] Commit Testing
 1 file changed


Git Push

Use “git push” to push the changes to remote repository. But if you do that you will get below exception.

fatal: The current branch jbt_branch has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin jbt_branch

Reason is branch is only created locally and it is not available in remote repository. Execute below command to push everything to remote.

JBT$ git push --set-upstream origin jbt_branch
Counting objects: 7, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 548 bytes | 548.00 KiB/s, done.
Total 7 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
 * [new branch]      jbt_branch -> jbt_branch
Branch jbt_branch set up to track remote branch jbt_branch from origin.


Git Pull/Merge

In case you want to get latest code from remote repository use “git pull“.  And if there is any conflict between remote and local repository you can use “git merge” to merge the changes and then commit the changes to remote.




Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.