Git & GitHub Tutorial

Rahul's Profile

Git Basics

Getting Started with Git

Now that Git is installed and configured with your identity, you're ready to begin using it

Setting Up Your First Repository

Follow these essential steps to get started:

  • Create a new project directory
  • Navigate to the directory
  • Initialize a Git repository

Creating a Git Project Folder

Begin by setting up a dedicated folder for your project. This will serve as the foundation for organizing and tracking your files efficiently.

Navigating to the Project Folder and Initializing Git

Once you've created your project folder, the next step is to navigate to it using the command line and initialize a Git repository.

Change Directory (cd) to Project Folder

Use the following command to move into your project folder:

cd path/to/your/project-folder

Replace path/to/your/project-folder with the actual location of your folder.

Initialize a Git Repository

Once inside your project directory, initialize Git using:

git init

This command creates a hidden .git folder, where Git stores version history and configurations.

git init check - git bash windows

Verifying the Initialization

To confirm that Git has been set up correctly, use:

git status

If Git is initialized, this command will display the repository status.

git status before adding any file - git bash windows


Getting Started with Version Control

Add a new file

Now we will start using git for file management. Head over to the folder location where you have initialized the git. If you are in Linux, use nedit or gedit to create a new file. Or manually create a new file in that folder location. Use 'editor' of your choice to create some dummy content in the added file. I have created following content for my dummy file and named it as 'index.html'


            <!DOCTYPE html>
            <html>
            <head>
              <title>My First Git Work</title>
            </head>
            <body>
              <h1>First Git Work</h1>
              <p>Welcome to my prject in my new Git Repo.</p>
            </body>
            </html>

          

Use 'ls' command in your 'command prompt' to see if the newly added file is present in this folder location. 'ls' lists all files in the current folder.
You can also go to that location directly through file browser and confirm the presence of the newly added file.


              ls
              index.html
            
Now use 'git status' command to check the status of your newly introduced file in the current git folder (since this folder is git initialized using 'git init').

git status after adding any file - git bash windows

Untracked File

You see the message above about 'untracked file'. It is saying that 'index.html' is 'untracked'. This simply means there is a new file in the 'git folder' but 'git' is not told to keep track of this file. So, there will be no record keeping for this file. If you want to track this file, which is really important for your version control requirements, you need to add this file to 'staging area'.

Git Add (Staging Area)

To keep track of your files, you need to 'add' your file to staging area using 'git add' command.


            git add index.html
          

You can also add everything which is untracked or changed using 'git add --all'


            git add --all
          

If you check 'git status' now. You will see something like this. Now your file is no more untracked although it is not 'commited' yet. This means you are in a state where you have staged your file to commit or add to your repository but have not added it yet to the repsitory.

git status after git add - git bash windows

At this point, you can either commit this file or also unstage it. To unstage means bring it out of the staging area where your file will no longer be tracked. Use following command if you want to unstage it.


            git restore --staged index.html
          

Git Commit

A commit acts as a checkpoint in your project, capturing the current state of your files along with a descriptive message explaining the modifications. If needed, you can always revert to a previous commit.


                    git commit -m "message" → Saves staged changes with a commit message.
                    git commit -a -m "message" → Commits all tracked modifications, bypassing staging.
                    git log → Displays the history of commits.

          
git commit - git bash windows