Back to Git & Dev Tools
2026-03-258 min read

Appendix C: Git Commands (Git & Dev Tools)

Learn Appendix C: Git Commands (Git & Dev Tools) step by step with clear examples and exercises.

Title: Mastering Git Commands (Git & Dev Tools) - A full guide for Developers

Why This Matters

In the realm of software development, version control is an indispensable aspect that facilitates efficient management of codebase modifications. Git, a popular open-source distributed version control system, has become the industry standard due to its flexibility and powerful features. This lesson aims to equip you with essential Git commands that every developer should master for smooth collaboration, productive coding, and effective bug fixing.

Prerequisites

Before diving into Git commands, it is crucial to have a basic understanding of the following:

  1. Command Line Interface (CLI): Familiarity with navigating and executing commands in a terminal or command prompt.
  2. File System Navigation: Knowledge of how to navigate through directories and files on your system.
  3. Basic Understanding of Version Control Systems: Awareness of the importance of version control, its benefits, and how it helps manage code changes.
  4. Text Editor: Familiarity with a text editor such as Vim, Emacs, or Visual Studio Code to create and modify files within your projects.

Core Concept

Git is a distributed version control system that enables developers to track modifications made to their codebase, collaborate with others, and manage multiple branches and versions of their projects. Its key features include:

  1. Version Control: Git maintains a record of every change made to the codebase, allowing developers to revert back to any previous version if needed.
  2. Branching: Git empowers developers to work on different features or bug fixes independently without affecting the main codebase.
  3. Merging: Once branches are ready for integration, Git facilitates seamless merging of changes from multiple branches into one.
  4. Collaboration: Git makes it easy for teams to collaborate by allowing them to work on the same project simultaneously while keeping their individual changes isolated until merged.
  5. Remote Repositories: Git supports remote repositories, enabling developers to easily share and collaborate on projects with others across the globe.
  6. GitHub and Other Hosting Services: Popular hosting services like GitHub, Bitbucket, or GitLab provide additional features such as issue tracking, project management, and collaboration tools that enhance the Git experience.

Worked Example

Let's walk through a simple example of using Git commands:

  1. Initialize a new Git repository in your project directory:
git init
  1. Create a new file named example.txt and add it to the staging area:
touch example.txt
git add example.txt
  1. Commit the changes with a meaningful commit message:
git commit -m "Initial commit"
  1. Create a new branch called feature-branch:
git checkout -b feature-branch
  1. Make some changes to example.txt in the feature-branch.
  2. Stage and commit the changes in the feature-branch:
git add example.txt
git commit -m "Added changes in feature-branch"
  1. Switch back to the main branch (master):
git checkout master
  1. Merge the feature-branch into the master:
git merge feature-branch
  1. Resolve any conflicts that may arise during the merge process.
  2. Push the changes to a remote repository (assuming you have already set up a remote):
git push origin master

Common Mistakes

  1. Forgetting to commit: Failing to commit changes before switching branches or leaving the terminal can lead to lost work. To avoid this, regularly commit your changes using git commit -am "Your message" which automatically stages all modified files.
  2. Ignoring merge conflicts: Merge conflicts are a common occurrence when merging branches, and ignoring them can result in unintended code changes. To resolve conflicts, use a text editor to manually edit the conflicting files and then add them back to the staging area with git add .
  3. Not using descriptive commit messages: Committing changes with vague or unclear messages makes it difficult for others (or even yourself) to understand the purpose of the changes. To write effective commit messages, follow the "5 I's" rule: Informative, Imperative, Individual, Isolated, and In the present tense.
  4. Misusing Git branching: Overuse of branches can lead to unnecessary complexity, while underuse can make collaboration more challenging. To strike a balance, create new branches for significant features or bug fixes, and merge them into the main branch as soon as they are ready for integration.
  5. Not using a .gitignore file: Failing to create and maintain a .gitignore file can result in unwanted files being tracked by Git. To prevent this, create a .gitignore file for your project and add common file patterns or directories that should be ignored.
  6. Not setting up a remote repository: Sharing your codebase with others requires setting up a remote repository on a hosting service like GitHub, Bitbucket, or GitLab. To do this, create an account on the desired platform, initialize a new repository, and then add the remote to your local repository using git remote add origin .
  7. Not pulling regularly: Pulling changes from the main branch helps keep your codebase up-to-date with any new modifications made by others. To pull changes, use git pull origin master.
  8. Not using Git hooks: Git hooks are scripts that run automatically when certain events occur in your repository, such as committing or pushing changes. They can help enforce coding standards, automate tasks, and prevent common mistakes. To set up Git hooks, create a .git/hooks directory in your project and add custom scripts with the appropriate filename (e.g., pre-commit, post-merge, etc.).
  9. Not using Git submodules: Git submodules allow you to include other projects as part of your main project's repository. This can be useful when working on complex projects that require multiple interdependent components. To create a submodule, use git submodule add and then commit the new submodule to your main repository.
  10. Not using Git stash: Git stash allows you to save your uncommitted changes temporarily so that you can switch branches without losing your work. To stash your changes, use git stash and then apply them back when needed with git stash apply.

