Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Wednesday, 5 July 2017

Git-2

                                                                              
                                                                                GitHub

GitHub is a web-based Git or version control repository and Internet hosting service. It is mostly used for code. It offers all of the distributed version control and source code management functionality of Git as well as adding its own features.

There are many ways it can be set up and configured, but at my job, here's how we use it: when a new employee starts, he downloads all the files from Github, which is an online server we're all connected to.

So he has his local version of the files, I have my local version, our boss has his local version, etc.

Before working with github i need to create an account with github then have to connect my local git repository with github using a pull request from my local machine to the github by using ssh connection.

When I make a change to some files, I go through the following process in the Terminal. (There are GUI clients for Git, but I prefer working on the command line.)


 > git pull

That pulls the latest changes down from github. If there are conflicts between those and my local ones, it tells me what they are, file-by-file, line-by-line, and I now have a chance to reconcile those differences.

After editing the files or creating new ones, I run this command:

 > git add .

Which adds all of my local changes to git, so that git knows about them. The dot after add specifically means to add all the changes I've made, e.g. new files I've added to my local folder or changes I've made to existing files. If I want, I can add only specific files, e.g.

 > git add myNewFile.js

I now write a comment about the adds I just made.

 > git commit -m "Fixed a major bug which stopped reports from printing."

Finally, I upload my changes to the server.

 > git push

Now, when my colleagues do a ...

 > git pull

... they will get my changes, and they will be notified if any of them conflict with their local versions.

There are all kinds of cool, useful commands for rolling back changes to a particular time or state. But probably the most useful thing about Git is branching. Let's say my team is working on code for an Asteroids game, and I get the idea for making spinning asteroids. This will involve making some major changes to the existing asteroids code, and I'm a little scared to do that. No worries, I can just make a branch.

First of all, I'll check which branches exist:

 > git branch
master*


So there's currently only one branch on my local machine, called master. The star by it means that's the branch I'm currently working in. I'll go ahead and create a new one:

 > git branch spinningAsteroids

That creates a copy of all the files in master. I'll now move into that branch.

 > git checkout spinningAsteroids
> git branch
master
spinningAsteroids*


I now spend a couple of hours in spinningAsteroids, doing whatever coding I need to do, not worrying about messing things up, because I'm in a branch. Meanwhile, I get a tech support call. They've found a critical bug and I need to fix it asap. No worries...

 > git checkout master

... fix bug ...

 > git pull
> git add .
> git commit -m "Fixed critical bug with high scores."
> git push


Now I can resume my work with spinningAsteroids.

 > git checkout spinningAsteroids
> git branch
master
spinningAsteroids*


... work, work, work ...

Okay, I'm now happy with my spinning asteroids, and I want to merge that new code into the main code base, so...

 > git checkout master
> git branch
master*
spinningAsteroids


 > git merge spinningAsteroids

Now the code from my branch is merged into the main code-base. I can now upload it.

 > git pull
> git add .
> git commit -m "added new cool feature! Spinning asteroids!!!"
> git push

Monday, 3 July 2017

Git

                                       Git Introduction

It is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency

Having a distributed architecture, Git is an example of a DVCS (hence Distributed Version Control System). Rather than have only one single place for the full version history of the software as is common in once-popular version control systems like CVS or Subversion (also known as SVN), in Git, every developer's working copy of the code is also a repository that can contain the full history of all changes.

Git allows a team of people to work together, all using the same files. And it helps the team cope with the confusion that tends to happen when multiple people are editing the same files.

Using git we can travel to different versions of the files in easy way.
--------------------------------------------------------------------------------------------------------------------------
Creating a directory and making it as git repository using git init command.

[test@server1 ~]$ mkdir g1

[test@server1 ~]$ cd g1

[test@server1 g1]$ git init                      ----> it makes a directory as git repository .

Initialized empty Git repository in /home/test/g1/.git/

------------------------------------------------------------------------------------------

After that first time users should give  Their name and email id to git using  the following way.


[test@server1 g1]$ git config --global user.name "test"             

[test@server1 g1]$ git config --global user.name "test@localhost"


[test@server1 g1]$ ls

[test@server1 g1]$ ls -la

total 12
drwxrwxr-x  3 test test 4096 Mar 28 16:28 .
drwx------ 12 test test 4096 Mar 28 16:28 ..
drwxrwxr-x  7 test test 4096 Mar 28 16:28 .git

------------------------------------------------------------------------------------------

Here we will check how git works by doing a simple example using as below

create a index.html file then add it to  to git repository  to track the file.

[test@server1 g1]$ vi index.html                         ---> creating a file inside git repositry 

[test@server1 g1]$ git add index.html                 ---> add it to repository track it

[test@server1 g1]$ git status             --> This command shows the status of git repository as number of files are in tracking   

# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   index.html
#

[test@server1 g1]$ git commit              ---> This command save the changes by give a message and                                                                            commit.
[master (root-commit) c097834] updating
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 index.html

--->It will show the log message as how many log files are there for a file which was commited.

[test@server1 g1]$ git log

commit c097834da5f9f54aa4f08cb253aae41f5557e77b
Author: test <test@localhost>
Date:   Tue Mar 28 16:29:36 2017 +0530

    updating
---------------------------------------------------------------------------
Now change the content of the index file and add to the repository and commit it.by below 

[test@server1 g1]$ vi index.html                           --Change the content 

[test@server1 g1]$ git add index.html


[test@server1 g1]$ git commit -m "update second time"

[master b0e8a8f] update second time
 1 files changed, 2 insertions(+), 0 deletions(-)

-------------------------------------------------------------------------

check the git log to get the previous version file.It will shows the commit id which is help full to go the previous version of the file.

[test@server1 g1]$ git log

commit b0e8a8ffb442dcb86b4cc4481e29e3ab42e256f2
Author: test <test@localhost>
Date:   Tue Mar 28 16:31:29 2017 +0530

    update second time

commit c097834da5f9f54aa4f08cb253aae41f5557e77b

Author: test <test@localhost>
Date:   Tue Mar 28 16:29:36 2017 +0530

    updating

---------------------------------------------------
 get the required modification file using commit id from git log using git checkout command as following

[test@server1 g1]$ more index.html

This is the git file

This is a modification doing second time

-----------------------------------------------------------------------------------------------------------------
Here you can observe that content of index file.

[test@server1 g1]$ git checkout c097834da5f9f54aa4f08cb253aae41f5557e77b -- index.html

[test@server1 g1]$ more index.html

This is the git file

---------------------------------------------------------------------------------------------------------------------
By using the commit id we can travel different versions of the git as below

[test@server1 g1]$ git checkout b0e8a8ffb442dcb86b4cc4481e29e3ab42e256f2 -- index.html

[test@server1 g1]$ more index.html

This is the git file

This is a modification doing second time

If you face any problem while practicing feel free to comment it and Bookmark this blog for quick reference.We will try to help you

Thanks
Devops Desk Team