A Complete Guide on Version Control System (Git) for System Administrator/Linux/Centos

  • What is version Control?

To understand what is a version control system lets understand with an example lets say you have file document1.txt this will be the first version of the document, later we update the document and save it as a document2.txt, this will be the second version of the document and lastly we make more changes and save it as a third time as document3.txt, now we have the 3 version of the documents as 3 files, this will allow us to go back to the previous version if we want to and even we can compare two versions manually either using tool using as diff command in linux or just see manually.

The problem of the system is that after make the changes 100 times the system get unviewly and also if the person of the team collaborating on a project and more than one person working on the document things get complex fast, multiple people may have the version of the same name that are not identical files.

A version control system manages the version for us each time we update the document we save it to original document and version control system track the change for us
this allows us to revert to the previous version, compare the versions and even collaborate with people working on the same document, it shows who made those changes and what were the changes.

A little History of Version Control :

1. In 1982 : Revision Control System (RCS) was released

RCS Feature:

– First widely used, cross-platform version control system.
– Most recent file is stored in full.
– Apply patches to go back to previous version.
– Tracked version of single files.
– Did not support directories or projects.
– Did not support binary files.
– Stored file locally.

2. In the late 1980s: Concurrent version system (CVS) was created.

CVS Feature:

– Allowed concurrent version , multiple people working on the same file on the same time.
– Files stored in CVS repository on the network.
– Saw files as single entities with version.
– Did not support binary files.

– 2000: Subversion was released:

– Subversion able to see entire directory or projects as one identity

SVN Feature:

– Do not manage separate version of each file.
– Subversion sees directories and projects as entities.
– Subversion tracks everything in directory.
– Subversion stores files in a centralized repository.

