Back to Git & Dev Tools
2025-12-248 min read

Make changes and commit (Git & Dev Tools)

Learn Make changes and commit (Git & Dev Tools) step by step with clear examples and exercises.

Why This Matters

Understanding how to make changes and commit using Git is crucial for any developer working in a team or maintaining their own projects. With Git, developers can efficiently manage changes, collaborate on projects, and revert back to previous states if necessary. This skill is essential for ensuring code quality, reducing conflicts, and promoting effective collaboration among developers.

Prerequisites

To follow this lesson, you should have a basic understanding of:

  1. The command line interface (CLI)
  2. Navigating directories in the CLI
  3. Basic file manipulation using commands like touch, ls, and mv
  4. Understanding the concept of a repository and working directory
  5. Familiarity with text editors or Integrated Development Environments (IDEs) for editing files
  6. Knowledge of branching and merging concepts in Git (optional but recommended)

Core Concept

Setting Up Git

To start using Git, you first need to install it on your system. On most Linux distributions, Git is available in the package manager, so you can simply run:

sudo apt-get install git

For Windows and macOS, you can download Git from the official website ().

Once installed, navigate to your project directory in the command line:

cd /path/to/your/project

Initialize a new Git repository by running:

git init

This creates a hidden .git directory within your project that contains all the necessary files for version control.

Making Changes and Committing

To make changes to your code, edit the files using your preferred text editor or IDE. Once you've made modifications, save the files and switch back to the command line. To see the changes you've made, use the git status command:

git status

This will display a list of any modified files in your repository along with their status (untracked, modified, or staged). If you're ready to commit these changes, stage them using git add. You can stage individual files or use git add . to stage all modified files at once:

git add filename.ext
git add .

After staging your changes, create a commit with a descriptive message that explains the nature of the changes:

git commit -m "Your commit message"

Viewing Commit History

To view the commit history for your repository, use the git log command:

git log

This will display a list of all commits in reverse chronological order, along with their commit messages and hash identifiers.

Worked Example

Let's create a simple example using a file called example.txt. First, navigate to your project directory:

cd /path/to/your/project

Create the file and write some content:

touch example.txt
echo "Hello, World!" > example.txt

Now, initialize Git and make an initial commit:

git init
git add example.txt
git commit -m "Initial commit"

Make some changes to the example.txt file:

echo "Hello, Universe!" > example.txt

Stage and commit the changes:

git add example.txt
git commit -m "Updated content"

View the commit history to see your two commits:

git log

Common Mistakes

  1. Forgetting to stage changes: Before committing, you must stage (add) any modified files using git add. Failing to do so will result in untracked changes that won't be included in the commit.
  2. Not providing a commit message: A descriptive commit message is essential for understanding the purpose of each commit and maintaining a clear history. Always provide a meaningful commit message when committing changes.
  3. Committing unnecessary changes: Be mindful of what you're committing. If you have unrelated changes in your working directory, stage and commit them separately to keep your commit history organized.
  4. Ignoring files: Sometimes, you may want to ignore certain files or directories from being tracked by Git. To do this, create a .gitignore file in your repository root and list the files or directories you want to ignore on separate lines.
  5. Not resolving merge conflicts: When working on a collaborative project, it's important to resolve any merge conflicts that arise during merging branches or pulling changes from remote repositories. Failing to do so can lead to issues in the codebase.
  6. Not using branches: Working directly on the main branch without creating separate branches for new features or bug fixes can lead to a messy commit history and potential conflicts. Using branches helps maintain a clean and organized commit history.
  7. Not using feature branches: When working on a collaborative project, it's important to create separate feature branches for each new feature or bug fix. This allows developers to work independently without affecting the main codebase until they're ready to merge their changes back into the main branch.
  8. Not rebasing: Rebasing is a powerful Git command that helps keep your project's commit history clean and linear by integrating changes from one branch onto another. Failing to use it can lead to a complex, difficult-to-manage commit history.
  9. Not using git stash: Git stash allows you to save your uncommitted changes temporarily so that you can switch to a different branch or work on something else without losing your progress. Failing to use it can result in lost work if you have to suddenly stop working on a feature.
  10. Not cleaning up unnecessary branches: After completing a feature or bug fix, it's important to delete the corresponding branch to keep your Git repository clean and organized. Failing to do so can lead to confusion and potential conflicts.

Practice Questions

  1. Create a new Git repository for a project called "MyProject". What command should you run?
  2. You've made several changes to your codebase, but forgot to stage some of them before committing. How can you include these unstaged changes in the next commit?
  3. You want to ignore a file named .env in your Git repository. What steps should you follow to achieve this?
  4. You've made a mistake in your last commit and want to revert back to the previous state. How can you do this using Git?
  5. You're working on a collaborative project with other developers, and someone else has made changes to a file that conflicts with your local changes. What steps should you take to resolve the conflict and merge their changes into your repository?
  6. Explain the difference between git add . and git commit -a.
  7. What is Git stash, and how can it be useful when working on a project?
  8. Explain the concept of rebasing in Git and why it's important to use it.
  9. How can you view the differences between two commits in your Git repository?
  10. What is a merge commit, and what are some situations where they might be useful or problematic?

FAQ

  1. What is the purpose of Git's staging area? The staging area (or index) serves as an intermediate step between your working directory and the commit history. It allows you to selectively choose which changes to include in the next commit.
  2. Can I revert a single file to a previous version without affecting other files? Yes, you can use git checkout followed by the file path and a specific commit hash to restore a single file to a previous state.
  3. How do I collaborate on a Git repository with others? To collaborate, you'll typically clone the remote repository to your local machine, make changes, and push them back to the remote repository using commands like git pull, git add, git commit, and git push.
  4. What is a merge conflict, and how do I resolve it? A merge conflict occurs when two or more developers have made conflicting changes to the same line(s) of code in the same file. Git will notify you of the conflict, and you'll need to manually edit the affected files to resolve the issue before committing and merging the changes.
  5. What is a branch in Git, and why should I use them? A branch represents an independent line of development within a repository. Branches allow you to work on new features or bug fixes without affecting the main codebase until you're ready to merge the changes back into the main branch (usually master or main). Using branches helps maintain a clean and organized commit history.
  6. What is Git stash, and how can it be useful when working on a project? Git stash allows you to save your uncommitted changes temporarily so that you can switch to a different branch or work on something else without losing your progress. It's particularly useful when you need to address an urgent issue or switch branches but haven't yet committed your current work.
  7. What is rebasing in Git, and why is it important? Rebasing is a powerful Git command that helps keep your project's commit history clean and linear by integrating changes from one branch onto another. By using rebasing, you can simplify the commit history, eliminate redundant commits, and make it easier to manage your Git repository.
  8. How can I view the differences between two commits in my Git repository? To view the differences between two commits, use the git diff command followed by the commit hashes:
git diff <commit_hash1>..<commit_hash2>

This will display the changes made between the two specified commits.

  1. What is a merge commit, and what are some situations where they might be useful or problematic? A merge commit combines changes from two different branches into a single commit. They can be useful when integrating changes from multiple branches, but they can also make your Git history more complex if not managed properly. In some cases, it may be preferable to use git rebase instead of merging directly to keep the history cleaner and easier to understand.
  2. What is a cherry-pick in Git, and when might it be useful? Cherry-picking allows you to apply individual commits from one branch onto another. This can be useful when you want to incorporate specific changes from one branch into another without merging the entire branch history. However, cherry-picking should be used with caution as it can lead to conflicts and potential issues if not done carefully.
Make changes and commit (Git & Dev Tools) | Git & Dev Tools | XQA Learn