Tutorial 4: Initialize GIT

What you will Learn:

 

Initialize GIT repository on local machine

To convert our working directory into a GIT repository, we have to initialize our working directory (viz gitrepo). We use ‘git init .’ command to do that

Initialize GIT

The output of above command shows that our local Git repository has been initialized.

Also notice (master) written right after ~/Desktop/gitrepo (master). The (master) tells us that we are inside the Git repository.

Remember that right now we are inside our local working dir and it is empty

Initialize GIT

So, we can now execute git commands in our local machine working directory (git add, commit etc…)

Initialize GIT

Please note: You don’t require ‘git init’ command if you want to work on an existing Github project. This command is only for new projects.

Next, see below. When we execute the ‘ls’ command, we don’t see any output. However when we execute ‘ls -al’, we do see the hidden .git directory that was created due to git initialization

Initialize GIT

Let us remove the .git folder by executing ‘rm -rf’ command. Notice that, after removing the .git folder, we no longer see (master). This means that we are no longer inside the Git repository

Initialize GIT

The below command does not show the .git folder anymore

Initialize GIT

Let us now try to run a git command. You will see a fatal error that says ‘Not a git repository’

Initialize GIT

Let us again execute ‘git init .’ command. The dot means current directory. Notice that we again see (master) and hence we are again inside a Git repository

Initialize GIT

Let us execute ‘git add’ command. See the output message now. We don’t get fatal error. Instead it says that nothing has been added. This is true because we did not mention any file or directory after ‘add’

Initialize GIT

This is how we initialize a Git repository.

Create files inside local working directory

Right now, our local gitrepo/ folder is empty

Initialize GIT

Let us create few files inside the gitrepo/ working directory. We will use the ‘touch’ command to create new files, see below

Initialize GIT

So we now have 3 files inside our local working directory

Initialize GIT

You can double check these files manually as well. Go to the above location C:\Users\DELL\Desktop\gitrepo and click ‘View’ from the top menu & select checkbox ‘Hidden items’

Initialize GIT

You would see the .git folder plus files you have created

Initialize GIT

Git status

Let us execute ‘git status’ command. This command gives us the status of changes that we made in our working directory and staging area. You can see below that we have 3 untracked files and also a message that says ‘nothing added to commit……..’

Initialize GIT

The message also says to use ‘git add’ to add the files to staging area.

Add files to Git staging (git add)

Let us now add a file to Git staging area

Also Read: Tutorial 3: Create Git Repository

Initialize GIT

Let us again execute the status command. Notice below that we now have 1 new file in the staging area and 2 untracked files

Initialize GIT

The output that you see above mentions the command to unstage the file. So if you want to remove demo.html from the staging area, you can simply execute below command

Initialize GIT

Now if you run the status command, you will get the below expected output

Initialize GIT

Let us now add all the files to staging area. We can use git add . command to do that.

Note 1: ‘git add’ adds from the current directory downwards

Note 2: dot means add all files from the current directory

We will understand about ‘Note 1’ a little later.

Initialize GIT

Let us execute status command. The current directory had 3 files and all of them have been added

Initialize GIT

So we have moved all the files to staging area

Initialize GIT

Let us once again remove all the files from the staging area (in the below command, -r is for recursive removal)

Initialize GIT

Let us now create a testdir and cd to it. We than create a test.html inside this testdir

Initialize GIT

So you can see the test.html file

Initialize GIT

Now when you run the status command, you will notice 4 untracked files as expected. The double dots .. represent one folder up and single dot . represents the file inside current testdir

Initialize GIT

Now execute git add . command

Initialize GIT

Now when you run the status command, notice that only test.html got added. Why is that so?

Initialize GIT

The answer lies in the fact that ‘git add’ adds from the current directory downwards. Since our current directory is ‘testdir’, we have only file when we go downward from this directory.

If you want to add every single file to staging area, we use the command git add -A

Initialize GIT

Now if you see the status, all the files are added to staging

Initialize GIT

As a best practice, we should use git add .
That way, we would know the files we are about to add to the staging area.

Thank you for reading!

Also Check: Selenium Online Training

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 10000 RS/149 USD
Attend All Live courses in just 10000 rs / $149 - offer ends 25th April 2024