Git : A beginners Guide

In this article, I am going to talk about Git. I am writing this article to provide you some basic stuff about Git to start with.

What is Github

  • Its a code sharing and publishing service.
  • Github is a website providing git hosting services.
  • Github provides hosted git repository plus some additional services (wikis, bug tracker, etc)

What is Repository in Github and how to create one

A repository is a directory or storage space where you can access your project, its files, and all the versions of its files that Git saves. A folder will not be treated special unless and until you execute “git init” command for it. After this only, git will accept all git command for given folder and it will be treated as a repository.

What is gist in Github and how to create one

Gist is a simple way to share snippets and pastes with others. All gists are Git repositories, so they are automatically versionedforkable and usable from Git.

To create gist login to Github and go to https://gist.github.com/

 

What is the difference between Gist and Repository

Gist is a simple way to share snippets and pastes with others. All gists are Git repositories, so they are automatically versionedforkable and usable from Git.

What is fork

If you want to contribute to someone else’s project, or would like to use someone’s project as the starting point for your own. This is known as “forking”. In other words, it helps you create your own branch of the project so that you can do whatever you want to do with this new version without worrying about the original project, later if you want to contribute to the original project you can send the PULL request from your forked repo to the original repo.

How fork is different from clone

Clone is git specific command while Fork is Github specific command.  When you are forking any project it doesn’t duplicate everything under your name. But it creates a new view which is specific to you. So if you want to work on any forked project you have to clone this on your local system.

Note*: I am still learning GIT. So there might be some issues in this article. Please help me improve this article.

Git Setup

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

Once done with the installation, check if everything is working properly by verifying git version on the terminal.

git --version

You should get an output like below.

git version 2.14.3 (Apple Git-98)

 

Git Clone

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

Without authentication

git clone /path/to/remote/repository

With Authentication

git clone userId@pwd:path/to/remote/repository

Example

git clone https://github.com/JBTAdmin/Spring-Boot.git

Output

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 a normal situation, you won’t make changes directly to the master branch. So first step would be to create a branch and commit all the changes into that branch and push the changes to the remote repository.

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

* master

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

git branch jbt_branch

Check the status again by executing “git branch”.

  jbt_branch
* master

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

git checkout jbt_branch

Check status: “git branch”

* jbt_branch
  master

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

 

Git Ignore

Now your code is pointing to a new branch and you are ready to commit the changes. But there is unwanted file that you don’t want to commit. And you want git to ignore all these files while committing. You need to create .gitignore file containing a 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 the home folder of user, else create in the current workspace.

I have done some changes in the code I checked out. See all the files that are modified by executing the 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/ApplicationStart.java

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

  spring-boot-sample.iml

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

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 create .gitignore file with this file name.

vi .gitignore

Once created, check the 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/ApplicationStart.java

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

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

 

Git Commit

The next step would be to commit the changes using the 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/ApplicationStart.java

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 the 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/ApplicationStart.java

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 a 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

The reason is branch is only created locally and it is not available in remote repository. Execute below command to push everything to the 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.
To https://github.com/JBTAdmin/Spring-Boot.git
 * [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 the 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 Reply

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.