Back to Git & Dev Tools
2026-04-196 min read

Committing and pushing (Git & Dev Tools)

Learn Committing and pushing (Git & Dev Tools) step by step with clear examples and exercises.

Title: Mastering Git Commits and Pushes for Developers - Expanded Version

Why This Matters

In software development, version control is essential to manage changes in a project's codebase effectively. Git is an open-source distributed version control system that allows developers to work collaboratively on projects with ease. Understanding how to commit and push changes in Git is crucial for efficient collaboration and maintaining a well-organized codebase.

A well-structured Git history helps teams keep track of the evolution of their project, making it easier to identify bugs, revert unwanted changes, and collaborate effectively. Moreover, using Git enables developers to work independently without fear of overwriting each other's changes.

Prerequisites

Before diving into the core concept of committing and pushing, it's important to have a basic understanding of:

  1. Git installation: Make sure you have Git installed on your system. You can download it from official Git website.
  2. Initializing a Git repository: Learn how to initialize a new Git repository in an existing project or create a new one with Git. Familiarize yourself with the git init command and its usage.
  3. Basic Git commands: Familiarize yourself with essential Git commands like git status, git add, git commit, and git log. Understand how to view the current status of your repository, stage files for committing, create new commits, and examine the commit history.
  4. Branching and merging: Learn about creating branches, switching between them, and merging changes from one branch to another using Git commands like git branch, git checkout, and git merge.
  5. Git configuration: Understand how to set up your user information (name and email) in Git using the git config command. This information will be associated with each commit you make, making it easier for others to identify who made changes to the codebase.

Core Concept

Creating a Commit

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

  1. Stage files: Use the git add command to stage (prepare for committing) the changes you want to include in the new commit. For example:
git add <file_name>
  1. Commit with a message: Once your files are staged, create a new commit by providing a descriptive commit message using the git commit command:
git commit -m "Your commit message"

A good commit message should clearly describe what changes were made, why they were made, and any relevant context. Avoid using vague or uninformative messages like "Fixing bugs."

Pushing Commits to a Remote Repository

After creating a local commit, you may want to share your changes with others or store them on a remote server like GitHub. To push commits to a remote repository:

  1. Set up the origin: If you haven't already, set up the remote repository as the origin using the git remote add command:
git remote add origin <remote_repository_url>
  1. Push commits: Push your local commits to the remote repository using the git push command:
git push -u origin master

Amending Commits

Sometimes, you might need to modify a recently committed change. To amend your most recent commit, use the git commit --amend command followed by a new commit message:

git commit --amend -m "Your updated commit message"

Stashing Changes

If you have uncommitted changes but need to switch branches or work on something else, you can stash your changes using the git stash command. This command saves your uncommitted changes and allows you to apply them later:

git stash save "Your stash message"

To apply a stashed change, use the git stash apply command:

git stash apply

Worked Example

Let's walk through a simple example of creating and pushing commits to a GitHub repository.

  1. Initialize a new Git repository in an existing project:
cd my_project
git init
  1. Create a file called README.md with some content:
echo "# My Project" > README.md
  1. Stage and commit the new file:
git add README.md
git commit -m "Initial commit: Add README file"
  1. Set up the remote repository as the origin:
git remote add origin <https://github.com/yourusername/my_project.git>
  1. Push commits to the remote repository:
git push -u origin master

Common Mistakes

1. Forgetting to commit changes

Always remember to stage and commit your changes before pushing them to a remote repository. Uncommitted changes won't be included in the push.

2. Not providing a meaningful commit message

A good commit message should clearly describe what changes were made, why they were made, and any relevant context. Avoid using vague or uninformative messages like "Fixing bugs."

3. Pushing commits without checking the remote branch

Always check your remote branch before pushing to ensure you're not overwriting someone else's changes. Use the git pull command to fetch and merge any new changes from the remote repository:

git pull origin master

4. Pushing commits with sensitive information exposed

Be careful when committing files that contain sensitive information, such as API keys or passwords. If you accidentally commit such files, remove them from the Git history using git filter-branch or git-filter-repo.

5. Ignoring large files in Git repositories

Large files can make your Git repository grow rapidly and slow down operations. To address this issue, consider ignoring large files using a .gitignore file or compressing them before committing.

Practice Questions

  1. You have made some changes to your local repository but want to switch branches temporarily. How would you save your current changes so that you can apply them later?
  • Git stash save "My Changes"
  1. You have created a new commit locally, but when you try to push it to the remote repository, you get an error message saying that there are unmerged changes. What should you do first?
  • Fetch and merge any new changes from the remote repository using git pull command.
  1. You want to amend your most recent commit with a new message. How would you do this?
  • Use the git commit --amend command followed by a new commit message: git commit --amend -m "Your updated commit message"
  1. Suppose you have committed some sensitive information accidentally in your Git repository. How can you remove these files from the Git history?
  • You can use git filter-branch or git-filter-repo to remove sensitive files from the Git history.
  1. Your teammate has pushed changes to a shared Git repository, but you are unable to pull their changes because of merge conflicts. What should you do to resolve these conflicts?
  • First, fetch the latest changes using the git pull command: git pull origin branch_name. Then, manually resolve any merge conflicts by editing the affected files and committing the resolved changes. Finally, push your changes to the remote repository.

FAQ

Q: What is the difference between git add and git commit?

A: git add stages (prepares) changes for committing, while git commit creates a new commit with the staged changes and saves it in Git history.

Q: Can I undo a commit in Git?

A: Yes, you can undo a commit using git revert, git reset, or git cherry-pick --reverse. Each command has its own use case, so choose the one that best suits your needs.

Q: What is a git stash?

A: A git stash saves your uncommitted changes and allows you to apply them later when needed. This is useful when you need to switch branches or work on something else temporarily without losing your current work.

Committing and pushing (Git & Dev Tools) | Git & Dev Tools | XQA Learn