Back to Git & Dev Tools
2026-01-266 min read

Commit level operations (Git & Dev Tools)

Learn Commit level operations (Git & Dev Tools) step by step with clear examples and exercises.

Why This Matters

In this extensive guide, we delve into commit level operations using Git and various developer tools. Understanding these concepts is crucial for mastering version control, debugging, and optimizing code efficiently.

Why This Matters

Commit level operations are essential in software development to manage changes made to the source code, collaborate with team members, and fix errors effectively. With commit level operations, you can track modifications, revert unwanted changes, and easily switch between different versions of your project.

Prerequisites

Before diving into commit level operations, it is essential to have a basic understanding of:

  1. Git fundamentals (initialization, cloning, branching, merging)
  2. Basic command line navigation
  3. Familiarity with a text editor or Integrated Development Environment (IDE) like Visual Studio Code or Sublime Text
  4. Understanding of common programming concepts such as variables, functions, and control structures
  5. Knowledge of Git commands for configuring user information (name, email, etc.)
  6. Familiarity with using GitHub or other version control platform

Core Concept

Creating a Commit

A commit is a snapshot of your project's source code at a specific point in time. To create a new commit:

  1. Stage the changes you want to include in the commit using git add or git add . for all files.
  2. Write a short, descriptive commit message using git commit -m "".
  3. Verify your commit history with git log.
  4. Configure user information (name, email) if not already set using git config user.name and git config user.email .
  5. Set the initial commit author using git config user.name "Initial Author" && git config user.email "initial.author@example.com".

Amending Commits

Sometimes you may need to modify an existing commit. To amend the most recent commit:

  1. Use git commit --amend to open the editor and make changes to the commit message.
  2. Save and close the editor to finalize the amended commit.
  3. If you need to change the content of a file in an existing commit, create a new patch with git diff > patch.txt, apply the patch using patch < patch.txt, stage the changes, and then use git commit --amend to create a new commit with the updated content.

Reverting Commits

If you need to revert specific changes, use the following commands:

  1. To revert to a specific commit, find its hash using git log and run git revert .
  2. To undo the latest commit, use git reset --hard HEAD~1.
  3. If you want to discard changes made in the working directory without committing them, use git reset --hard. Be cautious when using this command as it will erase all uncommitted changes.

Stashing Changes

When you're in the middle of work but need to switch branches or handle an emergency, you can stash your changes temporarily:

  1. Use git stash to save your current changes.
  2. Retrieve your stashed changes with git stash apply.
  3. To view a list of all stashed changes, use git stash list.
  4. If you have multiple stashes and want to apply a specific one, use git stash apply .

Worked Example

Let's demonstrate commit level operations using a simple example project. We will create, amend, revert, and stash commits to illustrate the concepts discussed above.

Step 1: Initialize a new Git repository

$ mkdir my-project && cd my-project
$ git init
$ git config user.name "Your Name"
$ git config user.email "your.email@example.com"

Step 2: Create an initial commit with a README file

$ touch README.md
$ git add README.md
$ git commit -m "Initial commit"

Step 3: Make changes and create a new commit

$ echo "This is my project!" >> README.md
$ git status
$ git add .
$ git commit -m "Added project description"

Step 4: Amend the latest commit

$ git commit --amend -m "Fixed typo in commit message"

Step 5: Revert the last commit

$ git revert HEAD

Step 6: Stash changes and switch branches

$ git stash
$ git checkout new-branch

Common Mistakes

  1. Forgetting to stage changes before committing: Always use git add to stage your changes before creating a commit.
  2. Writing long and vague commit messages: Keep commit messages short, descriptive, and easy to understand. Use the 72 character limit as a guideline.
  3. Not using --amend when modifying an existing commit: If you need to modify the commit message or content of an existing commit, use git commit --amend.
  4. Reverting commits without understanding their impact: Be cautious when reverting commits, as it may undo important changes. Make sure you understand what each commit contains before reverting it.
  5. Ignoring stashed changes: After stashing changes, remember to apply them before working on a different branch or switching back to the original branch.
  6. Stashing untracked files: By default, Git only stashes tracked files. If you have untracked files in your working directory that you want to include in the stash, use git stash -u or add them to the Git index before stashing with git add .
  7. Stashing changes that conflict with other stashed changes: When applying multiple stashes, conflicts may arise due to overlapping changes. Resolve these conflicts manually before continuing.
  8. Not using --hard when resetting the working directory: If you use git reset, it will move the HEAD pointer but leave your files intact. Use git reset --hard to discard all changes and return to the last committed state.

Practice Questions

  1. How can you view the history of all commits in your repository?
  2. What command should you use to stage all modified files for a commit?
  3. If you need to revert a specific file to its previous state, what command should you run?
  4. How can you temporarily save your changes and switch branches without losing them?
  5. What happens when you run git reset --hard HEAD~1?
  6. Why is it important to write descriptive commit messages?
  7. What is the difference between stashing changes and committing them?
  8. How can you view and apply multiple stashed changes?
  9. What should you do if you have untracked files that need to be included in a stash?
  10. What happens when you use git reset --hard without specifying a commit hash?

FAQ

Q: What is the difference between stashing changes and committing them?

A: Committing saves your changes as a new snapshot in your repository's history, while stashing temporarily saves your changes without creating a new commit. Stashed changes can be reapplied later on.

Q: Can I amend an older commit instead of creating a new one with a different message?

A: Yes, you can use git commit --amend to modify the most recent commit or any previous commit by using git commit --amend . However, this will create a new commit with the same hash as the original commit. To change the commit message of an older commit without creating a new one, use git rebase -i and follow the interactive rebase prompts to edit the commit messages.

Q: What is the purpose of Git stash apply?

A: git stash apply retrieves your stashed changes and applies them to your working directory, allowing you to switch branches or handle emergencies without losing your work. If you want to discard the stashed changes after applying them, use git stash drop.

Q: What is the difference between git reset --hard and git checkout or git restore ?

A: git reset --hard discards all changes in the working directory and moves the HEAD pointer to a specific commit. It can be used with a commit hash or HEAD~n, where n is the number of commits to move back. git checkout or git restore restores a file from a specific commit or branch, discarding any local changes made to that file.

Q: What happens when you run git reset --hard without specifying a commit hash?

A: When you run git reset --hard without specifying a commit hash, it will move the HEAD pointer to the most recent commit and discard all changes in the working directory, effectively undoing any local changes. Be cautious when using this command as it can result in data loss if not used correctly.

Commit level operations (Git & Dev Tools) | Git & Dev Tools | XQA Learn