Git Basics β Your First Repository
The everyday Git commands: init, status, add, commit, log, and push.
Time to use Git for real. In this lesson youβll create a repository, save snapshots of your work, and push it to GitHub.
π― Learning Objectives
By the end of this lesson you will:
- Initialise a Git repository
- Check status and stage changes
- Commit snapshots with good messages
- Read the commit history
- Connect a repo to GitHub and push
Create a Repository
mkdir my-project && cd my-project
git initgit init creates a hidden .git folder β thatβs where Git stores your entire history.
The status β add β commit Cycle
Create a file, then check what Git sees:
Stage the change, then commit it as a snapshot:
git add README.md # stage one file
git add . # or stage everything
git commit -m "Add project README"β Write meaningful commit messages
git commit -m "stuff" helps no one. Describe what changed and why: "Fix login redirect on expired session". Your future self and your teammates read these constantly.
Read the History
git log # full history
git log --oneline # compact, one line per commitPush to GitHub
Create an empty repository on GitHub, then connect and push:
git remote add origin https://github.com/your-user/my-project.git
git branch -M main
git push -u origin mainAfter the first push -u, future uploads are just git push.
π‘ Use SSH to skip password prompts
Remember the SSH keys from the Linux section? Add your public key to GitHub (Settings β SSH keys) and use the SSH remote git@github.com:your-user/my-project.git to push without typing credentials.
π§ͺ Hands-on Lab
Create and Commit a Repository
- Create a folder
git-laband rungit init - Create a
README.mdfile - Stage and commit it with a clear message
- View the history with
git log --oneline
π§ Knowledge Check
Which command turns a normal folder into a Git repository?
What is the correct order to save and share a change?
πΌ Interview Preparation
What is the difference between the working directory, the staging area, and the repository?
Summary
You created a repo, staged and committed changes, read the log, and pushed to GitHub. These commands are the ones youβll run dozens of times a day.