Git : A beginners Guide

Git:

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.

You should get an output like below.

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

With Authentication

Example

Output

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.

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

Check status again by executing “git branch”.

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

Check status : “git 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.

In my case output is like this

I have done changes only in one file(ApplicationStart.java), 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.

Once created, check status again

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.

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.

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

Git Push

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

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

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.