Open Source for Beginners
Getting Started with Open source
Demo 2 Basic Git Commands
Learn how to use Git for version control by walking through a simple mock project. We’ll cover:
- Initializing a Git repository
- Staging and committing files
- Inspecting commit history
By the end of this guide, you’ll understand the core Git commands for day-to-day workflows.
Table of Contents
- Initialize a Git Repository
- Stage and Commit
greetings.txt
- Add and Commit
bye.txt
- Quick Reference: Common Git Commands
- Links and References
1. Initialize a Git Repository
Start by creating a new directory and turning it into a Git repository:
mkdir hello-git
cd hello-git
git init
You should see:
Initialized empty Git repository in /path/to/hello-git/.git/
Now you’re inside a Git-controlled project. All subsequent Git commands will operate here.
2. Stage and Commit greetings.txt
First, create a file named greetings.txt
:
nano greetings.txt
Check the repository status to see untracked files:
git status
Sample output:
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
greetings.txt
Stage and commit your changes:
git add greetings.txt
git commit -m "Add greetings.txt with initial message"
View the commit history:
git log
Sample output:
commit eafd1cdd14d46c191c7aaf2675b5aef6c418c17 (HEAD -> master)
Author: you <[email protected]>
Date: Sat Sep 24 06:15:31 2022 +0530
Add greetings.txt with initial message
Note
A clear, descriptive commit message helps you and your team understand the purpose of each change.
3. Add and Commit bye.txt
Repeat the workflow to add a second file:
Create
bye.txt
:nano bye.txt
Confirm it’s untracked:
git status
Stage all changes at once:
git add .
Commit with a message:
git commit -m "Add bye.txt"
Review the full commit history:
git log
You’ll see both commits listed, with the most recent on top.
Warning
Always run git status
before committing to avoid accidentally omitting or including unwanted files.
Quick Reference: Common Git Commands
Command | Use Case | Example |
---|---|---|
git init | Create a new Git repository | Initialize version control in a project |
git status | Show untracked, staged, and changed files | Verify your working directory state |
git add <file> | Stage changes for the next commit | git add greetings.txt |
git add . | Stage all changes | Stage new, modified, and deleted files |
git commit -m "<message>" | Commit staged changes with a message | git commit -m "Add new feature" |
git log | View commit history | Display commits in reverse chronological order |
Links and References
Start practicing these commands today to build a solid foundation in version control!
Watch Video
Watch video content