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

  1. Initialize a Git Repository
  2. Stage and Commit greetings.txt
  3. Add and Commit bye.txt
  4. Quick Reference: Common Git Commands
  5. 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

The image shows a GNU Nano text editor screen with a new file named "greetings.txt" open. The screen displays various command options at the bottom.

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:

  1. Create bye.txt:

    nano bye.txt
    
  2. Confirm it’s untracked:

    git status
    
  3. Stage all changes at once:

    git add .
    
  4. Commit with a message:

    git commit -m "Add bye.txt"
    
  5. 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

CommandUse CaseExample
git initCreate a new Git repositoryInitialize version control in a project
git statusShow untracked, staged, and changed filesVerify your working directory state
git add <file>Stage changes for the next commitgit add greetings.txt
git add .Stage all changesStage new, modified, and deleted files
git commit -m "<message>"Commit staged changes with a messagegit commit -m "Add new feature"
git logView commit historyDisplay commits in reverse chronological order

Start practicing these commands today to build a solid foundation in version control!

Watch Video

Watch video content

Previous
How To Contribute with KodeKlouds Model for Contribution