Basic Git Commands

Piyumi M Dasanayaka
LinkIT
Published in
3 min readOct 19, 2020
Photo by Vanna Phon on Unsplash

In this article, I just explain very basic Git commands. I am sure most people already know about this. If you already know about these, better not read further. This article is probably not for you! :)

Okay…

Let’s start. Here I will explain basic git commands and how to check out a file from an earlier commit. And also how to reset the Git repository.

Steps:

  • At a convenient location on your computer, create a folder.
  • Open this created folder in your favorite editor.
  • Add a file named index.html to this folder, and add the following HTML code to this file:
<!DOCTYPE html><html><head></head><body><h1>This is a Header</h1></body></html>

Initializing the folder as a Git repository

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

Checking your Git repository status

  • Type the following at the prompt to check your Git repository’s status:
git status

Adding files to the staging area

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

If you want to add an individual file you, type:

git add <file-name>

Committing to the Git repository

  • To commit the current staging area to your Git repository, type:
git commit -m “first commit”

Checking the log of Git commits

  • To check the log of the commits to your Git repository, type
git log --oneline
  • Now, modify the index.html file as follows:
<!DOCTYPE html><html><head></head><body><h1>This is a Header</h1><p>This is a paragraph</p></body></html>
  • Add a sub-folder named template to your folder, and then add a file named index2.html to the templates folder. Then set the contents of this file to be the same as the index.html file above. This will use to compare with the restored file to verify whether it is restored or not.
  • Then check the status and add all the files to the staging area.
  • Then do the second commit to your repository
  • Now, modify the index.html file as follows:
<!DOCTYPE html><html><head></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 the third commit.

Checking out a file from an earlier commit

  • To check out the index.html from the second commit, find the number of the second commit using the git log, and then type the following at the prompt:
git checkout <second commit’s number> index.html

Resetting the Git repository

  • To discard the effect of the previous operation and restore index.html to its state at the end of the third commit, type:

git reset HEAD index.html

  • Then type the following at the prompt:
git checkout — index.html

Now you can see index.html file has restored as according to its state at the end of the third commit.

  • You can also use git reset to reset the staging area to the last commit without disturbing the working directory.

In this part, you will learn about how to set up and use an online Git repository and synchronize your local Git repository with your online repository. At the end of this part, you will be able to:

  • Set up the online repository as a remote repository for your local Git repository
  • Push your commits to the online repository
  • Clone an online Git repository to your computer

Setting up an Online Git repository

  • Sign up for an account either at Bitbucket (https://bitbucket.org) or GitHub (https://github.com).
  • Then set up an online Git repository named as folder name which we created earlier. Note the URL of your online Git repository.

Set the local Git repository to set its remote origin

  • At the prompt, type the following to set up your local repository to link to your online Git repository:
git remote add origin <repository URL>

Pushing your commits to the online repository

  • At the prompt, type the following to push the commits to the online repository:
git push -u origin master

Cloning an online repository

  • To clone an online repository to your computer, type the following at the prompt:
git clone <repository URL>

At the end of this article, you should have learned some basic Git commands.

Cheers…

Reference

--

--