1.6K Views

GIT104 - Essential Git Commands

Learn the essential Git commands used daily by developers, including add, commit, status, and log.

Mastering basic Git commands is essential for working efficiently with version control.
In this chapter, you will learn how to stage files, create commits, inspect repository status, and explore commit history.

Understanding the Git Workflow

Git operations typically follow this three-step workflow:

  1. Modify files in your Working Directory
  2. Stage the changes using git add
  3. Save the changes using git commit

These commands form the foundation of all Git usage.


Checking the Status of Your Repository

The git status command shows what is happening inside your project:

git status

Example output:

On branch main
Changes not staged for commit:
  modified: index.html

Staging Files with git add

Stage a single file:

git add index.html

Stage multiple files:

git add file1.js file2.css

Stage all modified files:

git add .

Unstage a file (remove it from staging area):

git restore --staged index.html

Creating Commits with git commit

A commit is a snapshot of your project.

Create a simple commit:

git commit -m "Add homepage layout"

Commit with a detailed message:

git commit -m "Fix login bug" -m "Resolved issue with token validation and improved error handling"

Amend the previous commit:

git commit --amend

Viewing Commit History with git log

Show full commit history:

git log

Typical output:

commit a3f1c9d2b1e4
Author: Your Name <your.email@example.com>
Date:   Tue Jan 12 14:22:01 2025
 
    Add header component

Compact view:

git log --oneline

Graph view:

git log --oneline --graph --all

Skipping the Staging Area (Optional)

Commit tracked changes without staging:

git commit -am "Update styles"

Notes:

  • Only works for tracked files
  • New files still require git add

Removing Files with git rm

Delete a tracked file:

git rm oldfile.js

Remove file from Git but keep it locally:

git rm --cached settings.json

Summary

In this chapter, you learned:

  • How to check repository status
  • How to stage and unstage files
  • How to create commits
  • How to view commit history
  • How to modify or skip the staging step
  • How to remove files using Git

These commands are used every day by developers and form the backbone of all Git workflows.

Next, we will explore branching in Git and learn how to create, switch, and merge branches effectively.