Back to the main page

git - version control system

Install GIT

[root@ca-zdudic2 ~]# yum search "Fast Version"
Loaded plugins: langpacks, ulninfo
============ N/S matched: Fast Version =============
git.x86_64 : Fast Version Control System

# yum install git

Installed:
  git.x86_64 0:1.8.3.1-6.el7_2.1
Dependency Installed:
  libgnome-keyring.x86_64 0:3.8.0-3.el7      
  perl-Error.noarch 1:0.17020-2.el7      
  perl-Git.noarch 0:1.8.3.1-6.el7_2.1
  perl-TermReadKey.x86_64 0:2.30-20.el7
For learning start reading: # man gittutorial

Importing a project

Say you have scripts in /nagios-scripts you wanna put under GIT control.
# cd /nagios-scripts

[root@ca-zdudic2 nagios-scripts]#  git init 
Initialized empty Git repository in /nagios-scripts/.git/

So this is the WORKING DIRECTORY, /nagios-scripts/.git
[root@ca-zdudic2 nagios-scripts]# ls -la /nagios-scripts/.git
total 40
drwxr-xr-x 7 root root 4096 Jan 20 12:09 .
drwxr-xr-x 3 root root 4096 Jan 20 12:09 ..
drwxr-xr-x 2 root root 4096 Jan 20 12:09 branches
-rw-r--r-- 1 root root   92 Jan 20 12:09 config
-rw-r--r-- 1 root root   73 Jan 20 12:09 description
-rw-r--r-- 1 root root   23 Jan 20 12:09 HEAD
drwxr-xr-x 2 root root 4096 Jan 20 12:09 hooks
drwxr-xr-x 2 root root 4096 Jan 20 12:09 info
drwxr-xr-x 4 root root 4096 Jan 20 12:09 objects
drwxr-xr-x 4 root root 4096 Jan 20 12:09 refs

Let's take a snapshot of the contents of all files under /nagios-scripts
[root@ca-zdudic2 nagios-scripts]#  git add . 

This snapshot is stored in a temporary staging area called "index".
[root@ca-zdudic2 nagios-scripts]# file .git/index
.git/index: Git index, version 2, 20 entries

[root@ca-zdudic2 nagios-scripts]#  strings .git/index 
DIRC
add_build_systems.sh
add_console_avocent.sh
add_fcswitch-brocade.sh
add_fcswitch-cisco.sh
add_fcswitch-hp.sh
add_fcswitch-qlogic.sh
add_ibswitch-cisco.sh
add_ibswitch-oracle.sh
add_ovm_qe.sh
add_pdu_baytech.sh
RJm%p1
add_pdu_eaton.sh
add_prod_systems.sh
add_switch-cisco.sh
add_switch-generic.sh
add_switch-juniper.sh
add_switch-netgear.sh
add_switch-oracle.sh
add_xsigo.sh
add_zfs_storage.sh
determine_parent.sh
zG2HrCc
aQWJ

To permanently store the contents of the index in the repository do "git commit". You'll find yourself in VI editor and type comment on the first line and save/exit with :wq
[root@ca-zdudic2 nagios-scripts]#  git commit 

This is the first initial commit :)
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Committer: root 
# On branch master
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#       new file:   add_build_systems.sh
#       new file:   add_console_avocent.sh
#       new file:   add_fcswitch-brocade.sh
#       new file:   add_fcswitch-cisco.sh
#       new file:   add_fcswitch-hp.sh
#       new file:   add_fcswitch-qlogic.sh
#       new file:   add_ibswitch-cisco.sh
#       new file:   add_ibswitch-oracle.sh
#       new file:   add_ovm_qe.sh
#       new file:   add_pdu_baytech.sh
#       new file:   add_pdu_eaton.sh
#       new file:   add_prod_systems.sh
#       new file:   add_switch-cisco.sh
#       new file:   add_switch-generic.sh
#       new file:   add_switch-juniper.sh
#       new file:   add_switch-netgear.sh
#       new file:   add_switch-oracle.sh
#       new file:   add_xsigo.sh
#       new file:   add_zfs_storage.sh
#       new file:   determine_parent.sh
#

[root@ca-zdudic2 nagios-scripts]#  git status 
# On branch master
nothing to commit, working directory clean

Making changes

Modify a file.
[root@ca-zdudic2 nagios-scripts]# vi add_zfs_storage.sh

