Initialising a Git Repository

How to initialise a git repository


Say you're working on a new project and you decide to setup version control in order to map out the history of the project, collaborate and better manage your development workflow over time. Here's a sample pattern to approach this usecase:

Step 1: Creating a New Directory

Before initialising a Git repository, you need to create a new directory to hold your project files. You can do this using the mkdir command in your terminal or command prompt:

mkdir my-project

Step 2: Navigating into the Project Directory

Navigate into the newly created directory using the cd command:

git init

After running this command, Git will create a hidden .git directory inside your project directory. This directory contains all the necessary metadata and objects to manage your repository.

Step 4: Verifying the Initialisation

To verify that the repository was successfully initialised, you can use the ls command with the -a flag to show hidden files:

ls -a

You should see the .git directory listed among the files and directories.

Step 5: Staging and Committing Files

Once the repository is initialised, you can start tracking changes to your project files. Let's assume you have some existing files in your project directory. To begin tracking them, you need to add them to the staging area using the git add command:

git add file1.txt file2.txt

This command stages file1.txt and file2.txt for the next commit. You can replace these filenames with the actual files in your project.

Step 6: Creating the Initial Commit

After adding the files to the staging area, you can create the initial commit using the git commit command. This command permanently saves the changes you have staged:

git commit -m "Initial commit"

The -m flag is used to provide a commit message describing the changes made in the commit. You can customise the commit message according to your project's needs.

Congratulations! You have successfully initialised a Git repository and created the initial commit for your project.

Last updated