Git 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). - 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/. In this lesson you save the output of each command into astepN/schrittN.txtfile as proof.
Your goal
You create a repo, add a file, make your first commit and view the history.
Exercises
1. Initialize a repository
Concept: initialise a repository.
git initturns a normal folder into a Git repository (it creates a hidden.gitfolder). Save the output as proof.mkdir -p /home/student/git-uebung && cd /home/student/git-uebung git init > schritt1.txt cat schritt1.txtCheck:
schritt1.txtcontains the wordInitialized(from "Initialized empty Git repository").2. Status of an empty repo
Concept:
git status.git statusshows the current state: which branch you are on and what changes exist. In a fresh repo everything is "clean".cd /home/student/git-uebung git status > schritt2.txt cat schritt2.txtCheck:
schritt2.txtcontainsbranchorcommit(from the status output).3. Track a file
Concept: staging with
git add. Before a file goes into a commit, you must "stage" it. Create a file, stage it, and record the status.cd /home/student/git-uebung echo "hello" > hallo.txt git add hallo.txt git status > schritt3.txt cat schritt3.txtCheck:
schritt3.txtmentionshallo.txt(as a staged file).4. Make your first commit
Concept: commit. A commit saves the staged changes as a snapshot with a description (
-m). Use the messagefirst commit.cd /home/student/git-uebung git -c user.email=student@lab -c user.name=Student commit -m "first commit" > schritt4.txt cat schritt4.txtThe
-c user....options set name/email for the commit once (otherwise Git complains).Check:
schritt4.txtcontainsfirst commit.5. Show the commit log
Concept: commit history.
git logshows all commits. With--onelineyou get one line per commit: the shortened commit ID (hash) plus the message.cd /home/student/git-uebung git log --oneline > schritt5.txt cat schritt5.txtCheck:
schritt5.txtcontains a line with a hash (at least 7 chars) followed by text.
Related courses
Now practice it yourself
Reading is good – doing is better. Start this course 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).
