Thursday, February 15, 2018

Useful GIT Commands

This article explains about the GIT basics. The below commands were executed in Ubuntu 17.04. As the initial step use the below command to install the GIT client in Ubuntu 17.04.

 sudo add-apt-repository ppa:git-core/ppa  
 sudo apt-get update  
 sudo apt-get install git  

Then create account in GIT, to do that you can go to [1] https://github.com/

Once we are done with the GIT account creation, the next step is to create a repository. for example I have created a repository as below:



Now we are ready to go through the scenario's where we encounter in programming.


1) Moving the local files to a New GIT Repository.


- For this I have created a folder with two files as below:


- Then execute the below command inside the folder.

 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git init  
 Initialized empty Git repository in /home/ajanthan/testgitrepo/.git/  
 ajanthan@ajanthan-Lenovo:~/testgitrepo$   

- Then execute the below:
 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git add .  

- Before commit the files we need execute the below command, to tell our folder, under what account need to commit the files.
 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git config --global user.email "ajanthaneng@gmail.com"  
 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git config --global user.name "ajanthanerepo"  

- Now execute the below:
 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git commit -m "Test commit"  
 [master (root-commit) 2d6bc85] Test commit  
  2 files changed, 4 insertions(+)  
  create mode 100644 README.md  
  create mode 100644 test.json  

- Now we need to specify the repository where the local files need to be moved, to that execute the below command, mentioning the repository url.
 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git remote add origin https://github.com/ajanthanerepo/test-repo-ajan1  
 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git remote -v  
 origin     https://github.com/ajanthanerepo/test-repo-ajan1 (fetch)  
 origin     https://github.com/ajanthanerepo/test-repo-ajan1 (push)  
 ajanthan@ajanthan-Lenovo:~/testgitrepo$   

- When we go to the repository still we couldn't see any files added, once we execute the below, then only the files will be moved from local.


 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git push origin master  
 Username for 'https://github.com': ajanthaneng@gmail.com  
 Password for 'https://ajanthaneng@gmail.com@github.com':   
 Counting objects: 4, done.  
 Delta compression using up to 4 threads.  
 Compressing objects: 100% (2/2), done.  
 Writing objects: 100% (4/4), 290 bytes | 290.00 KiB/s, done.  
 Total 4 (delta 0), reused 0 (delta 0)  
 To https://github.com/ajanthanerepo/test-repo-ajan1  
  * [new branch]   master -> master  
 ajanthan@ajanthan-Lenovo:~/testgitrepo$   

- Now we can see our files moved successfully.


- Also we can create a branch using the below commands.
 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git branch testbranch  
 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git status  
 On branch master  
 nothing to commit, working tree clean  
 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git checkout testbranch  
 Switched to branch 'testbranch'  
 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git status  
 On branch testbranch  
 nothing to commit, working tree clean  
 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git push -u origin testbranch  
 Username for 'https://github.com': ajanthaneng@gmail.com  
 Password for 'https://ajanthaneng@gmail.com@github.com':   
 Total 0 (delta 0), reused 0 (delta 0)  
 To https://github.com/ajanthanerepo/test-repo-ajan1  
  * [new branch]   testbranch -> testbranch  
 Branch 'testbranch' set up to track remote branch 'testbranch' from 'origin'.  
 ajanthan@ajanthan-Lenovo:~/testgitrepo$   


2) Cloning a remote repository and committing to that repository.

This is a most common scenario, where we clone a repository and do modifications and then commit those changes.

As the previous is a public repository, it is possible to directly clone the repo as below:
 ajanthan@ajanthan-Lenovo:~/testrepo4$ git clone https://github.com/ajanthanerepo/test-repo-ajan1.git  
 Cloning into 'test-repo-ajan1'...  
 remote: Counting objects: 4, done.  
 remote: Compressing objects: 100% (2/2), done.  
 remote: Total 4 (delta 0), reused 4 (delta 0), pack-reused 0  
 Unpacking objects: 100% (4/4), done.  
 ajanthan@ajanthan-Lenovo:~/testrepo4$ ls  
 test-repo-ajan1  
 ajanthan@ajanthan-Lenovo:~/testrepo4$   

