Bird
Raised Fist0
Gitdevops~10 mins

Creating a repository with git init - Visual 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
Process Flow - Creating a repository with git init
Open terminal
Navigate to folder
Run 'git init'
Git creates .git folder
Folder becomes a git repository
Ready to track files
This flow shows how running 'git init' in a folder creates a hidden .git directory, turning it into a git repository ready to track changes.
Execution Sample
Git
mkdir myproject
cd myproject
git init
Creates a folder, enters it, and initializes a new git repository inside.
Process Table
StepCommandActionResult
1mkdir myprojectCreate folder named 'myproject'Folder 'myproject' created
2cd myprojectChange directory to 'myproject'Current directory is 'myproject'
3git initInitialize git repositoryInitialized empty Git repository in /path/to/myproject/.git/
💡 After 'git init', the folder is now a git repository with a .git directory
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3
Current DirectoryUser home or whereverUser home or wherevermyprojectmyproject
.git folderDoes not existDoes not existDoes not existExists inside 'myproject'
Key Moments - 2 Insights
Why do we not see the .git folder after running 'git init'?
The .git folder is hidden by default in most systems. You can see it by running 'ls -a' in the terminal. This is shown in step 3 where git creates the .git directory.
Does 'git init' add any files to the repository automatically?
No, 'git init' only creates the .git folder to track changes. You still need to add files manually. This is why after step 3, the repository is empty but ready.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of running 'git init'?
ACreates a new folder named .git
BDeletes all files in the folder
CInitializes an empty Git repository with a .git folder
DCommits all files automatically
💡 Hint
Check the 'Result' column in step 3 of the execution table
According to the variable tracker, when does the current directory change to 'myproject'?
AAfter step 2
BAfter step 1
CAfter step 3
DIt never changes
💡 Hint
Look at the 'Current Directory' row in the variable tracker after each step
If you run 'git init' twice in the same folder, what happens according to the flow?
AIt overwrites the existing .git folder
BIt does nothing and keeps the existing repository
CIt creates two .git folders
DIt deletes the folder
💡 Hint
Git init is safe to run multiple times; it reinitializes without overwriting the .git folder or losing data
Concept Snapshot
git init
- Run inside a folder to create a new git repository
- Creates a hidden .git directory to track changes
- Does not add or commit files automatically
- Folder becomes ready for git commands like add, commit
- Use 'ls -a' to see the .git folder
Full Transcript
To create a git repository, first open your terminal and navigate to the folder where you want the repository. Run 'git init' there. This command creates a hidden folder named .git that stores all git data. After this, your folder is a git repository ready to track files. The .git folder is hidden, so use 'ls -a' to see it. Running 'git init' does not add any files automatically; you must add files manually. If you run 'git init' again in the same folder, it safely reinitializes the existing repository without overwriting the .git folder or resetting history.

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