See difference that has been made but not yet added to the index:
[root@ca-zdudic2 nagios-scripts]#  git diff 
diff --git a/add_zfs_storage.sh b/add_zfs_storage.sh
index 87c006f..dba3a55 100644
--- a/add_zfs_storage.sh
+++ b/add_zfs_storage.sh
@@ -1,7 +1,7 @@
 #!/bin/sh
 #set -x

-# adding prod server to nagios  -> old line
+# adding zfs storage to nagios <- new line
 # only adds host definition
 # --- ERROR SUBROUTINE

See the status now:
[root@ca-zdudic2 nagios-scripts]#  git status 
# On branch master
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#       modified:   add_zfs_storage.sh
#
no changes added to commit (use "git add" and/or "git commit -a")

Add its updated contents to the index:
[root@ca-zdudic2 nagios-scripts]#  git add add_zfs_storage.sh 

Status now is :
[root@ca-zdudic2 nagios-scripts]#  git status 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#       modified:   add_zfs_storage.sh
#

This gives no output now.
[root@ca-zdudic2 nagios-scripts]#  git diff 

But this is change that has been added to the index, by using "--cached"
[root@ca-zdudic2 nagios-scripts]#  git diff --cached 
diff --git a/add_zfs_storage.sh b/add_zfs_storage.sh
index 87c006f..dba3a55 100644
--- a/add_zfs_storage.sh
+++ b/add_zfs_storage.sh
@@ -1,7 +1,7 @@
 #!/bin/sh
 #set -x

-# adding prod server to nagios
+# adding zfs storage to nagios
 # only adds host definition
 # --- ERROR SUBROUTINE

The index is temporary staging area, so we need to commit change in order to permanently store the index ontents into repository.
[root@ca-zdudic2 nagios-scripts]#  git commit 
[master dac65cf] Some small chage for fun .........
 Committer: root 
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 user.name "Your Name"
    git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
    git commit --amend --reset-author
 1 file changed, 1 insertion(+), 1 deletion(-)

Another way to modify, see diff, add and commit (in one step) is:
[root@ca-zdudic2 nagios-scripts]# vi add_ovm_qe.sh

[root@ca-zdudic2 nagios-scripts]#  git diff 
diff --git a/add_ovm_qe.sh b/add_ovm_qe.sh
index a83dc39..2c1224c 100755
--- a/add_ovm_qe.sh
+++ b/add_ovm_qe.sh
@@ -1,7 +1,7 @@
 #!/bin/sh
 #set -x
-# adding prod server to nagios
+# adding OVM QE to NAGIOS
 # only adds host definition
 # --- ERROR SUBROUTINE

[root@ca-zdudic2 nagios-scripts]#  git commit -a 
[master 4476064] change prod server -> OVM QE
 Committer: root 
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 user.name "Your Name"
    git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
    git commit --amend --reset-author
 1 file changed, 1 insertion(+), 1 deletion(-)

Project history and logs

To view the history of changes:
[root@ca-zdudic2 nagios-scripts]#  git log 
commit 44760646524886dae32dd58b6c1bd54a64fc1374
Author: root 
Date:   Fri Jan 20 13:01:49 2017 -0800

    change prod server -> OVM QE

commit dac65cff124aaf2dc50671d5cf03e0746eeaf200
Author: root 
Date:   Fri Jan 20 12:58:32 2017 -0800

    Some small chage for fun .........

commit a1d666a5fb8dffe0eb26e57f39a9eca2b3731058
Author: root 
Date:   Fri Jan 20 12:19:21 2017 -0800

    This is the first initial commit :)

For example, see the "commit 44760646524886dae32dd58b6c1bd54a64fc1374". This is the name of commit, and to see its details:
[root@ca-zdudic2 nagios-scripts]#  git show 44760646524886dae32dd58b6c1bd54a64fc1374 
commit 44760646524886dae32dd58b6c1bd54a64fc1374
Author: root 
Date:   Fri Jan 20 13:01:49 2017 -0800

    change prod server -> OVM QE

diff --git a/add_ovm_qe.sh b/add_ovm_qe.sh
index a83dc39..2c1224c 100755
--- a/add_ovm_qe.sh
+++ b/add_ovm_qe.sh
@@ -1,7 +1,7 @@
 #!/bin/sh
 #set -x

-# adding prod server to nagios
+# adding OVM QE to NAGIOS
 # only adds host definition

 # --- ERROR SUBROUTINE

To see overview of the change and to see each step:
[root@ca-zdudic2 nagios-scripts]#  git log --stat --summary 
commit 44760646524886dae32dd58b6c1bd54a64fc1374
Author: root 
Date:   Fri Jan 20 13:01:49 2017 -0800

    change prod server -> OVM QE

 add_ovm_qe.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

