How to use GIT Distributed version control system basic commands

How to use GIT Distributed version control system basic commands


What is git

– Git is a Distributed Version Control System
– Git is an Open source project
– In Git every developers working copy of the code is also a repository that can contain full history of all the changes

Below are some useful commands which we can use to setup git repository locally and remotely and
version control our projects.

1. Installing git – default
======================================================================================================
root@server1# yum install git
root@server1# rpm -qa | grep ^git*
git-1.8.3.1-4.el7.x86_64

2. Verifying the git installation
======================================================================================================
root@server1# git version
git version 1.8.3.1

3. git command line help options
======================================================================================================
root@server1# git help
root@server1# git help <command>
root@server1# git help config

4. Configuring git with user name and email address
======================================================================================================
root@server1# git config –global user.name “Sud”
root@server1# git config –global user.email “xxx@yyy.com”
root@server1# git config –global –list
user.name=Sud
user.email=xxx@yyy.com
root@server1# cat ~/.gitconfig
[user]
        name = Sud
        email = xxx@yyy.com

5. Initializing a git repository from a new project
======================================================================================================
root@server1# pwd
/home/student/gitspace
root@server1# git init simplestack
Initialized empty Git repository in /home/student/gitspace/simplestack/.git/

root@server1# pwd;ls -ahl
/home/student/gitspace/simplestack
total 4.0K
drwxr-xr-x. 3 root root   17 Jun  1 01:26 .
drwxr-xr-x. 3 root root   24 Jun  1 01:26 ..
drwxr-xr-x. 7 root root 4.0K Jun  1 01:26 .git

root@server1# pwd;ls -ahl
/home/student/gitspace/simplestack/.git
total 20K
drwxr-xr-x. 7 root root 4.0K Jun  1 01:26 .
drwxr-xr-x. 3 root root   17 Jun  1 01:26 ..
drwxr-xr-x. 2 root root    6 Jun  1 01:26 branches
-rw-r–r–. 1 root root   92 Jun  1 01:26 config
-rw-r–r–. 1 root root   73 Jun  1 01:26 description
-rw-r–r–. 1 root root   23 Jun  1 01:26 HEAD
drwxr-xr-x. 2 root root 4.0K Jun  1 01:26 hooks
drwxr-xr-x. 2 root root   20 Jun  1 01:26 info
drwxr-xr-x. 4 root root   28 Jun  1 01:26 objects
drwxr-xr-x. 4 root root   29 Jun  1 01:26 refs

6. Initializing a git repository for already existing project
======================================================================================================
root@server1# pwd
/home/student/gitspace
root@server1# mkdir simplehtml; cd simplehtml/
root@server1# touch index.html type.css login.html scripts.js
root@server1# ls -ltr
total 0
-rw-r–r–. 1 root root 0 Jun  1 01:32 type.css
-rw-r–r–. 1 root root 0 Jun  1 01:32 scripts.js
-rw-r–r–. 1 root root 0 Jun  1 01:32 login.html
-rw-r–r–. 1 root root 0 Jun  1 01:32 index.html

Now initialize this project for managing with git
root@server1# pwd
/home/student/gitspace/simplehtml
root@server1# git init
Initialized empty Git repository in /home/student/gitspace/simplehtml/.git/
root@server1# ls -ltr /home/student/gitspace/simplehtml/.git/
total 16
drwxr-xr-x. 4 root root   29 Jun  1 01:35 refs
drwxr-xr-x. 2 root root    6 Jun  1 01:35 branches
-rw-r–r–. 1 root root   73 Jun  1 01:35 description
drwxr-xr-x. 2 root root   20 Jun  1 01:35 info
drwxr-xr-x. 2 root root 4096 Jun  1 01:35 hooks
-rw-r–r–. 1 root root   23 Jun  1 01:35 HEAD
-rw-r–r–. 1 root root   92 Jun  1 01:35 config
drwxr-xr-x. 4 root root   28 Jun  1 01:35 objects

7. Staging, Commiting and check status Status of the project files
======================================================================================================
root@server1# pwd
/home/student/gitspace/simplehtml
root@server1# ls -ltr
total 0
-rw-r–r–. 1 root root 0 Jun  1 01:32 type.css
-rw-r–r–. 1 root root 0 Jun  1 01:32 scripts.js
-rw-r–r–. 1 root root 0 Jun  1 01:32 login.html
-rw-r–r–. 1 root root 0 Jun  1 01:32 index.html

