GIT INIT

Hemashree S

Git Init

  • The 'git init ' command is the first step in starting a Git project. It creates a new, empty repository and converts an existing project into a Git-managed project. While most Git commands are run within the repository, the ' git init ' command can be executed outside of it. 
  • The 'git init' command generates a '.git' subdirectory in the current working directory, which holds all the essential metadata. This metadata is organized into objects, refs, and temporary files. Additionally, it sets up a HEAD pointer for the repository's master branch.

Creating the first Repository

  • Git, a version control system, allows developers to share projects seamlessly. When learning Git, it's essential to understand how to create a project within it. A repository is a directory that holds all the project-related data, and a single repository can contain multiple projects.
  • We can create a repository for both new and existing projects. Let's explore the process of creating a repository.

Creating a Repository for a Blank (NEW) Project

To create an empty repository, open the command line in your desired directory and run the `init` command as shown below:
$ git init
The command above will create an empty '.git' repository. For example, if we want to create a Git repository on our desktop:
To achieve this, open Git Bash on the desktop and execute the command. Refer to the output shown below:

Git Init

The command above will initialize a '.git' repository on the desktop. Now, we can create and add files to this repository for version control.
To create a file, use the 'cat' or 'touch' command as shown below:
$ touch <file Name>
To add files to the repository, use the `git add` command as shown below:
$ git add <file name>
For more information about the `git add` command, visit Git Add.

Create a Repository for an existing project

To share your project on a version control system and manage it with Git, navigate to your project's directory and open the Git command line (or Git Bash for Windows). Then, run the following command to initialize a new repository:
Syntax:
$ git init
Output:

Git Init

The command above will create a new subdirectory called '.git', which contains all the essential repository files. This '.git' folder serves as the framework for the Git repository. Refer to the image below:

Git Init

An empty '.git' repository has been added to my existing project. To begin version control for the existing files, we need to track them using the `git add` command, followed by a commit.
We can use the `git status` command to list all untracked files.
$ git status
Refer to the output below:

Git Init

The output above shows a list of all untracked files generated by the `git status` command. For more information on the status command, visit Git Status.
We can track all untracked files using the `git add` command.

Create a Repository and Directory Together

The 'git init' command lets us create a new empty repository along with a directory. The '.git' folder is created within that directory. If you want to create a blank repository with a specific project name, you can do so using the 'git init' command. See the command below:
$ git init NewDirectory
The command above will create an empty '.git' repository within a directory named 'NewDirectory'. Refer to the output below:

Git Init
In the output above, both the directory and the repository are created.
Therefore, we can create a repository using the 'git init; command. Two additional commands that are essential to get started with Git are 'git add' and 'git commit '.
To explore various operations on the repository, refer to Git Repository.



Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send