Git Git and GitHub

How to Use Git to Commit Changes and Revert Code in Your Project

Posted: January 09, 2026 | Updated: January 09, 2026
Share: Twitter LinkedIn

How to Use Git to Commit Changes and Revert Code in Your Project

Posted: December 05, 2025 | Updated: December 05, 2025

Git is a powerful version control system that allows developers to track changes, collaborate with others, and manage project history efficiently. Whether you’re working on a small personal project or a large team-based application, understanding how to commit changes and undo mistakes is essential.

In this guide, we’ll cover:

  1. Committing changes to your project

  2. Viewing your commit history

  3. Reverting code to a previous commit


1. Committing Changes in Git

After editing your code, Git allows you to stage and commit your changes so they become part of the project history.

Step 1: Check the status of your repository

git status
  • Shows which files are modified, staged, or untracked.

  • Example output:

On branch main
Changes not staged for commit:
  modified:   app/models.py
  modified:   app/views.py
Untracked files:
  new_file.py

Step 2: Stage the changes

Use git add to stage files you want to include in your next commit:

git add app/models.py app/views.py
  • To stage all changes, you can use:

git add .

Step 3: Commit the changes

Once staged, commit your changes with a descriptive message:

git commit -m "Add user authentication and profile page"

  • The commit message should clearly describe the changes you made.

  • After committing, your changes are saved in Git’s history.


2. Viewing Your Commit History

To see all commits in your project:

git log --oneline

  • Example output:

6sfsfb0a Add user authentication and profile page
fsfsfgh Fix bug in post creation
i5sf9l Initial commit

  • Each commit has a hash (e.g., 6c03b0a) that identifies it.

  • This hash is useful when you want to revert or reset changes.


3. Reverting Code to a Previous Commit

Sometimes you may make changes that you no longer want to keep. Git provides several ways to undo changes:

Option A: Keep changes in working directory (soft reset)

git reset --soft 

  • Moves your branch pointer to the specified commit.

  • Keeps changes in your working directory staged for a new commit.

  • Example:

git reset --soft f4vdv6h

Option B: Undo a commit safely (create a new commit)

git revert 

  • Creates a new commit that undoes the specified commit.

  • Safe to use if you’ve already shared your branch with others.

Example:

git revert 6ddg0a

  • Git will generate a commit like “Revert ‘Add user authentication and profile page’”.

Option C: Discard all changes completely (hard reset)

git reset --hard 

  • Resets your branch to the given commit and discards all changes after it.

  • Warning: This is destructive — changes cannot be recovered unless backed up.

Example:

git reset --hard ffsesh

  • Use git status after this to confirm your branch matches the previous state.


4. Pushing Changes to Remote Repository

After committing or reverting locally, you can update your remote repository:

git push origin main

  • If you used --hard reset or changed history, you may need to force push:

git push origin main --force

⚠️ Use force push with caution, as it overwrites remote history.