root@server1# git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use “git add <file>…” to include in what will be committed)
#
#       index.html
#       login.html
#       scripts.js
#       type.css
nothing added to commit but untracked files present (use “git add” to track)

root@server1# git add *

root@server1# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use “git rm –cached <file>…” to unstage)
#
#       new file:   index.html
#       new file:   login.html
#       new file:   scripts.js
#       new file:   type.css
#

root@server1# git commit -m “sample files to commit”
[master (root-commit) baf9bc6] sample files to commit
 4 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 index.html
 create mode 100644 login.html
 create mode 100644 scripts.js
 create mode 100644 type.css

root@server1# git status
# On branch master
nothing to commit, working directory clean

root@server1# touch hello.html
root@server1# ls -ltr
total 0
-rw-r–r–. 1 root root 0 Jun  1 01:32 type.css
-rw-r–r–. 1 root root 0 Jun  1 01:32 scripts.js
-rw-r–r–. 1 root root 0 Jun  1 01:32 login.html
-rw-r–r–. 1 root root 0 Jun  1 01:32 index.html
-rw-r–r–. 1 root root 0 Jun  1 01:41 hello.html

root@server1# git status
# On branch master
# Untracked files:
#   (use “git add <file>…” to include in what will be committed)
#
#       hello.html
nothing added to commit but untracked files present (use “git add” to track)

root@server1# git add hello.html

root@server1# git status
# On branch master
# Changes to be committed:
#   (use “git reset HEAD <file>…” to unstage)
#
#       new file:   hello.html
#

root@server1# git commit -m “commit hello.html file”
[master e691792] commit hello.html file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hello.html

root@server1# git status
# On branch master
nothing to commit, working directory clean

8. Modifying and updating a file into repository
======================================================================================================

root@server1# pwd
/home/student/gitspace/simplehtml
root@server1# ls -ltr
total 0
-rw-r–r–. 1 root root 0 Jun  1 01:32 type.css
-rw-r–r–. 1 root root 0 Jun  1 01:32 scripts.js
-rw-r–r–. 1 root root 0 Jun  1 01:32 login.html
-rw-r–r–. 1 root root 0 Jun  1 01:32 index.html
-rw-r–r–. 1 root root 0 Jun  1 01:41 hello.html
root@server1# vi hello.html
root@server1# cat hello.html
<html>
<head>
        <title> This is a simple web page </title>
</head>
<body>
<h1> Hello World !! <h1>
</body>
</html>

root@server1# git status
# On branch master
# 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:   hello.html
#
no changes added to commit (use “git add” and/or “git commit -a”)

root@server1# git add hello.html

root@server1# git status
# On branch master
# Changes to be committed:
#   (use “git reset HEAD <file>…” to unstage)
#
#       modified:   hello.html
#

root@server1# git commit -m “Adding the modified file”
[master a2de3e0] Adding the modified file
 1 file changed, 8 insertions(+)

root@server1# git status
# On branch master
nothing to commit, working directory clean

9. Adding and commiting a modified file in single step
======================================================================================================

root@server1# pwd
/home/student/gitspace/simplehtml
# ls -ltr
total 4
-rw-r–r–. 1 root root   0 Jun  1 01:32 type.css
-rw-r–r–. 1 root root   0 Jun  1 01:32 scripts.js
-rw-r–r–. 1 root root   0 Jun  1 01:32 login.html
-rw-r–r–. 1 root root   0 Jun  1 01:32 index.html
-rw-r–r–. 1 root root 114 Jun  1 01:50 hello.html

root@server1# vi hello.html
root@server1# git status
# On branch master
# 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:   hello.html
#
no changes added to commit (use “git add” and/or “git commit -a”)

root@server1# git commit -am “Adding, commiting and putting a comment to the modified file”
[master 9e99a38] Adding, commiting and putting a comment to the modified file
 1 file changed, 1 insertion(+)

root@server1# git status
# On branch master
nothing to commit, working directory clean

10. Unstaging and discarding changes in the current working directory
======================================================================================================

root@server1# pwd
/home/student/gitspace/simplehtml
root@server1# ls -ltr
total 4
-rw-r–r–. 1 root root   0 Jun  1 01:32 type.css
-rw-r–r–. 1 root root   0 Jun  1 01:32 scripts.js
-rw-r–r–. 1 root root   0 Jun  1 01:32 login.html
-rw-r–r–. 1 root root   0 Jun  1 01:32 index.html
-rw-r–r–. 1 root root 159 Jun  1 01:57 hello.html
root@server1# cat hello.html
<html>
<head>
        <title> This is a simple web page </title>
