The Very Basics: git init & git status

The Very Basics: git init & git status

Β·

4 min read

Overview:

In this blog, I've shared the use of some Terminal Commands like - mkdir, cd, ls, ls -a, two Git Commands - git status & git init(This command being one of the most important) & information about the Hidden folder - .git.

What is a Repository?

A Git repository (also known as a "Repo") is a workspace that tracks and manages files within a folder.

Anytime we want to use Git with a project - be it a web dev project, an app dev project etc we need to create a new git repository. We can have as many repos on our machine all with its own separate histories & contents (ie.. any kind of changes and commits)

Let's assume there are different folders like project 1, project 2 etc. Each one of these is a separate project. If we want to use Git, we have to go into each project folder & create a Git repository.

Let's see how to initialize/create a Git repository with Git CLI, shall we?

Initializing a Git Repo!

  • Prerequisite: Git must be installed on your machine. (if you are using windows otherwise, you can use the default terminal on Linux and macOS)

    Git Bash is an application for Microsoft Windows environments that provides an emulation layer for a Git command-line experience. Bash is an acronym for Bourne Again Shell. A shell is a terminal application used to interface with an operating system through written commands. Bash is a popular default shell on Linux and macOS.

STEP-1: We need to first create a folder/directory on our local machine which will become the home of a new git repository. I'll make a folder on my Desktop, to do it - Right click on the desktop window and open your Git Bash. image.png A terminal similar to πŸ‘† must open up on your desktop.

STEP-2: Use the command mkdir- also called as make directory to create the project folder. image.png

  • Here,mkdir command makes the directory/folder named - gitdemo. You'll see the folder created on your desktop, the second you press - enter.
  • Since, our working directory (ie our current location) is still Desktop; here, the command cd (change directory) is used to change our path to the folder - gitdemo.
  • The command ls shows List contents of current directory which in this case is empty since the gitdemo folder is also empty.

Our First Git command - git status

image.png

git status - gives information on the current status of a git repository and its contents.

Since we have not initialized a git repository in the gitdemo folder, git recognises it & show us the above error.

STEP-3: Here comes the REAL DEAL! image.png

  • Inside the gitdemo directory i made another directory named ProjectOne via command mkdir where I'll actually initialize my repo -> used cd to change directory path -> git status shows - not a git repo yet!

Our Second git command - git init

Use git init to create a new git repository. Before we do anything git-related we must initialise a repo first. which totally makes sense.

image.png Here, the message says we have initialized an empty Git repo in our folder ProjectOne

Please note that - git init is something we do only ONE TIME per PROJECT/REPO.
You do not want to create a repo inside a repo.

image.png

Now, when we do git status it tells us that -

  • We are currently at the master branch
  • We haven't made any commits
  • Since, our folder is empty, it's telling us to add files so that we can commit them.

    By default, GitHub uses the term 'master' for the primary version of a source code repository. But, from Oct. 1, 2020 the company said, "Any new repositories you create will use 'main' as the default branch, instead of master."

The .git folder

So, we just initialized our first git repo but what actually happened? where is the git? These might be the questions you must be thinking about.

When we look at the official git docs it says - The git init command creates an empty Git repository - basically a .git directory with subdirectories for objects, refs/heads, refs/tags, and template files. An initial branch without any commits will be created. image.png

.git is basically a hidden folder -

  • We couldn't see it when we run the ls command.
  • We could see it when we run the ls -a command which helps us to see the hidden files/folders.
  • We got into the .git folder via cd command -> ls command then showed us it's contents.

IF WE DELETE .git FOLDER OUR GIT HISTORY IS GONE! Hence, it's hidden.

Congratulations! you've just learned how to initialize a repo which is one of the most important steps while learning git. πŸŽ‰

In my next blog, I'll be sharing how to actually ADD & COMMIT in a repo. Do share this blog with someone who you think will get benefitted from it.
Share your questions and feedback in the comment section as well πŸ˜ŠπŸ™Œ

Β