← Git

Coding challenge

Challenge 001 - First commit

Scenario

You have a folder with a new project that is not in Git yet. Your goal is to turn it into a repository, stage the files, and make the first commit. When you are done, git log must show one commit and git status must indicate that there is nothing pending.

Starting point: an empty folder where you will create an index.html file.

How to solve it

# 1. Turn the folder into a Git repository
git init
# Creates the hidden .git folder. Git now tracks this directory.

# 2. Create a file so there is something to commit
echo "<h1>Hola mundo</h1>" > index.html

# 3. Check the status: index.html shows up as untracked
git status
# Untracked files: index.html

# 4. Stage the file (move it to the staging area)
git add index.html
# You could also use: git add .  (for everything)

# 5. Commit: create the first commit with a clear message
git commit -m "Primer commit: agrega pagina inicial"

Why it works

The flow follows Git's three areas. git init sets up the repository. The file starts in the working directory as untracked. With git add we move it to the staging area, choosing what goes into the commit. With git commit we save that snapshot into the repository. The imperative message documents what the commit does.

Full solution

View commented solution
# Initialize the repository
git init

# (Optional but recommended) check your identity for the commits
git config user.name    # should show your name
git config user.email   # should show your email

# Create content
echo "<h1>Hola mundo</h1>" > index.html

# Stage and commit
git add index.html
git commit -m "Primer commit: agrega pagina inicial"

# Check the result
git log --oneline
# 1a2b3c4 Primer commit: agrega pagina inicial

git status
# On branch main
# nothing to commit, working tree clean

Key point: without git add, the commit would have nothing to save. The commit only includes what is in the staging area.