Then I'm modifying the test.json file after that getting the diff.
 ajanthan@ajanthan-Lenovo:~/testrepo4/test-repo-ajan1$ vi test.json   
 ajanthan@ajanthan-Lenovo:~/testrepo4/test-repo-ajan1$   
 ajanthan@ajanthan-Lenovo:~/testrepo4/test-repo-ajan1$   
 ajanthan@ajanthan-Lenovo:~/testrepo4/test-repo-ajan1$   
 ajanthan@ajanthan-Lenovo:~/testrepo4/test-repo-ajan1$ git diff test.json  
 diff --git a/test.json b/test.json  
 index e20351f..4adce1d 100644  
 --- a/test.json  
 +++ b/test.json  
 @@ -1,3 +1,3 @@  
  {  
 -"ajanthan":"test"  
 +"ajanthanee":"test"  
  }  
 ajanthan@ajanthan-Lenovo:~/testrepo4/test-repo-ajan1$   

Now going to commit the changes, before that check the URLs.
 ajanthan@ajanthan-Lenovo:~/testrepo4/test-repo-ajan1$ git remote -v  
 origin     https://github.com/ajanthanerepo/test-repo-ajan1.git (fetch)  
 origin     https://github.com/ajanthanerepo/test-repo-ajan1.git (push)  

 ajanthan@ajanthan-Lenovo:~/testrepo4/test-repo-ajan1$ git add test.json   
 ajanthan@ajanthan-Lenovo:~/testrepo4/test-repo-ajan1$ git commit -m "added by ajanth"  
 [master ee7a1c2] added by ajanth  
  1 file changed, 1 insertion(+), 1 deletion(-)  
 ajanthan@ajanthan-Lenovo:~/testrepo4/test-repo-ajan1$   

Now do the push.
 ajanthan@ajanthan-Lenovo:~/testrepo4/test-repo-ajan1$ git push  
 Username for 'https://github.com': ajanthan005@yahoo.com  
 Password for 'https://ajanthan005@yahoo.com@github.com':   
 Counting objects: 3, done.  
 Delta compression using up to 4 threads.  
 Compressing objects: 100% (2/2), done.  
 Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done.  
 Total 3 (delta 0), reused 0 (delta 0)  
 To https://github.com/ajanthanerepo/test-repo-ajan1.git  
   2d6bc85..ee7a1c2 master -> master  
 ajanthan@ajanthan-Lenovo:~/testrepo4/test-repo-ajan1$   

Consider that the ajanthanerepo2 has updated the file test.json. Due to that we need to update the local repository of ajanthanerepo. To do that we need to pull as below:
 ajanthan@ajanthan-Lenovo:~/testgitrepo$ git pull origin master  
 remote: Counting objects: 6, done.  
 remote: Compressing objects: 100% (4/4), done.  
 remote: Total 6 (delta 0), reused 3 (delta 0), pack-reused 0  
 Unpacking objects: 100% (6/6), done.  
 From https://github.com/ajanthanerepo/test-repo-ajan1  
  * branch      master   -> FETCH_HEAD  
   2d6bc85..c8cc9ef master   -> origin/master  
 Updating 2d6bc85..c8cc9ef  
 Fast-forward  
  test.json | 2 +-  
  1 file changed, 1 insertion(+), 1 deletion(-)  

To confirm we can do a cat and see the file content.
 ajanthan@ajanthan-Lenovo:~/testgitrepo$ cat test.json   
 {  
 "ajanthanee":"test-edited by repo2"  
 }  
 ajanthan@ajanthan-Lenovo:~/testgitrepo$   

Hope this helps someone who starts with GIT...