Uncategorized

Git – a practical guide for beginners

Git is an open-source and free version control system,  created by Linus Torvalds in 2005. Unlike SVN and CVS (older centralized version control systems), Git is distributed, which means every developer has the full history of their code repository locally. This slows down the initial cloning of the repository, but makes subsequent operations such as commit, blame, diff, merge and log dramatically faster.

Git also has excellent support for rewriting repository history, branching, and merging which has lead to many innovative and powerful workflows and tools. One such popular tool that allows teams to collaborate on Git branches and efficiently review each other’s code is Pull request. Git is considered as the modern standard for software development and it is the most widely used version control system in the world today.

Basic overview of how Git works:

  1. Create a “repository” (project) in your Git account.
  2. Clone (or copy) the repository to your local machine
  3. Create a “working branch” (version).
  4. Create a file to your local repo and “commit” (save) the changes
  5. “Push” the changes you have made to your master branch
  6. “Pull” the changes to your local machine from the main (master) branch
  7. Open a “pull request” (propose changes to the master branch) and get code reviewed.
  8. IF everything seems fine “Merge” your branch to the master branch.

 

This article will give you a basic understanding of the general operations related to Git. The article will try to explain each step, starting from Creation of SSH key to Pull request with proper screenshots for the better understanding,

Before starting, there are some pre-requisite requirements:

  1. Git (You can download it from https://git-scm.com/downloads).
  2. Account on Github.

 

Generating SSH key from scratch:

  1. Go to Start->All Programs->Git->Git Bash –
  2. Once the Bash shell is opened, follow the steps below.

            a. Run the below command:

The above email address should be the one, registered on the Git account.

b. Just press <Enter> to accept the default location and file name.

c. Enter, and re-enter, a passphrase when prompted. The whole interaction will look like below:

 

3. Now go to c:\users\{username}\.ssh and open file ‘id_rsa.pub’ in notepad. Copy the whole content to Clipboard.

4. After Login into the GITHUB website, go to settings.

 

5. Click on the “SSH and GPG Keys” tab under Personal settings.

 

6. Click on “New SSH Key” to create the key.

 

7.  After clicking in “New SSH key” the following screen will open. Now paste the content you copied in step-3 in the text area given under the        “Key” label.

 

8.  Open the Git Bash and copy the name (present at the topmost line).

 

9.  Now go back to GITHUB and paste the name you copied in step-8 in the text area given under “Title” label.

10. Once copied your screen must look like this:

 

11.  Click on “Add SSH Key” and your key would get generated.

Create your working branch on repository:

  1. Go to your repository.
  2.  To create your own branch. just click on “Branch: Master”
  3. Write the name of your branch you want to create (you can see it creating).
  4. After entering name press enter or click on the blue highlighted area having branch name.

Note: If you can’t see the name you are typing, in the blue highlighted area (Create branch: <Name you are typing>), it means you are not added as a contributor. Please contact your respective repository admin to add you as a contributor.

 

 

Clone the code from repository:

5. Now you must Clone the code present in the repository into your local machine for this you have to take the following steps:

  • Click the “Clone or download” button.
  • Now click on “Use SSH” (in blue) to select “Clone with SSH”.
  • Copy the URL in the text area (Beginning with git@…).

6. Create a folder in your local machine.

7. Open the folder-> Right Click -> Git bash here.

8. Clone your code into your directory using the command:

 git clone <git@urbranch url>

(Here <git@urbranch url> is the same URL you copied in step 5).

9. After step 8, all the code in the repository will copy on your local machine.

 

Basic Git Operations:

Under this section, we will learn how to switch the branch,  how to commit your local changes, and push them into the repository.

Switch the branch:

After cloning, you will see your branch code in your drive (in your project folder).

  1. Do ‘cd test’ in command prompt to get into your code directory (inside test folder).

 

 

2. Presently you would be on Master Branch, but you need to do all modifications on your working branch only. So, switch from master branch to      your working branch by using the command.

3. Check the branch name appearing in brackets, it should change from “master” to your branch name

Note: Master Branch is the main branch, different contributors should work on their own working branch and then create a Pull Request to merge their code on to the master.

When you checkout to some branch, your code will get replaced by the code present in the working folder. So, it is always a good practice to first take a backup and then pull the latest code and merge it with your code.

 

Commit local changes into the repository:

When you have made the necessary changes in the code, you need to push the code to your branch.

Execute the following commands:

  1. git status (will show you all the changes you have made along with respective file name.)
  2. git add . (will add all the newly created files)
  3. git commit –m ”any logical message” (commit all the changes made)
  4. git push (will push your changes to your branch)
  5. git status

 

 

Check the message after 5th command, it should show “Your branch is up-to-date with origin/<your working branch name>”, which means your changes are pushed successfully to your working branch on the remote repository.

 

Pull Request:

After updating your code to your branch, you need to create a pull request for the same so that it can be merged to the master branch.

  1. Take the following steps to create a pull request:
  •  Click on the “New pull request” button, when you are in your branch.

 

  • After clicking on New pull request, you will see a window.
  • Add understandable comments related to your changes in the “leave a comment” section.
  • Now choose a reviewer to review your code (All the contributors are visible in reviewer list)
  • After choosing the reviewer hit the “Pull request button” to create the pull request from the master branch.

 

2. After clicking on “Create pull request” the below screen will appear where you can see your pull request along with the reviewer’s name.

3. Once your changes are reviewed by the reviewer, he/she can merge your code to the master branch after removing conflicts (if any).

About The Author