Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Create a Git Repository with Three Clean Commits, a .gitignore That Hides Secrets, and One Remote

Beginner30 minGit · Git Bash or Terminal · A free GitHub account

Course: DevOps · Chapter 2: Git, GitHub and Git Bash · Lesson: 2.13 Lab: three commits and one remote

Chapter 2 explains Git, GitHub and the staging area; this lab does it once, end to end.

Chala mitrano! Aaj aapan aaplya laptop var pahila Git repo banvuya. Three small commits, one .gitignore, ani ek remote. Ekdum simple aahe.

Suppose we are…

Suppose we have just joined an Infosys project team as a fresher. On day one the team lead says: "Start the badge API, commit small, and never push the .env file." Many freshers zip the folder and email it. That has no history and no review. Git keeps every change with a message, and GitHub keeps a shared copy for the team.

In this lab we create the repo from zero, make three clean commits, prove that the secret file is ignored, and connect one remote.

Goal of this lab

By the end you will have:

  • A local repository with exactly three commits and clear messages.
  • A .gitignore that keeps .env, node_modules/ and *.pem out of Git.
  • A remote named origin, checked with git remote -v.

What you need

  • Git installed (git --version shows 2.x).
  • Git Bash on Windows, or Terminal on Mac and Linux.
  • A free GitHub account and one empty repository you created yourself (no README).
  • 30 minutes.

Part 1: Create the repository

  1. Tell Git who you are (only once per laptop):

    git config --global user.name "Your Name"
    git config --global user.email "you@example.com"
    git config --global init.defaultBranch main
    
  2. Create a folder and turn it into a repository:

    mkdir badge-api-hello
    cd badge-api-hello
    git init
    

    What you should see: Initialized empty Git repository in .../badge-api-hello/.git/

  3. Check the state. Get used to running this before every add:

    git status
    

    What you should see: On branch main and No commits yet.

Part 2: Three small commits

  1. Commit 1: a tiny app file.

    cat > app.js <<'EOF'
    const http = require("http");
    http.createServer((req, res) => res.end("badge api ok\n")).listen(3000);
    EOF
    git add app.js
    git commit -m "Add hello badge API on port 3000"
    

    What you should see: 1 file changed, 2 insertions(+)

  2. Commit 2: a .gitignore so junk and secrets never enter Git.

    cat > .gitignore <<'EOF'
    .env
    node_modules/
    *.pem
    *.log
    EOF
    git add .gitignore
    git commit -m "Ignore env files, keys, logs and node_modules"
    
  3. Commit 3: a short README.

    printf "# badge-api-hello\n\nRun: node app.js\nOpen: http://localhost:3000\n" > README.md
    git add README.md
    git commit -m "Add README with run steps"
    
  4. Read the story of your repo:

    git log --oneline
    

    What you should see: three lines, newest first, each with a short id and your message.

    c3d4e5f Add README with run steps
    b2c3d4e Ignore env files, keys, logs and node_modules
    a1b2c3d Add hello badge API on port 3000
    

Part 3: Prove the secret file is ignored

  1. Create a fake secret file. Never put a real password in a lab.

    echo "DB_PASSWORD=fake-lab-value" > .env
    git status
    

    What you should see: nothing to commit, working tree clean. The .env file is not listed, because .gitignore hides it.

  2. Ask Git why a file is ignored:

    git check-ignore -v .env
    

    What you should see: .gitignore:1:.env .env

Part 4: Connect one remote

  1. On GitHub, open your empty repository and copy its HTTPS or SSH URL. Add it as origin (replace YOURUSER):

    git remote add origin https://github.com/YOURUSER/badge-api-hello.git
    git remote -v
    

    What you should see: two lines for origin, one (fetch) and one (push).

  2. Push only if your sign-in already works (a token in the credential manager, or an SSH key):

    git push -u origin main
    

    If push asks for a password, do not type your GitHub account password. Use a personal access token or set up SSH (lesson 2.12). The three local commits already finish this lab.

    What you should see: main -> main and branch 'main' set up to track 'origin/main'.

Ravindra Bagale's Tip

Run git status before every git add .. Two seconds of reading saves you from committing a key file that you then have to rotate.

Common mistakes

MistakeWhat happensFix
Typing git add . before creating .gitignore.env or node_modules gets stagedgit rm --cached .env, add it to .gitignore, commit again
Running git remote add origin twiceerror: remote origin already exists.Use git remote set-url origin <url>
Using the GitHub account password on pushAuthentication failedUse a personal access token or an SSH key
One giant commit called "update"Nobody can review or undo one partSmall commits with a clear verb: Add, Fix, Remove

Self-check checklist

0 of 4 done

Try-at-home challenge

Change the message in app.js, run git diff to see the change, then undo it with git restore app.js. Write down when git commit --amend is safe (only before you push).

Samajla ka? Small commits, clean .gitignore, one remote. Aata pudhe jaauya branches and pull requests madhe.