commit dac65cff124aaf2dc50671d5cf03e0746eeaf200
Author: root 
Date:   Fri Jan 20 12:58:32 2017 -0800

    Some small chage for fun .........

 add_zfs_storage.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

commit a1d666a5fb8dffe0eb26e57f39a9eca2b3731058
Author: root 
Date:   Fri Jan 20 12:19:21 2017 -0800

    This is the first initial commit :)

 add_build_systems.sh    |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_console_avocent.sh  |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_fcswitch-brocade.sh |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_fcswitch-cisco.sh   |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_fcswitch-hp.sh      |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_fcswitch-qlogic.sh  |  93 +++++++++++++++++++++++++++++++++++++++++++
 add_ibswitch-cisco.sh   |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_ibswitch-oracle.sh  |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_ovm_qe.sh           |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_pdu_baytech.sh      |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_pdu_eaton.sh        |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_prod_systems.sh     |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_switch-cisco.sh     |  95 ++++++++++++++++++++++++++++++++++++++++++++
 add_switch-generic.sh   |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_switch-juniper.sh   |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_switch-netgear.sh   |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_switch-oracle.sh    |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_xsigo.sh            |  94 +++++++++++++++++++++++++++++++++++++++++++
 add_zfs_storage.sh      |  94 +++++++++++++++++++++++++++++++++++++++++++
 determine_parent.sh     | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 20 files changed, 1986 insertions(+)
 create mode 100755 add_build_systems.sh
 create mode 100755 add_console_avocent.sh
 create mode 100755 add_fcswitch-brocade.sh
 create mode 100644 add_fcswitch-cisco.sh
 create mode 100644 add_fcswitch-hp.sh
 create mode 100644 add_fcswitch-qlogic.sh
 create mode 100644 add_ibswitch-cisco.sh
 create mode 100644 add_ibswitch-oracle.sh
 create mode 100755 add_ovm_qe.sh
 create mode 100644 add_pdu_baytech.sh
 create mode 100755 add_pdu_eaton.sh
 create mode 100755 add_prod_systems.sh
 create mode 100755 add_switch-cisco.sh
 create mode 100644 add_switch-generic.sh
 create mode 100644 add_switch-juniper.sh
 create mode 100644 add_switch-netgear.sh
 create mode 100755 add_switch-oracle.sh
 create mode 100644 add_xsigo.sh
 create mode 100644 add_zfs_storage.sh
 create mode 100755 determine_parent.sh

Branches

A single Git repository can maintain multiple branches, the default one, initially created is "master".
[root@ca-zdudic2 nagios-scripts]#  git branch 
* master

To create a new branch named "testing" :
[root@ca-zdudic2 nagios-scripts]#  git branch testing 

[root@ca-zdudic2 nagios-scripts]#  git branch 
* master
  testing

The asteriks shows the branch you are currently on. To switch to 'testing' branch:
[root@ca-zdudic2 nagios-scripts]#  git checkout testing 
Switched to branch 'testing'

[root@ca-zdudic2 nagios-scripts]#  git branch 
  master
* testing

So each branch has its own changes, let's edit a file, commit the change, and view history.
[root@ca-zdudic2 nagios-scripts] vi add_build_systems.sh

[root@ca-zdudic2 nagios-scripts]  git commit -a 

[root@ca-zdudic2 nagios-scripts]#  git log 
commit cfa44f1790d9e5518c9970934ea375894f28eb62
Author: root 
Date:   Sat Jan 21 21:06:16 2017 -0800

    change prod into BUILD

commit 44760646524886dae32dd58b6c1bd54a64fc1374
Author: root 
Date:   Fri Jan 20 13:01:49 2017 -0800

    change prod server -> OVM QE

And change to master branch.
[root@ca-zdudic2 nagios-scripts]#  git checkout master 
Switched to branch 'master'

[root@ca-zdudic2 nagios-scripts]#  git log 
commit 44760646524886dae32dd58b6c1bd54a64fc1374
Author: root 
Date:   Fri Jan 20 13:01:49 2017 -0800

    change prod server -> OVM QE

Right, "change prod into BUILD" is not present in the master branch.

While still in master branch, edit file with change different then in testing branch.
[root@ca-zdudic2 nagios-scripts]# vi add_build_systems.sh

[root@ca-zdudic2 nagios-scripts]#  git commit -a 
[master b28497a] change prod to build-server in master branch
 Committer: root 
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 user.name "Your Name"
    git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
    git commit --amend --reset-author
 1 file changed, 3 insertions(+), 1 deletion(-)

