Bird
Raised Fist0
Gitdevops~5 mins

Creating a repository with git init - Step-by-Step CLI Walkthrough

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
When you want to start tracking changes in your project files, you need a place to store that history. Git helps by creating a repository, which is like a special folder that remembers all your changes.
When you start a new project and want to keep track of changes over time.
When you want to save versions of your work to avoid losing progress.
When you want to share your project with others using version control.
When you want to organize your code and collaborate with teammates.
When you want to prepare your project to be uploaded to a remote server like GitHub.
Commands
Create a new folder named 'my-project' to hold your project files.
Terminal
mkdir my-project
Expected OutputExpected
No output (command runs silently)
Change directory into the new project folder to work inside it.
Terminal
cd my-project
Expected OutputExpected
No output (command runs silently)
Initialize a new Git repository in the current folder. This creates a hidden folder where Git stores all version history.
Terminal
git init
Expected OutputExpected
Initialized empty Git repository in /home/user/my-project/.git/
List all files including hidden ones to confirm the '.git' folder was created.
Terminal
ls -a
Expected OutputExpected
. .. .git
-a - Show all files including hidden ones
Key Concept

If you remember nothing else from this pattern, remember: git init creates a hidden folder that starts tracking your project changes.

Common Mistakes
Running git init outside the project folder
Git will create the repository in the wrong place, causing confusion and misplaced files.
Always navigate into your project folder before running git init.
Not checking for the .git folder after init
You might think the repository was not created and run commands that fail.
Use ls -a to confirm the .git folder exists after git init.
Summary
Create a project folder and move into it.
Run git init to start a new Git repository.
Verify the repository by checking for the hidden .git folder.

Practice

(1/5)
1. What does the command git init do in a folder?
easy
A. It creates a new Git repository by adding a hidden .git folder.
B. It deletes all files in the folder.
C. It uploads the folder to a remote server.
D. It merges two branches automatically.

Solution

  1. Step 1: Understand the purpose of git init

    The command git init starts a new Git repository in the current folder by creating a hidden .git directory.
  2. Step 2: Identify what git init does not do

    It does not delete files, upload data, or merge branches automatically.
  3. Final Answer:

    It creates a new Git repository by adding a hidden .git folder. -> Option A
  4. Quick Check:

    git init creates .git folder = A [OK]
Hint: Remember: git init creates .git folder to start tracking [OK]
Common Mistakes:
  • Thinking git init deletes files
  • Confusing git init with git clone
  • Assuming git init uploads files
2. Which of the following is the correct syntax to initialize a Git repository in the current directory?
easy
A. git init
B. git start
C. git create
D. git new

Solution

  1. Step 1: Recall the Git command to start a repository

    The correct command to initialize a Git repository is git init.
  2. Step 2: Verify other options

    Commands like git start, git create, and git new do not exist in Git.
  3. Final Answer:

    git init -> Option A
  4. Quick Check:

    Initialize repo command = git init = A [OK]
Hint: Use 'git init' exactly to start a repo [OK]
Common Mistakes:
  • Typing git start instead of git init
  • Using git create or git new which are invalid
  • Adding extra words after git init
3. After running git init in an empty folder, what will be the output of ls -a?
medium
A. . .. .gitignore
B. . ..
C. . .. .git .gitignore
D. . .. .git

Solution

  1. Step 1: Understand what git init creates

    Running git init creates a hidden .git folder inside the current directory.
  2. Step 2: List all files including hidden ones

    The command ls -a shows all files including hidden ones like ., .., and .git. Since the folder is empty except for .git, only these appear.
  3. Final Answer:

    . .. .git -> Option D
  4. Quick Check:

    git init creates .git folder only = B [OK]
Hint: ls -a shows .git folder after git init [OK]
Common Mistakes:
  • Expecting .gitignore to appear automatically
  • Not including hidden files in listing
  • Confusing .git with other files
4. You ran git init but the .git folder is missing. What is the most likely reason?
medium
A. You ran git init but your folder is already a Git repository.
B. You forgot to install Git on your system.
C. You ran git init inside a folder without write permission.
D. You ran git init with a typo like git initt.

Solution

  1. Step 1: Check permissions for creating .git folder

    If the folder does not allow writing, git init cannot create the hidden .git directory.
  2. Step 2: Evaluate other options

    If Git was not installed, the command would fail with an error. A typo would cause a command not found error. If the folder was already a Git repo, .git would exist.
  3. Final Answer:

    You ran git init inside a folder without write permission. -> Option C
  4. Quick Check:

    Missing .git usually means no write permission = D [OK]
Hint: Check folder write permission if .git missing after init [OK]
Common Mistakes:
  • Assuming Git is not installed without checking error
  • Ignoring typos in command
  • Thinking .git disappears if repo exists
5. You want to start tracking an existing project folder with Git. Which sequence of commands correctly initializes the repository and adds all files for the first commit?
hard
A. git init; git commit -m "Initial commit"; git add .
B. git init; git add .; git commit -m "Initial commit"
C. git clone; git add .; git commit -m "Initial commit"
D. git add .; git init; git commit -m "Initial commit"

Solution

  1. Step 1: Initialize the Git repository

    Use git init to create the repository in the existing folder.
  2. Step 2: Add all files to staging area

    Use git add . to stage all files for commit.
  3. Step 3: Commit the staged files

    Use git commit -m "Initial commit" to save the snapshot.
  4. Final Answer:

    git init; git add .; git commit -m "Initial commit" -> Option B
  5. Quick Check:

    Init, add, then commit = C [OK]
Hint: Always init first, then add files, then commit [OK]
Common Mistakes:
  • Running git commit before git add
  • Using git clone instead of git init for existing folder
  • Adding files before initializing repo