</head>
<body>
<h1> Hello World !! <h1>
<p>This space is for some useful purpose</p>
</body>
</html>
# vi hello.html
# cat hello.html
<html>
<head>
        <title> This is a simple web page </title>
</head>
<body>
<h1> Hello World !! <h1>
<p>This space is for some useful purpose</p>
<h3> DO NOT WANT THIS CHANGE TO BE COMMITED </h3>
</body>
</html>

root@server1# git status
# On branch master
# 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:   hello.html
#
no changes added to commit (use “git add” and/or “git commit -a”)

root@server1# git add hello.html

root@server1# git status
# On branch master
# Changes to be committed:
#   (use “git reset HEAD <file>…” to unstage)
#
#       modified:   hello.html
#

root@server1# git reset HEAD hello.html
Unstaged changes after reset:
M       hello.html

root@server1# git status
# On branch master
# 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:   hello.html
#
no changes added to commit (use “git add” and/or “git commit -a”)

root@server1# git checkout — hello.html

root@server1# git status
# On branch master
nothing to commit, working directory clean

root@server1# cat hello.html
<html>
<head>
        <title> This is a simple web page </title>
</head>
<body>
<h1> Hello World !! <h1>
<p>This space is for some useful purpose</p>
</body>
</html>

root@server1# git status
# On branch master
nothing to commit, working directory clean

11. History of the commits
======================================================================================================

root@server1# git log
commit 9e99a38c16ee86e38fc4323b8907bf667fb92cd2
Author: Sudhir B <xxx@yyy.com>
Date:   Thu Jun 1 01:57:53 2017 -0400

    Adding, commiting and putting a comment to the modified file

commit a2de3e0ee70984246c1b50f369789e19769e266a
Author: Sudhir B <xxx@yyy.com>
Date:   Thu Jun 1 01:51:09 2017 -0400

    Adding the modified file

commit e69179281ca7d40962c93cc33f47aa2445f7c23d
Author: Sudhir B <xxx@yyy.com>
Date:   Thu Jun 1 01:42:41 2017 -0400

    commit hello.html file

commit baf9bc6cdd4820c976e924842080362405e064aa
Author: Sudhir B <xxx@yyy.com>
Date:   Thu Jun 1 01:40:59 2017 -0400

    sample files to commit

root@server1# git log –oneline
9e99a38 Adding, commiting and putting a comment to the modified file
a2de3e0 Adding the modified file
e691792 commit hello.html file
baf9bc6 sample files to commit

root@server1# git log –oneline –graph
* 9e99a38 Adding, commiting and putting a comment to the modified file
* a2de3e0 Adding the modified file
* e691792 commit hello.html file
* baf9bc6 sample files to commit

root@server1# git log –oneline –graph –decorate
* 9e99a38 (HEAD, master) Adding, commiting and putting a comment to the modified file
* a2de3e0 Adding the modified file
* e691792 commit hello.html file
* baf9bc6 sample files to commit

root@server1# git log –oneline –graph –decorate –color
* 9e99a38 (HEAD, master) Adding, commiting and putting a comment to the modified file
* a2de3e0 Adding the modified file
* e691792 commit hello.html file
* baf9bc6 sample files to commit

12. Removing commited file
======================================================================================================

root@server1# pwd
/home/student/gitspace/simplehtml
root@server1# touch test.sh
root@server1# echo “Hello..” > test.sh
root@server1# ls -ltr
total 8
-rw-r–r–. 1 root root   0 Jun  1 01:32 type.css
-rw-r–r–. 1 root root   0 Jun  1 01:32 scripts.js
-rw-r–r–. 1 root root   0 Jun  1 01:32 login.html
-rw-r–r–. 1 root root   0 Jun  1 01:32 index.html
-rw-r–r–. 1 root root 159 Jun  1 02:25 hello.html
-rw-r–r–. 1 root root   8 Jun  1 02:42 test.sh

root@server1# git status
# On branch master
# Untracked files:
#   (use “git add <file>…” to include in what will be committed)
#
#       test.sh
nothing added to commit but untracked files present (use “git add” to track)

root@server1# git add test.sh

