CS Electrical And Electronics
@cselectricalandelectronics
All PostsProgramming

Setting Up Git And Commands To Checkpoint Your Code To Save Time

Hello guys, welcome back to my blog. In this article, I will discuss setting up git, basic commands, commands to checkpoint your code to save time, I will share some example like adding code and changing it.

If you need an article on some other topics then click on ask questions and add your question. You can also catch me @ Instagram – Chetan Shidling.

Also, read:

  1. MATLAB Programming Tutorials For Beginners With Examples.
  2. File Operation In C Programming With Explanation And Code.
  3. Internship For Engineers, Some Tips To Take Internship Anywhere.

Setting Up Git

Git is version control. Version control means software tools that enable the management of changes to the source code. This means it will maintain history. Suppose if you make some changes in your source code and it went wrong then you can go back to the previous source code with the help of version control tools.

In this article you will learn.

  1. Install Git on your computer
  2. Assure that Git can be handled from the command-line or command-prompt on your computer
  3. Set up some of the basic global configurations for Git

Downloading and Installing Git

  1. To install Git on your machine, go to https://git-scm.com/downloads to download the Git installer for your particular computing platform.
  2. Then, understand the installation steps as you install Git using the installer.
  3. You can find more details about installing Git at https://git-scm.com/book/en/v2/Getting-Started-Installing-Git. This document lists different methods of installing Git on different platforms.
  4. Installing any of the GUI tools like GitHub Desktop will likewise install Git on your computer.
  5. On a Mac, setting up XCode command-line tools too will set up Git on your computer.
  6. You can take any of the ways that are most convenient for you.

Some Global Configuration for Git

  1. Begin a cmd window or terminal on your computer.
  2. Check to get sure that Git is installed and available on the command line, by copying the following at the command prompt:
git --version  

To configure the user name to be employed by Git, type the below command at the prompt:

git config --global user.name "Your Name"

To configure your email to be utilized by Git, type the below command at the prompt:

git config --global user.email <your email address>

You can check your default Git global configuration, you can type the below command at the prompt:

git config --list

After this exercise you will have Git available on the command-line of your PC or laptop.

Basic Git Commands

In this article you will get familiar with some basic Git commands. At the end of this article you will be able to:

  1. Set up a folder as a Git repository
  2. Conduct basic Git operations on your Git repository

Git Commands

  • At an accessible location on your laptop, create a folder named git-test.
  • Open this git-test folder in your preferred editor like VS Code, sublime text editor.
  • Add a file named index.html to this folder, and type the HTML code that is given below to this file:
<!DOCTYPE html>
<html>
    <head>
       <title>Chetan Shidling</title>
    </head>
    <body>
        <h1>This is a Header</h1>
    </body>
</html>

Initializing the folder as a Git repository

  1. Go to the git-test folder in your cmd window/terminal and type the below command at the prompt to initialize the folder as a Git repository:
git init

Checking your Git repository status

  1. Type the below command at the prompt to check your Git repository’s status:
git status

Adding files to the staging area

  1. To add files to the staging area of your Git repository, type the below command:
git add .

Commiting to the Git repository

  1. To commit the current staging area to your Git repository, type the below command:
git commit -m "first commit"

Checking the log of Git commits

  1. To check the log of the commits to your Git repository, type the below command:
git log --oneline

Now, change the index.html file as follows:

<!DOCTYPE html>
<html>
    <head>
       <title>Chetan Shidling</title>  
    </head>
    <body>
        <h1>This is a Header</h1>
        <p>This is a paragraph</p>
    </body>
</html>
  1. Add a sub-folder named templates or any other name to your git-test folder, and then attach a file named test.html to the templates folder. Then add HTML code to this file to be the same as the index.html file above.
  2. Then check the status of current and add all the files to the staging area.
  3. Then by using the command do the second commit to your repository
  4. Now, modify the index.html file as follows:
<!DOCTYPE html>
<html>
    <head>
        <title>Chetan Shidling</title>  
    </head>
    <body>
        <h1>This is a Header</h1>
        <p>This is a paragraph</p>
        <p>This is a second paragraph</p>
    </body>
</html>

Now add the modified index.html file to the staging area and then do a third commit.

Checking out a file from an earlier commit

  1. To check out the index.html from the second commit, get the number of the second commit using the git log, and then type the below command at the prompt:
git checkout <second commit's number> index.html

Resetting the Git repository

  • To avoid the effect of the previous operation and restore index.html to its state at the end of the third commit, type the below command:
git reset HEAD index.html
  • Then type the following at the prompt:
git checkout -- index.html

You can also use git reset to reset the staging area to the last commit without disrupting the working directory. Experiment with these commands until you fully understand how to use Git.

I hope this article may help you all a lot. Thank you for reading. If you have any doubts related to this article “Setting Up Git”, then click on ask questions.

Also, read:

Author Profile

CS Electrical And ElectronicsChetu
Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking
Share Now

CS Electrical And Electronics

Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking