Practise this module for free on a real VM
Sign up for freeGit Basics
Understand version control with git. Run real git commands and save the output to a file as evidence.
Your first git commits
Git basics: your first commits
Git is the standard tool for version control of code: it stores snapshots of your files so you can trace and roll back every change. Every developer uses it daily.
Key terms
- Repository (repo): a project folder managed by Git (
git initcreates it). - Branch: a line of development; the main branch is usually called
maintoday. - Staging (
git add): you choose which changes go into the next snapshot. - Commit (
git commit): a saved snapshot with a description. - Log (
git log): the list of all commits.
Work in the folder /home/student/git-uebung/. git status shows you where you stand after every step.
How the checker works
It looks directly into your repository (read-only): whether it exists, which branch you are on, which files Git knows and which commits exist. You do not need to copy any output into files.
Your goal
You create a repo, rename the branch, add a file, make your first commit and build a small history.
Exercises
1. Initialize a repository
Concept: initialise a repository.
git initturns a normal folder into a Git repository (it creates a hidden.gitfolder).mkdir -p /home/student/git-uebung && cd /home/student/git-uebung git init git statusCheck:
git statusin the folder/home/student/git-uebungreportsNo commits yetinstead ofnot a git repository. The checker looks directly into your repository.2. Read and rename the branch
Concept: branch. The first line of
git statustells you which branch (line of development) you are on — in a fresh repo oftenmaster. Many projects call the main branchmaintoday. Rename it and check the result:cd /home/student/git-uebung git status git branch -m main git statusCheck:
git statusstarts withOn branch main.3. Track a file
Concept: staging with
git add. Before a file goes into a commit, you must "stage" it — Git then keeps it ready. Create a file, stage it and look at the status each time:cd /home/student/git-uebung echo "hello" > hallo.txt git status git add hallo.txt git statusBefore
git add,hallo.txtis listed under Untracked files, afterwards under Changes to be committed.Check:
git statuslistshallo.txtunderChanges to be committed.4. Make your first commit
Concept: commit. A commit saves the staged changes as a snapshot with a description (
-m). Git wants to know who makes the commit — set your name and email once (a made-up address is fine):cd /home/student/git-uebung git config --global user.name "Your Name" git config --global user.email "you@example.org" git commit -m "first commit"Check:
git log --onelineshows a line like1a2b3c4 first commit.5. Second commit and history
Concept: commit history. Every commit attaches to the previous one — that is how the history grows. Change
hallo.txt, commit the change and look at the history:cd /home/student/git-uebung echo "second line" >> hallo.txt git commit -am "second commit" git log --oneline-aautomatically includes all known, changed files. With--onelineyou get one line per commit: the shortened commit ID (hash) plus the message.Check:
git log --onelineshows at least two commits.
Knowledge check: Git basics
No VM, about 3 minutes. You recall what you practised in this module – wrong answers come back once more at the end.
Related courses
Now practice it yourself
Reading is good – doing is better. Practise this module on a real Linux VM, right in your browser. A free account is all it takes.
Start for freeLab content under CC BY 4.0 – free to use with attribution (© TechLogia).