root@server1# git status
# On branch master
# Changes to be committed:
#   (use “git reset HEAD <file>…” to unstage)
#
#       new file:   test.sh
#

root@server1# git commit -m “Adding the test.sh script file”
[master 1973417] Adding the test.sh script file
 1 file changed, 1 insertion(+)
 create mode 100644 test.sh
root@server1# git status
# On branch master
nothing to commit, working directory clean

root@server1# git rm test.sh
rm ‘test.sh’

root@server1# git status
# On branch master
# Changes to be committed:
#   (use “git reset HEAD <file>…” to unstage)
#
#       deleted:    test.sh
#

root@server1# ls -ltr
total 4
-rw-r–r–. 1 root root   0 Jun  1 01:32 type.css
-rw-r–r–. 1 root root   0 Jun  1 01:32 scripts.js
-rw-r–r–. 1 root root   0 Jun  1 01:32 login.html
-rw-r–r–. 1 root root   0 Jun  1 01:32 index.html
-rw-r–r–. 1 root root 159 Jun  1 02:25 hello.html

root@server1# git commit -m “deleting the file”
[master f16737d] deleting the file
 1 file changed, 1 deletion(-)
 delete mode 100644 test.sh

root@server1# git status
# On branch master
nothing to commit, working directory clean

13. Deleting an abruptly removed file
======================================================================================================

root@server1# ls -ltr
total 4
-rw-r–r–. 1 root root   0 Jun  1 01:32 type.css
-rw-r–r–. 1 root root   0 Jun  1 01:32 scripts.js
-rw-r–r–. 1 root root   0 Jun  1 01:32 login.html
-rw-r–r–. 1 root root   0 Jun  1 01:32 index.html
-rw-r–r–. 1 root root 159 Jun  1 02:25 hello.html

root@server1# touch unknown.txt
root@server1# git add unknown.txt
root@server1# git commit -m “Commiting a unknown.txt file”
[master 28ca01e] Commiting a unknown.txt file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 unknown.txt

root@server1# git status
# On branch master
nothing to commit, working directory clean

root@server1# rm -rf unknown.txt

root@server1# git status
# On branch master
# Changes not staged for commit:
#   (use “git add/rm <file>…” to update what will be committed)
#   (use “git checkout — <file>…” to discard changes in working directory)
#
#       deleted:    unknown.txt
#
no changes added to commit (use “git add” and/or “git commit -a”)

root@server1# git add -u

# git status
# On branch master
# Changes to be committed:
#   (use “git reset HEAD <file>…” to unstage)
#
#       deleted:    unknown.txt
#

root@server1# git commit -m “Delete this file”
[master adcf6ca] Delete this file
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 unknown.txt

root@server1# git status
# On branch master
nothing to commit, working directory clean

14. Moving a file using git command
======================================================================================================

Let say i want to move all *.html file into web directory

root@server1# pwd
/home/student/gitspace/simplehtml
root@server1# ls -ltr
total 4
-rw-r–r–. 1 root root   0 Jun  1 01:32 type.css
-rw-r–r–. 1 root root   0 Jun  1 01:32 scripts.js
-rw-r–r–. 1 root root   0 Jun  1 01:32 login.html
-rw-r–r–. 1 root root   0 Jun  1 01:32 index.html
-rw-r–r–. 1 root root 159 Jun  1 02:25 hello.html

root@server1# mkdir web
root@server1# git mv *.html web
root@server1# cd web/
root@server1# ls -ltr
total 4
-rw-r–r–. 1 root root   0 Jun  1 01:32 login.html
-rw-r–r–. 1 root root   0 Jun  1 01:32 index.html
-rw-r–r–. 1 root root 159 Jun  1 02:25 hello.html
root@server1# cd ..
root@server1# ls -ltr
total 0
-rw-r–r–. 1 root root  0 Jun  1 01:32 type.css
-rw-r–r–. 1 root root  0 Jun  1 01:32 scripts.js
drwxr-xr-x. 2 root root 57 Jun  1 03:07 web

root@server1# git status
# On branch master
# Changes to be committed:
#   (use “git reset HEAD <file>…” to unstage)
#
#       renamed:    hello.html -> web/hello.html
#       renamed:    index.html -> web/index.html
#       renamed:    login.html -> web/login.html
#