Practice Questions

  1. What is the purpose of Git?
  2. How does Git handle branching and merging?
  3. Explain the importance of using descriptive commit messages.
  4. What happens when you forget to commit changes before switching branches or leaving the terminal?
  5. Why is it essential to resolve merge conflicts instead of ignoring them?
  6. What is a .gitignore file, and why should you use one in your projects?
  7. How would you create a new branch, make some changes, and merge those changes back into the main branch using Git commands?
  8. What are Git hooks, and how can they be used to automate tasks or enforce coding standards?
  9. What is a Git submodule, and when might it be useful in your projects?
  10. How would you use Git stash to save your uncommitted changes temporarily so that you can switch branches without losing your work?

FAQ

  1. What is the difference between Git and other version control systems like SVN or Mercurial?
  • Unlike centralized systems like SVN, Git is distributed, allowing each developer to have a complete copy of the repository on their machine. This makes it more flexible and efficient for collaboration.
  1. Why should I use Git instead of manually managing my code changes?
  • Manually managing code changes can be error-prone, time-consuming, and difficult to track. Git provides a systematic approach to version control, making it easier to collaborate, manage changes, and revert back to previous versions if needed.
  1. What is the best way to learn Git commands?
  • Practicing using Git with real projects or exercises is the most effective way to learn. Additionally, reading documentation and tutorials can help solidify your understanding of various Git commands and workflows.
  1. How do I handle large files that I don't want to track with Git?
  • You can use a .gitignore file to exclude certain files or directories from being tracked by Git. This helps keep your repository size manageable and focused on the essential parts of your project. Alternatively, you can compress large files before committing them or use external storage solutions like Amazon S3 or Google Cloud Storage.
  1. What is the best way to collaborate with others using Git?
  • Collaborating with others in a Git project involves setting up a remote repository, sharing it with team members, and regularly pulling and merging changes from the main branch. It's also essential to establish clear communication channels for discussing feature requests, bug fixes, and other collaborative efforts. Additionally, consider using collaboration tools like GitHub's pull requests or Bitbucket's merge requests to streamline the review and approval process.
  1. How do I manage conflicts when working on the same files with others?
  • When multiple developers work on the same file, Git will detect conflicts and require manual resolution. To minimize conflicts, establish clear guidelines for naming conventions, coding standards, and branching strategies within your team. Additionally, consider using tools like GitHub's Code Review or Bitbucket's Review Request to facilitate code reviews and reduce the likelihood of conflicts.
  1. How do I handle long-running branches that take a significant amount of time to merge?
  • To manage long-running branches, consider breaking them down into smaller, more manageable pieces. This can help reduce the risk of merging issues and make it easier for others to collaborate on your work. Additionally, consider using Git's rebase feature to clean up your commit history before merging, which can help simplify the merge process and improve code readability.
  1. How do I handle sensitive data or proprietary information in my Git repository?
  • To protect sensitive data or proprietary information, you can use encryption tools like GPG (GNU Privacy Guard) to encrypt files before committing them to your repository. Additionally, consider using Git's filtering mechanisms to exclude specific files or directories from being tracked by Git.
  1. How do I manage multiple repositories for a large project?
  • For large projects with multiple interdependent components, consider using a monorepo (monolithic repository) approach, where all components are stored within a single repository. Alternatively, you can use a multirepo approach, where each component has its own separate repository. In either case, consider using tools like Lerna or Yarn Workspaces to manage dependencies and build processes across multiple repositories.
  1. How do I handle large commits that contain many unrelated changes?
  • To avoid large commits with many unrelated changes, consider breaking your work into smaller, more focused commits. This can help improve code readability, make it easier to track changes, and reduce the risk of merge conflicts. Additionally, consider using Git's interactive rebase feature to squash or reorder commits as needed.
Appendix C: Git Commands (Git & Dev Tools) | Git & Dev Tools | XQA Learn