Sunday 20 December 2015

Git - A Quick Introduction

Git is turning out to be the favourite Version Control System being used in software giants or in open source world. This post will collate all the gymnastics can be done with Git by a beginner.

First of all you need a bitbucket account. Then create a repository. You also need to download git software. I have created git-experiment repo and made it public. (For all How To kind of question you need Google.)

To create repository locally you can do $git init inside the folder. But to clone a central repo, you need to get repo URL by using clone option. Then execute the following command from your git bash.

Refer this link and setup ssh so that you need not enter password while interacting with server.
https://confluence.atlassian.com/bitbucket/set-up-ssh-for-git-728138079.html

$ git clone https://sibtain@bitbucket.org/sibtain/git-experiment.git git-experiment-repo
Cloning into 'git-experiment-repo'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

We have cloned an empty repository. Set the required config values using following commands.
$ git config user.email "your_name@abc.com"
$ git config user.name "Your_Name"

Let's add README.md, create a branch and push it to central repository.
$ cd git-experiment-repo/
$ vi README.md

Add content in the README.md file and Save it. Now execute
$ git status
On branch master

Initial commit

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

        README.md

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

Now add the file.
$ git add README.md

View status of git repository.
$ git status
On branch master

Initial commit

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

        new file:   README.md

Commit Changes. (this will throw error if you haven't configure user.email and user.name)

$ git commit

This will open a file in which I have added first 3 lines specifying a commit message.
Initial Checkin

Adds README.md file with contributors list.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
#       new file:   README.md

Save the file with ESC+wq & you will see following message.
[master (root-commit) 3a86518] Initial Checkin
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.
 1 file changed, 2 insertions(+)
 create mode 100644 README.md

To view the commit history in the local repository, you can execute following command.
$ git log
commit 3a86518414ad9fd99164ef5ad1abbba8190f9dda
Author: Your_Name <your_name@abc.com>
Date:   Sun Dec 20 15:27:31 2015 +0530

    Initial Checkin

    Adds README.md file with contributors list.

It has generated a 40 digit SHA-1 value which is unique for each commit. To view a summary list of commits,
$ git log --oneline
3a86518 Initial Checkin

Now it the time to push our commits to central repository.
$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 279 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://sibtain@bitbucket.org/sibtain/git-experiment.git
 * [new branch]      master -> master

Done. With this we had a run through a life cycle of git commands. But there is lot to explore. Keep watching this space !

No comments:

Post a Comment

Your comments are very much valuable for us. Thanks for giving your precious time.

Do you like this article?