How to initialize, add and commit in git?

Git is a powerful version control system. Git allows making commands via Command Prompt or Terminal app to make changes like the addition of files, commit in the project, initialization of repository etc.

Git initialization

It is a process to start managing of initialized project or file.

git init

This is a command for initializing a Git repository to manage a Git project.

Initializing a Git repository is performed in the following steps.

  1. You will need to open a terminal or command prompt. If you have Windows, then you have to open a command prompt or Powershell.

  2. Once you have opened your command prompt or terminal, you will need to navigate to the directory where you want to create your Git repository. You can do this by using the 'cd'(change-directory) command.

    For example, we are using a folder, which is created on the Desktop named 'git-pro'.

    We make a command to change the directory from the current to that folder's directory.

    cd C:\Users\mishr\Desktop\git-pro

  3. Once the directory is changed, we make a command to go to our text editor.

    code .

    We can also write the command of initialization in the command prompt without going to VS Code terminal.

    git init

    After pressing enter, a default text editor is opened.

  4. Now we will make commands in VS Code. For exercise purposes, we will create an html file named 'index.html'.

  5. Now that you're in the project directory, you can initialize the Git repository using the git init command.

    git init

    It will create a .git folder in the current directory.

Adding the file to git

It is a process to permit git for tracking the changes in a file of files.

git add

This is a fundamental command in Git that tells Git to start tracking changes to a file or files.

Syntax:

git add <filename> //It is used to track a single specified file.

git add <file1> <file2> <file3> //It is used to track multiple files.

git add * //It is used to add all files in the current directory.

git add . //This will add files to the current directory.

In this example, we will add 'index.html' to git for tracking changes.

When a file is added to git, it becomes on-stage means every change in this file will be tracked by git.

Difference between git add . and git add *

We will know the difference with practicals in the next blog.

Staging area:

When we add files using git add, Git moves them from the working directory to the staging area.

The staging area is a temporary storage area where Git keeps track of the changes that you want to include in your next commit.

We can check the staging status of any file by giving the command git status to the terminal.

Removing changes from the staging area

You can remove any file from the staging area to the working directory.

If you added a file to the staging area by mistake, you can use the following command to remove it.

git reset <filename>

Committing a file in Git

This process is used to record changes to the repository.

git commit -m "message"

The git commit command is used to create a new commit in Git, which records changes to the repository.

Syntax:

git commit -m "message"

The -m flag indicates that the commit message will be specified in the command line.

For example, first, we added a file 'index.html' to the git by using the command git add index.html.

And then we make a change in its title.

After making changes we will commit the file by using the command git commit -m "this is commit" .

Amending a Commit:

If you need to make changes to a commit after it has been created, you can use the --amend flag with the git commit command. It allows modifying the last commit you made.

For example, if we forgot to include a file in my last commit, we can add it to the staging area and then amend the last commit with the following command.

git add <new file>

git commit --amend -m "Updated commit"

If we create a new file after the commit named 'goo.html' and then amend it to the first commit.

Did you find this article valuable?

Support WebIndia by becoming a sponsor. Any amount is appreciated!