Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+1 vote
31 views
in Information Technology by (176k points)
Explain Git Init command

Please log in or register to answer this question.

1 Answer

+1 vote
by (176k points)

The git init command is used to initialize a new Git repository. This command sets up all the necessary files and directories that Git uses to track changes to your project files. Here's a detailed explanation along with example codes:

Usage

git init

Steps to Use git init

  1. Navigate to Your Project Directory

    • Open your terminal or command prompt.
    • Navigate to the directory where you want to initialize a Git repository.
    cd path/to/your/project 
  2. Initialize the Git Repository

    • Run the git init command.
    git init 
  3. Verify Initialization

    • Check that the .git directory has been created. This directory contains all the configuration files and repositories for Git to track changes.
    ls -a 

    You should see a directory named .git in your project directory.

Example Code

Step-by-Step Example

  1. Create a New Directory for Your Project

    mkdir my-new-project
    cd my-new-project 
  2. Initialize Git in the Directory

    git init 

    Output:

    Initialized empty Git repository in /path/to/my-new-project/.git/ 
  3. Add Some Files to Your Project

    echo "# My New Project" > README.md
    echo "print('Hello, World!')" > hello.py 
  4. Check the Status

    git status 

    Output:

    On branch master
    
    No commits yet
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
          README.md
          hello.py
    
    nothing added to commit but untracked files present (use "git add" to track) 
  5. Stage the Files for Commit

    git add README.md hello.py 
  6. Commit the Files

    git commit -m "Initial commit"
    

    Output:

    [master (root-commit) abcdef1] Initial commit
     2 files changed, 2 insertions(+)
     create mode 100644 README.md
     create mode 100644 hello.py 

Explanation

  • mkdir my-new-project && cd my-new-project: Creates a new directory named my-new-project and navigates into it.
  • git init: Initializes an empty Git repository in the current directory.
  • echo "# My New Project" > README.md and echo "print('Hello, World!')" > hello.py: Creates a README.md and hello.py file with some initial content.
  • git status: Displays the status of the repository, showing that README.md and hello.py are untracked.
  • git add README.md hello.py: Stages the README.md and hello.py files, preparing them for commit.
  • git commit -m "Initial commit": Commits the staged files to the repository with the message "Initial commit".

Related questions

+1 vote
1 answer
+1 vote
1 answer
+1 vote
1 answer
+1 vote
1 answer
+1 vote
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...