Monday, 14 August 2017

How to install GIT in Ubuntu server? How to add/clone the local repository to/from the GitHub?

In this post, i would like to present you some interesting things of GIT.

GIT - is a version control system and which is been used in many projects in the recent days.

Step 1:  Install GIT in Ubuntu

sudo apt-get install git
Installation Log:

How to add the local repository to the GitHub?

Step 2: Configure the GitHub 

Once the git is successfully installed in the Linux machine. Then it is required to configure the GitHub by using the user name and email Id as shown below.

git config --global user.name  "provide_your_user_name"
git config --global user.email "provide_your_email_id"

Step 3: Create a Local Repository (or directory) 

Create a local directory in your system and later this repository can be pushed in to the GIT HUB website.
git init your_preferred_directory_name

If the command is successful, it will show message as Initialized empty Git repository in  /root/Localproject/.git/

Step 4: Change directory to locally created
cd Localproject
Step 5: Create your files for Local repository 

Create two sample files in the local repository.
>  File 1:  Readme   -> Write description about your project.  
               e.g: "Set up the GIT test repository"
>  File 2:  testfile.py -> Any source code file.

#!/usr/bin/python

def test():
  print "Hello world"

test()

Step 6: Add files to the Local repository 

After creating the sample files, then add these files to the local repository index.
git add <your_files>
Here git add will place the files in the buffer like space, later it will be added to the git repository.

Step 7: Commit the changes to Local repository 

Once the files are added and finalized, then commit or uploaded the  files to the local repository 
git commit -m "your commit message" 

Check the log of the file using the below command.

git log "your_file_name" 
root@muni:~/Localproject# git log testfile.py
commit 0db1bb0ea2a595ec46a23f7956786de04c075ea4
Author: amsekhar <ams@gmail.com>
Date:   Fri Aug 11 14:42:18 2017 -0500

    first commit the files

Step 8: Create user Account in  GitHub website and Create Repository 

1. Login to https://github.com/ 
2. Create your user Account using "SIGN UP" option in the website.
3. Create repository on GitHub as shown below. Note that the name of the GitHub repository (Localproject) should be same as the local repository name (Localproject).  

Click on right hand side of create repository 

Step 9: Connect to the GitHub Repository

Once the repository is created, then push the changes of local repository to the GitHub website.

git remote add origin https://github.com/your_user_name/your_project_name.git
e.g:  git remote add origin https://github.com/amsekhar/Localproject.git
Step 10: Push the local files to the remote GitHub Repositry

Push the local files present in local server to the remote GitHub repository

git push origin master
It will prompt you to enter the credentials of GitHub account.  Username : xxxx and  Password:xxxx



How to clone the repository from the GitHub to local server?
To get an copy of an existing Git repository from GitHub or from any GIT server use the "git clone" command.

  • We shall receives a full copy of all data that the GitHub or server has. 
  • Every version of every file for the history of the project is pulled down by default when you run git clone.
Steps: Clone the GitHub Repository using "git clone command"
  •  Login in to the GitHub website with your credentials
  •  Navigate to the main page of repository.
  •  Under your repository name, click Clone or download.   Copy the link to clipboard.
  •  Use one of the below commands 
git clone <https://github.com/your_git_user_name/your_project_name.git>  
git clone <https://github.com/user_name/project_name.git>  <your_desired_name>
e.g: git clone https://github.com/rishi417/testproject.git
e.g: git clone https://github.com/rishi417/testproject.git  Mytestproject



How to create new Branch in GitHub?
1. Login to https://github.com/ 
2. Create your user Account using "SIGN UP" option in the website.
3. Create branch on GitHub as shown below.


How to checkout the feature Branch from Remote/GitHub?

Step 1: Clone the GitHub Repository using "git clone command"
  •  Login in to the GitHub website with your credentials
  •  Navigate to the main page of repository.
  •  Under your repository name, click Clone or download.   Copy the link to clipboard.
  •  Use one of the below commands

git clone <https://github.com/your_git_user_name/your_project_name.git>  
e.g: git clone https://github.com/rishi417/testproject.git
Step 2: Change directory to the Local Repository (Here, testproject)

cd testproject
Step 3: Then checkout the respective branch from GitHub 

git checkout -b <Local_repository_branch_name> origin/<gitHub-Branch-name>
e.g: git checkout -b my_local_branch origin/new_feature_branch_001
verify using: git status
How to add a new file to GitHub branch from the local repository?

Step 1: Be in the local Repository
             create new file and add using git add, git commit -m "new file added"

cd testproject; vim new_file.py; git add new_file.py;  git commit -m "new file added"
Step 2: Push the new file in to the GitHub Feature Branch

git push origin <local_branch_name> : <remote_branch_name>
e.g: git push origin my_local_branch : new_feature_branch_001

Step 3: Verify in the GitHub repository for the new file.

How to remove file from local branch and also from the feature Branch located in GitHub?

Step 1: Be in the local Repository
             Remove using git rm <file_name>

git rm "file_name"        e.g: git rm new_file_rishi.py
Step 2: Commit the changes
             
git commit -m "new file removed"        
Step 3: Push the changes in to the GitHub Feature Branch

git push origin <local_branch_name> : <remote_branch_name>
e.g: git push origin my_local_branch : new_feature_branch_001

Step 3: Verify in the GitHub repository for removed file.

How to merge the feature branch changes in to master branch?

You can do either using "git fetch"  and  "git merge" commands (or).
Git fetch - Download objects and refs from another repository

Use "git pull command" . "git pull" => "git fetch" + "git merge"

Step1: First checkout the Master branch.
git checkout master
Step 2: Merge the changes in to master branch using git pull command.
git pull origin <feature_branch_name>
git pull origin new_feature_branch_001
Step 3: Verify in the GitHub Master branch for the changes.


How to switch between the branches?
git checkout <branch_name>
e.g: git checkout master   >-- git checkout new_feature_branch_001
How to check which branch your repository is pointing?
git branch  or git status
How to update the local repository with the changes from GitHub repository?


Step1: First check which branch your local repository is working on.
            The below command will show the branch you are working on.
git branch
Step 2: Update the local repository using git pull command.
git pull origin <branch_name>
e.g: git pull origin master

How to find out who has commit the file?
git log <file_name>
 There is another interesting command "git blame" which gives information about each line of a file (or)
e.g: git blame <file_name>
 Annotates each line in the given file with information from the revision which last modified the line.
useful to blame your colleague that you haven't the wrong fix.. :) .. 





Useful commands of GIT 
  • git checkout
  • git status
  • git pull
  • git fetch
  • git rebase
  • git blame
  • git diff
  • git stash
  • git clone
  • git push
  • git rm

Git GUI Tools
In Linux , one can manage the GIT commands from the command line.
If you don't feel comfortable with command line, then there are several graphical user interface (GUI) tools are available.

  •  GitKraken
  •  Git-cola
  •  SmartGit
  •  Giggle
  •  Gitg
  •  Git GUI
  •  Qgit

Please check out this link for more info.
      https://www.tecmint.com/best-gui-git-clients-git-repository-viewers-for-linux/