Wednesday, October 15, 2014

Working with GIT

GIT is now considered as a most versatile distributed version control system. The GIT is not completely different from the sub-version we use. The main benefit of using GIT is that it provides better ways when tracking and handles file changes.

Normally with the SVN, we configure a SVN Repository and then use a client to connect to the repository to download the project. Once we have the project in our space, we make changes and then commit the files to the repository again. This is much like a Client-Server model.

But GIT works in a different way, when we download a Project from a remote GIT location we actually download everything from version history to individual files along with the changes done. Once the download is done the local GIT repository acts as a server. We can do a Check-in, check-out and all other version control activities. When we are ready we can merge our changes to the remote GIT location

With GIT developers don’t need to connect to the master Repository all the time. It is also good at branching and merging. GIT is considered best for the Open Source projects. We just need to make changes and push to our local repository. When connected we can ask the project manager to push the changes from our repository to the master repository. GIT is also good in bridging between it and SVN.

In this article we will see how we can install and Configure GIT in a Linux environment.

1. Download GIT from here
2. Configure the GIT using,
 [root@localhost git-2.1.2]# cd GIT/
 [root@localhost git-2.1.2]# ./configure
 [root@localhost git-2.1.2]# make
 [root@localhost git-2.1.2]# make install

3. Check the installation using
[root@localhost git-2.1.2]# whereis git
git: /usr/bin/git /usr/local/bin/git /usr/share/man/man1/git.1.gz

[root@localhost git-2.1.2]# git --version
git version 2.1.2

Once we see the correct responses, we can confirm that GIT is configured correctly.

4. Our next step is to create a Project. Create a Directory as gitpasswordtest and from inside the location use

[root@localhost gitpasswordtest]# git init
Initialized empty Git repository in /root/dump/gitpasswordtest/.git/

Once the command returns we can see file created in the location as,

[root@localhost gitpasswordtest]# ls -alrt
drwxr-xr-x. 7 root root 4096 Oct 15 17:45 .git

[root@localhost gitpasswordtest]# cd .git/
[root@localhost .git]# ls -alrt
drwxr-xr-x. 4 root root 4096 Oct 15 17:45 refs
-rw-r--r--. 1 root root   73 Oct 15 17:45 description
drwxr-xr-x. 3 root root 4096 Oct 15 17:45 ..
drwxr-xr-x. 2 root root 4096 Oct 15 17:45 info
drwxr-xr-x. 2 root root 4096 Oct 15 17:45 hooks
-rw-r--r--. 1 root root   23 Oct 15 17:45 HEAD
drwxr-xr-x. 2 root root 4096 Oct 15 17:45 branches
drwxr-xr-x. 4 root root 4096 Oct 15 17:45 objects
-rwxr--r--. 1 root root   92 Oct 15 17:45 config
drwxr-xr-x. 7 root root 4096 Oct 15 17:45 .

5. Create a File and commit the file. Our next step is to create a file and commit the file to the GIT repository.

[root@localhost gitpasswordtest]# touch hello
[root@localhost gitpasswordtest]# echo “This is Jagadish” >> hello

Once the file is created , add the file to GIT repository as
[root@localhost gitpasswordtest]# git add hello

We can add files even as
[root@localhost gitpasswordtest]# git add *.java
[root@localhost gitpasswordtest]# git add *.c

In most cases we use below which will add all files under the current location
[root@localhost gitpasswordtest]# git add .

One the file is added , commit the changes as
[root@localhost gitpasswordtest]# git commit -m 'First basic File to Git'
[master (root-commit) 0fe6798] First basic File to Git
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global "Your Name"
    git config --global

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 hello

From the above output we can see that the file commit to GIT is successfully. But we a warning that name and password are not configured.

For configuring these, we can use
[root@localhost gitpasswordtest]# git config --global "GIT Admin"
[root@localhost gitpasswordtest]# git config --global

Verify the git configuration information as,
[root@localhost gitpasswordtest]# git config --list Admin
This information is stored in the .gitconfig file under your home directory.

$ cat ~/.gitconfig
        name = GIT Admin
        email =

6. Make changes to the file and commit
[root@localhost gitpasswordtest]# vi hello ( make some changes )
[root@localhost gitpasswordtest]# git diff
diff --git a/hello b/hello
index 167f877..4ae8d27 100644
--- a/hello
+++ b/hello
@@ -1 +1 @@
-this is jagadish
+this is jagadish after changes

[root@localhost gitpasswordtest]# git add hello
[root@localhost gitpasswordtest]# git commit -m "this is second change"
[master 4f48fe8] this is second change
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
1 file changed, 1 insertion(+), 1 deletion(-)

7. Status check. Once the changes are committed ,we can check the status using
[root@localhost gitpasswordtest]# git status
On branch master nothing to commit, working directory clean

We can also see changes done to the file using
[root@localhost gitpasswordtest]# git log hello
commit 4f48fe8ce43b8aa9fc90903a197e057cb94a75a9
Author: root <root@localhost.localdomain>
Date:   Wed Oct 15 17:52:26 2014 +0530

    this is second change

commit 0fe679861458c3a34da30c7ecc4f4ee1dca39ec4
Author: root <root@localhost.localdomain>
Date:   Wed Oct 15 17:50:45 2014 +0530

    First basic File to Git

By this we cover the basics of using GIT server. I will be providing more doc on how to handle advanced operations on GIT.

No comments :

Post a Comment