3. 2005: Git was released.

      And this article is focused on specific Git version control system which is widely used by the Developer/System Administrator.

      About Git:

      – Open Source : Meaning everyone has access to the source code

      – Free : As you don’t have to pay for it.

      – Very popular : among programmer specially open source programmer

      – Very Powerful.

      Git Features:

      – Distributed version control.
      – Each user has their own local repository.
      – It is not centralized.
      – Tracks changes, not version.

      Git Installation :

      The installation of git is quit easy, you can use yum to install the package.

      [[email protected] ~]# yum install git
      Loaded plugins: amazon-id, search-disabled-repos
      Resolving Dependencies
      --> Running transaction check
      ---> Package git.x86_64 0:1.8.3.1-23.el7_8 will be installed
      --> Processing Dependency: perl-Git = 1.8.3.1-23.el7_8 for package: git-1.8.3.1-23.el7_8.x86_64
      --> Processing Dependency: perl(Git) for package: git-1.8.3.1-23.el7_8.x86_64
      --> Running transaction check
      ---> Package perl-Git.noarch 0:1.8.3.1-23.el7_8 will be installed
      --> Finished Dependency Resolution
      
      Dependencies Resolved
      
      ==========================================
      Package Arch Version Repository Size
      ==========================================
      Installing:
      git x86_64 1.8.3.1-23.el7_8 rhel-7-server-rhui-rpms 4.4 M
      Installing for dependencies:
      perl-Git noarch 1.8.3.1-23.el7_8 rhel-7-server-rhui-rpms 56 k
      
      Transaction Summary
      =========================================
      Install 1 Package (+1 Dependent package)
      
      Total download size: 4.5 M
      Installed size: 22 M
      Is this ok [y/d/N]: y

       

      and That’s it.

       

      Git Configure:

      # git config --global user.name "technicalmint"
      # git config --global user.email "[email protected]"
      # git config --global core.editor "vim"
      # git config --global color.ui "true"
      # git config --list
      
      user.name=technicalmint
      [email protected]
      core.editor=vim
      color.ui=true
      core.repositoryformatversion=0
      core.filemode=true
      core.bare=false
      core.logallrefupdates=true

       

      Now create a directory and initialize the git.

      [[email protected] ~]# mkdir GitProject
      [[email protected] ~]# cd GitProject
      [[email protected] GitProject]# git init
      Initialized empty Git repository in /root/GitProject/.git/
      [[email protected] GitProject]# cd .git/
      [[email protected] .git]# ls
      branches config description HEAD hooks info objects refs
      [[email protected] .git]#

      Committing and adding:

      [[email protected] GitProject]# vim firstfile.txt
      
      This is our first file
      
      wq!

       

      [[email protected] GitProject]# git status
      # On branch master
      #
      # Initial commit
      #
      # Untracked files:
      # (use "git add <file>..." to include in what will be committed)
      #
      # firstfile.txt
      nothing added to commit but untracked files present (use "git add" to track)
      [[email protected] GitProject]# git add firstfile.txt

      The above command tells git to track the file (it hasn’t tracked the changes yet, its been just told to pay attention to the file).

      [[email protected] GitProject]# git status
      # On branch master
      #
      # Initial commit
      #
      # Changes to be committed:
      # (use "git rm --cached <file>..." to unstage)
      #
      # new file: firstfile.txt
      #
      
      [[email protected] GitProject]# git commit -m "first commit"
      
      The above command changes commit to the disk
      [master (root-commit) 235fe0d] first commit
      1 file changed, 1 insertion(+)
      create mode 100644 firstfile.txt

       

      Note: git doesn’t store the version of any file but rather changes to them.

      [[email protected] GitProject]# vim firstfile.txt
      
      This is a second line.
      
      wq!
      
      [[email protected] GitProject]# git add firstfile.txt
      [[email protected] GitProject]# git commit -a -m "second commit"
      [master b49bab1] second commit
      1 file changed, 3 deletions(-)
      [[email protected] GitProject]#

       

      Where -a : “all changes”

      => git stores changes not version.

      Analyzing git commit history:

      [[email protected] GitProject]# git log
      commit b49bab193f63969fa8765d795cef14e74e1b71fa
      Author: technicalmint <[email protected]>
      Date: Fri Jul 3 17:48:28 2020 +0000
      
      second commit
      
      commit 946beb6a1c46b789316cd4312da1c5a816d7b89e
      Author: technicalmint <[email protected]>
      Date: Fri Jul 3 17:46:22 2020 +0000
      
      second commit

       

      [[email protected] GitProject]# git log -n 1       => TO check the most recent one
      commit b49bab193f63969fa8765d795cef14e74e1b71fa
      Author: technicalmint <[email protected]>
      Date: Fri Jul 3 17:48:28 2020 +0000
      
      second commit
      
      All commit with certain dates
      
      [[email protected] GitProject]# git log --since=2020-06-20
      commit b49bab193f63969fa8765d795cef14e74e1b71fa
      Author: technicalmint <[email protected]>
      Date: Fri Jul 3 17:48:28 2020 +0000
      
      second commit
      
      commit 946beb6a1c46b789316cd4312da1c5a816d7b89e
      Author: technicalmint <[email protected]>
      Date: Fri Jul 3 17:46:22 2020 +0000
      
      second commit

      Ignoring Files:

      There may be time when you want to store file in the git project directory but you don’t want git to track them, what we exactly want to git to completely ignore the file.

      [[email protected] GitProject]# vim .gitignore
      
      *.exe
      *.zip
      *.tar.gz
      *.swp
      
      wq!
      
      [[email protected] GitProject]# touch archive.zip
      
      [[email protected] GitProject]# git status
      # On branch master
      # Untracked files:
      # (use "git add <file>..." to include in what will be committed)
      #
      # .gitignore

       

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

      => .zip file has been ignored in above result.

      Rolling Back the Changes.

      [[email protected] GitProject]# vim firstfile.txt
      
      delete the second line
      
      [[email protected] GitProject]# git diff
      diff --git a/firstfile.txt b/firstfile.txt
      index 30066a6..ee32aa3 100644
      --- a/firstfile.txt
      +++ b/firstfile.txt
      @@ -1,3 +1,2 @@
      This is our first line.
      
      -This is our second line.
      [[email protected] GitProject]#

       

      # git diff -> this command compares the disk file on the repository we see our deleted line in the red.

      Now, what we want to do is copy the version of file project that’s include the line back over the current working directory to do so check the file on the project this will take current project file and copy into the directory.

      [[email protected] GitProject]# git checkout -- firstfile.txt
      
      Above command is not to consider the another branch but rather pull up from the current branch.
      
      [[email protected] GitProject]# cat firstfile.txt
      This is our first line.
      
      This is our second line.
      [[email protected] GitProject]#

       

      -> and we can see that the line is back, this works for file changes in the directory but not staged or committed.

      -> Now lets delete the line again.

      [[email protected] GitProject]# vim firstfile.txt
      
      delete the second line
      
      [[email protected] GitProject]# git add firstfile.txt
      [[email protected] GitProject]# git status
      # On branch master
      # Changes to be committed:
      # (use "git reset HEAD <file>..." to unstage)
      #
      # new file: .gitignore
      # modified: firstfile.txt
      #

       

      Now, Color has changed.

      Unstaged changes after reset:

      [[email protected] GitProject]# git reset HEAD firstfile.txt
      Unstaged changes after reset:
      M firstfile.txt
      [[email protected] GitProject]# git status
      # On branch master
      # Changes to be committed:
      # (use "git reset HEAD <file>..." to unstage)
      #
      # new file: .gitignore
      #
      # 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: firstfile.txt
      #

       

      [[email protected] GitProject]# git checkout -- firstfile.txt

      One more time delete the line and commit this time:

      [[email protected] GitProject]# vim firstfile.txt
      
      delete the second line
      
      [[email protected] GitProject]# git add firstfile.txt
      [[email protected] GitProject]# git commit -m "deleted second line"
      [master 2dbc351] deleted second line
      3 files changed, 4 insertions(+), 1 deletion(-)
      create mode 100644 .gitignore
      delete mode 100644 archive.zip
      [[email protected] GitProject]# git log -n1
      commit 2dbc351e1ce3e819760a204a0cf5290e3d540be1
      Author: technicalmint <[email protected]>
      Date: Sat Jul 4 07:29:43 2020 +0000

       

      deleted second line

      [[email protected] GitProject]# git revert 2dbc351e1ce3e819760
      
      [[email protected] GitProject]# cat firstfile.txt
      This is our first line.
      
      This is our second line.
      [[email protected] GitProject]# git log -n1
      
      commit ef97bcafe9f2fbc7b10c2c46a43fa884ba64dd54
      Author: technicalmint <[email protected]>
      Date: Sat Jul 4 07:32:13 2020 +0000
      
      Revert "deleted second line"
      
      This reverts commit 2dbc351e1ce3e819760a204a0cf5290e3d540be1.

      Git Branching:

      One of the more powerful feature of git is branching, branching allow us to try new ideas, switch between branches quickly and allow collaboration new features without disrupting current one.

      Long story Short.
      – Create a test copy of all files
      – Make changes
      – Test changes
      – Merge changes back into production copy
      or
      – If we don’t like the changes (throw away test copy)

      Creating Branches:

      [[email protected] GitProject]# git branch
      * master
      
      *master -> current branch is master
      
      Let's create a new branch
      
      [[email protected] GitProject]# git branch testconfig
      [[email protected] GitProject]# git branch
      * master
      testconfig
      [[email protected] GitProject]# git checkout testconfig
      Switched to branch 'testconfig'
      [[email protected] GitProject]# git branch
      master
      * testconfig
      [[email protected] GitProject]# vim firstfile.txt
      
      this is our third line.
      
      wq!
      
      [[email protected] GitProject]# git commit -a -m "This adds a third line"
      [testconfig 3cc54d6] This adds a third line
      1 file changed, 2 insertions(+)
      [[email protected] GitProject]# git log -n1
      commit 3cc54d6925a4fa4c607dc289491885bc8ccaf0f3
      Author: technicalmint <[email protected]>
      Date: Sat Jul 4 07:41:12 2020 +0000
      
      This adds a third line
      [[email protected] GitProject]# git checkout master
      Switched to branch 'master'
      [[email protected] GitProject]# cat firstfile.txt
      This is our first line.
      
      This is our second line.
      [[email protected] GitProject]#

       

      -> We can see new line is not here as we are in master branch.
      -> We can consider as master branch as production config and testconfig is test env.

      [[email protected] GitProject]# git log -n1
      commit ef97bcafe9f2fbc7b10c2c46a43fa884ba64dd54
      Author: technicalmint <[email protected]>
      Date: Sat Jul 4 07:32:13 2020 +0000
      
      Revert "deleted second line"
      
      This reverts commit 2dbc351e1ce3e819760a204a0cf5290e3d540be1.
      [[email protected] GitProject]#

       

      -> We can see commit does not exist in this branch.

      Comparing, renaming, and deleting branches:

      [[email protected] GitProject]# git branch
      * master
      testconfig
      
      To check what changes the testconfig make.
      
      [[email protected] GitProject]# git diff master..testconfig
      diff --git a/firstfile.txt b/firstfile.txt
      index 30066a6..26f4c00 100644
      --- a/firstfile.txt
      +++ b/firstfile.txt
      @@ -1,3 +1,5 @@
      This is our first line.
      
      This is our second line.
      +
      +this is our third line.

      [[email protected] GitProject]# git diff master..testconfig^

      add ^(carrot sysmbol) -> This will show what was the previous version of testconfig branch

      Rename the branch:

      -> just like in linux use –move to renaming.

      [[email protected] GitProject]# git branch --move testconfig development
      [[email protected] GitProject]# git branch
      developement
      * master
      
      [[email protected] GitProject]# git checkout development
      
      [[email protected] GitProject]# git branch
      * development
      master
      [[email protected] GitProject]# git branch development-grant
      
      => Now we have one another branch.
      [[email protected] GitProject]# git branch
      * development
      development-grant
      master

      Delete the branch:

      [[email protected] GitProject]# git branch
      * development
      master
      [[email protected] GitProject]# git branch development-grant
      [[email protected] GitProject]# git branch
      * development
      development-grant
      master
      [[email protected] GitProject]# git branch --delete development-grant
      Deleted branch development-grant (was 3cc54d6).
      [[email protected] GitProject]# git branch
      * development
      master

      -> you need to switch into different branch and then delete it also if you have made changes in the branch it will warned you.

      # git branch -D <branch name> -> this will overwrite the warning

      Merging Branches:

      We have two types of merger, the first one is fast forward merge, in this case the master branch stays the same and the only changes in the development branch we made.

      [[email protected] GitProject]# git diff master..development
      diff --git a/firstfile.txt b/firstfile.txt
      index 30066a6..26f4c00 100644
      --- a/firstfile.txt
      +++ b/firstfile.txt
      @@ -1,3 +1,5 @@
      This is our first line.
      
      This is our second line.
      +
      +this is our third line.
      [[email protected] GitProject]#

      -> So to do fast forward merge we need to do use git merge.
      -> Now go back to master branch.

      [[email protected] GitProject]# git checkout master
      Switched to branch 'master'
      
      [[email protected] GitProject]# git merge development
      Updating ef97bca..3cc54d6
      Fast-forward
      firstfile.txt | 2 ++
      1 file changed, 2 insertions(+)
      [[email protected] GitProject]#

       

      -> now verify using diff command.

      [[email protected] GitProject]# git diff master..development
      [[email protected] GitProject]#

      Now again checkout for development.

      [[email protected] GitProject]# git checkout development
      Switched to branch 'development'
      [[email protected] GitProject]# vim firstfile.txt
      
      This is our fourth line.
      
      wq!
      
      [[email protected] GitProject]# git add firstfile.txt
      [[email protected] GitProject]# git commit -m "This adds fourth line"
      [development f499f8e] This adds fourth line
      1 file changed, 2 insertions(+)
      [[email protected] GitProject]# git diff master..development
      diff --git a/firstfile.txt b/firstfile.txt
      index 26f4c00..0be3e1f 100644
      --- a/firstfile.txt
      +++ b/firstfile.txt
      @@ -3,3 +3,5 @@ This is our first line.
      This is our second line.
      
      this is our third line.
      +
      +This is our fourth line.
      [[email protected] GitProject]#

       

      There is scenario when you don’t do the fast forward merge and the real merge or second type.

      [[email protected] GitProject]# git checkout master
      Switched to branch 'master'
      [[email protected] GitProject]# vim firstfile.txt
      Now adding 4th line.
      wq!
      
      [[email protected] GitProject]# git add firstfile.txt
      [[email protected] GitProject]# git commit -m "This adds a 4th line"
      [master 1036526] This adds a 4th line
      1 file changed, 2 insertions(+)
      [[email protected] GitProject]# git diff master..development
      diff --git a/firstfile.txt b/firstfile.txt
      index 2179914..0be3e1f 100644
      --- a/firstfile.txt
      +++ b/firstfile.txt
      @@ -4,4 +4,4 @@ This is our second line.
      
      this is our third line.
      
      - Now adding 4th line.
      +This is our fourth line.
      [[email protected] GitProject]#

       

      -> Now we see that both branches have committed
      -> Now let’s attempt to merge

      [[email protected] GitProject]# git merge development
      Auto-merging firstfile.txt
      CONFLICT (content): Merge conflict in firstfile.txt
      Automatic merge failed; fix conflicts and then commit the result.
      [[email protected] GitProject]#

      We see the error : Automatic merge failed; fix conflicts and then commit the result.

      [[email protected] GitProject]# git status
      # On branch master
      # You have unmerged paths.
      # (fix conflicts and run "git commit")
      #
      # Unmerged paths:
      # (use "git add <file>..." to mark resolution)
      #
      # both modified: firstfile.txt
      #
      no changes added to commit (use "git add" and/or "git commit -a")
      [[email protected] GitProject]#

      -> This tells that both branch has commit the changes and I need to resolve that before the merge.

      Now let’s open the problem file:

      [[email protected] GitProject]# vim firstfile.txt
      
      This is our first line.
      
      This is our second line.
      
      this is our third line.
      
      <<<<<<< HEAD
      Now adding 4th line.
      =======
      This is our fourth line.
      >>>>>>> development

       

      We see like below:

      <<<<<<< HEAD
      Now adding 4th line.
      =======
      This is our fourth line.
      >>>>>>> development

      -> At this point We have to manually decide which one we have to keep and which one we have to delete, lets make it like below.

      This is our fourth line.

      Now add the file using git add.

      [[email protected] GitProject]# git commit  => Hit Enter.
      
      Merge branch 'development'
      
      wq!
      
      [master 3848165] Merge branch 'development'
      [[email protected] GitProject]#
      
      -> we can verify it using git diff command.
      
      [[email protected] GitProject]# git diff master..development
      
      -> and we can see both branches are the same

       

      # Now see graphically.

      [[email protected] GitProject]# git log --graph --oneline
      * 3848165 Merge branch 'development'
      |\
      | * f499f8e This adds fourth line
      * | 1036526 This adds a 4th line
      |/
      * 3cc54d6 This adds a third line
      * ef97bca Revert "deleted second line"
      * 2dbc351 deleted second line
      * 736834c archive file
      * b49bab1 second commit
      * d000ad0 third commit
      * 946beb6 second commit
      * c7e15fc first commit
      * 235fe0d first commit
      [[email protected] GitProject]#

      Now open a github account and push the file to git repository.

      # git remote add origin https://github.com/your-git-hub-repository.
      # git push -u origin master
      
      # git remote -v
      origin https://github.com/your-git-hub-repository. (fetch)
      origin https://github.com/your-git-hub-repository. (push)

       

      TO clone

      # git clone and then url

      Getting and Pushing to a remote repository:

      # vim firstfile.txt
      This is another line to remote repository
      
      wq!

       

      # git status
      # git commit -a -m "this adds a remote line"
      # git status
      # git push origin master
      username:
      password:

      => git fetch does not merge you have to do it manually, git pull fetched and merges.

      SSH-Key (optional)

      # ssh-keygen -t rsa -b 4096 -C "your-email_address"

      That’s it in this article, hope you enjoyed it. Please share it across if think it’s good.

      Thanks for reading!! Have a great day!!

       

      Leave a Reply

      Your email address will not be published. Required fields are marked *