[root@ca-zdudic2 nagios-scripts]# git log
commit b28497a8e236363e8bb4f4ce8f08303dc1384ce9
Author: root 
Date:   Sat Jan 21 21:15:06 2017 -0800

    change prod to build-server in master branch

commit 44760646524886dae32dd58b6c1bd54a64fc1374
Author: root 
Date:   Fri Jan 20 13:01:49 2017 -0800

    change prod server -> OVM QE

Let's merge testing into master branch.
[root@ca-zdudic2 nagios-scripts]#  git merge testing 
Auto-merging add_build_systems.sh
CONFLICT (content): Merge conflict in add_build_systems.sh
Automatic merge failed; fix conflicts and then commit the result.

Yes, there is the conflict and to see it:
[root@ca-zdudic2 nagios-scripts]#  git diff 
diff --cc add_build_systems.sh
index e1d4848,372a641..0000000
--- a/add_build_systems.sh
+++ b/add_build_systems.sh
@@@ -1,9 -1,7 +1,13 @@@
  #!/bin/sh
  #set -x

++<<<<<<< HEAD
 +# adding BUILD-SERVER to NAGIOS ...
 +######################################
 +
++=======
+ # adding BUILD server to nagios
++>>>>>>> testing
  # only adds host definition

  # --- ERROR SUBROUTINE

Checkout to testing branch is not allowed until conflict is fixed.
[root@ca-zdudic2 nagios-scripts]#  git checkout testing 
add_build_systems.sh: needs merge
error: you need to resolve your current index first

Even cat file shows the conflict:
[root@ca-zdudic2 nagios-scripts]# cat add_build_systems.sh
#!/bin/sh
#set -x

<<<<<<< HEAD
# adding BUILD-SERVER to NAGIOS ...
######################################

=======
# adding BUILD server to nagios
>>>>>>> testing

Edit the file, correct conflict, commit and this will complete merge process.
[root@ca-zdudic2 nagios-scripts]#  git commit -a 
[master 17e1637] Merge branch 'testing'
 Committer: root 
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 user.name "Your Name"
    git config --global user.email you@example.com

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

    git commit --amend --reset-author

[root@ca-zdudic2 nagios-scripts]#  git merge testing 
Already up-to-date.

Testing branch can be deleted now after successful merge,
[root@ca-zdudic2 nagios-scripts]#  git branch -d testing 
Deleted branch testing (was cfa44f1).

[root@ca-zdudic2 nagios-scripts]#  git branch 
* master

Collaboration

Clone /nagios-scripts repository to new directory /new-scripts so another user can work and contribute.
[root@ca-zdudic2 new-scripts]#  git clone /nagios-scripts /new-scripts 
Cloning into '/new-scripts'...
done.

[root@ca-zdudic2 new-scripts]# vi determine_parent.sh

[root@ca-zdudic2 new-scripts]#  git commit -a 
[master c61b997] start work on brumfield gateways
 Committer: root 
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 user.name "Your Name"
    git config --global user.email you@example.com

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

    git commit --amend --reset-author

 1 file changed, 2 insertions(+)

[root@ca-zdudic2 new-scripts]#  git log 
commit c61b997e17a4fbf276d15f6e2d06b67a38999f15
Author: root 
Date:   Sat Jan 21 22:03:57 2017 -0800

    start work on brumfield gateways

[root@ca-zdudic2 new-scripts]#  git show c61b997e17a4fbf276d15f6e2d06b67a38999f15 
commit c61b997e17a4fbf276d15f6e2d06b67a38999f15
Author: root 
Date:   Sat Jan 21 22:03:57 2017 -0800

    start work on brumfield gateways

diff --git a/determine_parent.sh b/determine_parent.sh
index 3689bf2..b725df0 100755
--- a/determine_parent.sh
+++ b/determine_parent.sh
@@ -2,6 +2,8 @@

 # scripts for adding systems in Nagios will call this script
 # in order to determine default gateway or parent
+# need to add Brumfiled CO
+# ---------------------------

 # check if there is one argument
 if [ $# != 1 ]; then

Now we can pull changes from /new-scripts/ repo to master branch in /nagios-scripts repo. The "pull" command does two things, it fetches changes from a remote branch, then merges them into the current branch.
[root@ca-zdudic2 nagios-scripts]#  git pull /new-scripts/ master 
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /new-scripts
 * branch            master     -> FETCH_HEAD
Updating 17e1637..c61b997
Fast-forward
 determine_parent.sh | 2 ++
 1 file changed, 2 insertions(+)

Back to the main page