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

Command-line Git (Git & Dev Tools)

Learn Command-line Git (Git & Dev Tools) step by step with clear examples and exercises.

Title: Mastering Command-line Git for Developers: A full guide to Version Control and Dev Tools

Why This Matters

In the dynamic world of software development, version control is an essential skill that helps manage code changes effectively. Git, a powerful distributed version control system, has become the industry standard due to its robustness, flexibility, and ease of use. Command-line Git offers developers a versatile toolset for handling complex projects efficiently, making it crucial to master this skill.

In interviews, recruiters often ask about your familiarity with Git and command-line tools. Understanding Git's command-line interface can help you tackle real-world bugs, collaborate with other developers, and showcase your problem-solving abilities during the interview process.

Prerequisites

Before diving into Git, it is essential to have a basic understanding of:

  1. Operating System (Linux/MacOS/Windows)
  • Familiarity with file navigation commands like cd, ls, and pwd
  • Text editors such as vi, nano, or vim
  • Basic shell scripting concepts including variables, loops, functions
  • Understanding of directories and files
  1. File Navigation: Learn to navigate your file system using commands like cd, ls, and pwd. These commands will help you move around your project directory structure easily.
  1. Text Editors: Familiarize yourself with text editors such as vi, nano, or vim. These editors are commonly used for editing files directly from the command line.
  1. Basic Shell Scripting: Understanding shell scripting concepts like variables, loops, and functions will help you automate repetitive tasks and write custom scripts to streamline your workflow.
  1. Understanding of Directories and Files: A solid grasp of directories (folders) and files is crucial when working with Git. You should be able to create, delete, rename, and move files and directories using command-line tools.

Core Concept

What is Git?

Git is a distributed version control system designed to track changes in source code during software development. It allows multiple developers to work on the same project simultaneously without overwriting each other's changes.

Key Features:

  • Distributed: Each developer has a full copy of the repository, making it easier to collaborate and work offline.
  • Versioning: Git keeps track of every change made to the codebase, allowing developers to revert to previous versions if needed.
  • Branching: Git allows for multiple branches, enabling developers to work on different features or fixes independently without affecting the main project.

Installing Git

To install Git on your system, follow these steps:

  1. Linux/MacOS: Use package managers like apt (Debian-based distributions), yum (CentOS/RHEL), or Homebrew (MacOS) to install Git.

Debian-based distros

sudo apt-get install git

CentOS/RHEL

sudo yum install git

MacOS with Homebrew

brew install git


2. **Windows:** Download Git for Windows from the official website (https://git-scm.com/download/win) and follow the installation wizard's instructions.

### First-Time Git Setup

After installing Git, you need to configure your user settings:

Set your name and email

git config --global user.name "Your Name"

git config --global user.email "youremail@example.com"


### Getting Help

Use the `git help` command to get more information about Git commands:

Get help on a specific command, e.g., commit

git help commit

Worked Example

In this example, we will create a simple project, make changes, and commit them using Git.

  1. Create a new directory for your project:
mkdir my-project && cd my-project
  1. Initialize the Git repository:
git init
  1. Create a file called main.c with some content, e.g., a simple "Hello, World!" program:
echo "#include <stdio.h>\n\nint main() {\n printf(\"Hello, World!\\n\");\n return 0;\n}" > main.c
  1. Add the file to the Git repository:
git add main.c
  1. Commit the changes with a message:
git commit -m "Initial commit"
  1. Make some changes to main.c, e.g., modify the program to print your name instead of "Hello, World!":
nano main.c
  1. Add and commit the updated file:
git add main.c
git commit -m "Updated main.c"
  1. View the commit history using git log command:
git log
  1. To view more detailed information about a specific commit, use the git show command followed by the commit hash:
git show <commit-hash>
  1. To create a new branch called my-branch, use the following command:
git checkout -b my-branch

Now, you can make changes in this new branch without affecting the main branch. When you're ready to merge your changes back into the main branch, use the git merge my-branch command.

Creating and Switching Branches

You can create a new branch using git checkout -b . To switch between branches, use git checkout .

Staging Changes

When you make changes to files in your Git repository, those changes are not automatically staged for commit. You must explicitly stage the changes using git add before committing them:

Stage all changes

git add .

Stage specific file

git add


### Committing Changes

After staging your changes, you can commit them with a message using the `git commit` command:

git commit -m "Your commit message"

Common Mistakes

  1. Forgetting to add files before committing: Always use git add before committing changes to ensure they are included in the repository.
  1. Committing unfinished work: Never commit half-done or untested code. It's better to create a temporary commit, test it, and then amend or squash the commit if necessary.
  1. Ignoring merge conflicts: When merging branches, Git may encounter conflicts that require manual resolution. Ignoring these can lead to lost changes or inconsistencies in the codebase.

Merge Conflicts Resolution

When resolving merge conflicts, Git will highlight the conflicting lines and allow you to choose which version of the code to keep. You can use a text editor like vim or nano to resolve these conflicts manually. Save the file after resolving the conflict, then stage and commit the changes using Git commands.

Practice Questions

  1. How do you create a new branch in Git?
  2. What command is used to view the commit history?
  3. How would you undo the last commit?
  4. What happens if you forget to add a file before committing changes?
  5. Explain how to resolve merge conflicts in Git.
  6. Describe the difference between git status and git diff.
  7. What is a git rebase, and when would you use it?
  8. How do you view the differences between two specific branches or commits in Git?
  9. What is a git stash, and why might you need to use it?
  10. Explain how to create and manage git tags for versioning purposes.
  11. What does the git merge command do, and when would you use it?
  12. How can you view the differences between two specific versions of a file in Git?
  13. What is the purpose of the .gitignore file, and how can you create one for your project?
  14. Describe the difference between hard links and symbolic links in Git.
  15. What is a git cherry-pick, and when would you use it?

FAQ

  1. What is the difference between Git and SVN (Subversion)?
  • Git is a distributed version control system, while SVN is a centralized one. Git allows for local commits before pushing changes to a remote repository, making it more flexible and efficient.
  1. How do I create a GitHub account?
  • Go to https://github.com/ and click "Sign up" to create a new account using your email address or social media accounts.
  1. What is the purpose of git pull?
  • git pull fetches updates from the remote repository and merges them into your local branch, updating your codebase with the latest changes.
  1. How do I revert to a specific commit in Git?
  • Use the git reset command followed by the commit hash to revert to that specific commit:
git reset --hard <commit-hash>
  1. What is the difference between git checkout and git switch?
  • In older versions of Git, the command was called git checkout. As of Git 2.23, it has been replaced with git switch, which offers a more intuitive and user-friendly syntax for switching branches.
Command-line Git (Git & Dev Tools) | Git & Dev Tools | XQA Learn