root@server1# git commit -m “Moved the *.html files into web directory”
[master 1874327] Moved the *.html files into web directory
 3 files changed, 0 insertions(+), 0 deletions(-)
 rename hello.html => web/hello.html (100%)
 rename index.html => web/index.html (100%)
 rename login.html => web/login.html (100%)

root@server1# git status
# On branch master
nothing to commit, working directory clean

15. Ignoring file that should not be commited
======================================================================================================

root@server1# pwd
/home/student/gitspace/simplehtml
root@server1# touch SystemOut.log

root@server1# git status
# On branch master
# Untracked files:
#   (use “git add <file>…” to include in what will be committed)
#
#       SystemOut.log
nothing added to commit but untracked files present (use “git add” to track)

root@server1# vi .gitignore
root@server1# cat .gitignore
*.log

root@server1# 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)

root@server1# git add .gitignore

root@server1# git commit -m “Adding .gitignore file”
[master 11daa10] Adding .gitignore file
 1 file changed, 1 insertion(+)
 create mode 100644 .gitignore
root@server1# git status
# On branch master
nothing to commit, working directory clean

16. Setting up ssh authentication with github repository
======================================================================================================

root@server1# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
a3:bd:ee:d2:49:68:d1:7a:a8:f2:83:48:07:cf:86:c7 root@server1
The key’s randomart image is:
+–[ RSA 2048]—-+
|                 |
|                 |
|       .         |
| .    . .        |
|  *    =S        |
| o E  =oo.       |
|..+. o.+..       |
|. o o . o.       |
|   o.. ++        |
+—————–+

root@server1# pwd
/root/.ssh
root@server1# ls -ltr
total 16
-rw——-. 1 root root  617 Nov 20  2014 authorized_keys
-rw-r–r–. 1 root root  364 Dec  3  2014 known_hosts
-rw-r–r–. 1 root root  407 Jun  1 04:53 id_rsa.pub
-rw——-. 1 root root 1766 Jun  1 04:53 id_rsa

– Copy the content of id_rsa.pub key file (public key file) and add the key into github

Goto – github.com/user settings – SSH and GPG keys – New SSH key – Copy the content – Add SSH key

– Now SSH to github as show below

root@server1# ssh -T git@github.com
The authenticity of host ‘github.com (192.30.253.112)’ can’t be established.
RSA key fingerprint is xxx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘github.com,192.30.253.112’ (RSA) to the list of known hosts.
Enter passphrase for key ‘/root/.ssh/id_rsa’:
Hi xxx! You’ve successfully authenticated, but GitHub does not provide shell access.

17. Staging, Commiting and Pushing content to remote git repository
======================================================================================================

root@server1# cd /home/student/gitspace/
root@server1# ls -ltr
total 0
drwxr-xr-x. 3 root root 17 Jun  1 01:26 simplestack
drwxr-xr-x. 4 root root 96 Jun  1 04:43 simplehtml
root@server1# cd simplestack/
root@server1# ls -ltr
total 0

root@server1# git remote add origin git@github.com:xxx/consolejava.git
# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use “git add” to track)

root@server1# touch simple.txt
root@server1# ls -ltr
total 0
-rw-r–r–. 1 root root 0 Jun  1 05:17 simple.txt

root@server1# git add simple.txt
root@server1# git commit -m “Add txt file”
[master (root-commit) 0527f8a] Add txt file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 simple.txt

root@server1# git status
# On branch master
nothing to commit, working directory clean

root@server1# git remote -v
origin  git@github.com:xxx/consolejava.git (fetch)
origin  git@github.com:xxx/consolejava.git (push)

root@server1# git pull origin master
Warning: Permanently added the RSA host key for IP address ‘192.30.255.113’ to the list of known hosts.
Enter passphrase for key ‘/root/.ssh/id_rsa’:
warning: no common commits
remote: Counting objects: 18, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 18 (delta 7), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (18/18), done.
From github.com:xxx/consolejava
 * branch            master     -> FETCH_HEAD
Already up-to-date!

root@server1# git push -u origin master
Warning: Permanently added the RSA host key for IP address ‘192.30.253.113’ to the list of known hosts.
Enter passphrase for key ‘/root/.ssh/id_rsa’:
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 358 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To git@github.com:xxx/consolejava.git
   0bbab0a..4a40d2c  master -> master
Branch master set up to track remote branch master from origin.

root@server1# git push  origin master
Enter passphrase for key ‘/root/.ssh/id_rsa’:
Everything up-to-date

Hope you enjoyed reading this article. Thank you.