Version Control with Git: Essential Command Line Commands

Written by Web Hosting Expert

November 5, 2025
Version Control with Git: Essential Command Line Commands

Version control helps teams track and manage changes to software projects, ensuring smooth collaboration and safe rollbacks when needed. Among the available tools, Git is the most widely used due to its speed, flexibility, and robust branching model.

While many developers use visual interfaces, mastering Git from the command line gives you full access to its capabilities, offering greater precision, speed, and control, especially for complex workflows or resolving conflicts.

This guide will give you a solid grasp of essential Git commands, helping you move confidently from setup to collaboration and troubleshooting.

TL;DR:
Mastering Git from the command line gives developers full control over version tracking, collaboration, and rollback workflows. This guide covers everything from installing Git and configuring .gitignore to using key commands like git init, git add, git commit, git branch, and git merge. It also explores productivity tools such as git stash, cherry-pick, and aliases to speed up your workflow. With strong Git command-line skills, developers can manage branches efficiently, resolve conflicts faster, and maintain clean, organized project histories.

Understanding Git and Version Control


What is Version Control?

Version control is a system that records changes to files over time, allowing you to revisit, compare, and restore previous versions of your work. It is an essential tool for managing software development, ensuring that teams can collaborate efficiently, track progress, and recover from mistakes without losing valuable work.

What is Git?

Git is a distributed version control system created by Linus Torvalds in 2005. It allows developers to work independently on different parts of a project while maintaining a complete history of all changes. Unlike centralized systems, Git provides every contributor with a full local copy of the project’s history, enabling faster operations and greater resilience.

Key Benefits of Using Git

  • Collaboration: Multiple team members can work on the same project without overwriting each other’s changes, using branches and merges to coordinate their efforts.

  • History Tracking: Every change is recorded with detailed information, including what was changed, who made the change, and why, making it easy to review and audit project history.

  • Backup: Because every contributor has a complete copy of the repository, projects are inherently protected against data loss.

  • Branching: Git’s branching model allows developers to experiment, develop new features, and fix bugs in isolated environments, merging them back into the main project when ready.

90%

💰 90% OFF YOUR FIRST MONTH WITH ALL VERPEX RESELLER HOSTING PLANS

with the discount code

MOVEME

Use Code Now

Getting Started with Git


Installing Git

Git is available for all major operating systems. Here's how to install it:

1. Windows: Download the Git installer from git-scm.com and follow the installation wizard. During setup, you can configure options like line ending conversions and the default editor.

2. macOS: You can install Git through Homebrew with:

brew install git

Alternatively, Git is included when you install Xcode Command Line Tools:

xcode-select --install

3. Linux: Use your distribution’s package manager:

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install git

Fedora:

sudo dnf install git

After installation, configure Git with your personal details so your commits are properly attributed:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

You can also set a default text editor (e.g., Vim, Nano, VS Code) for Git to use when writing commit messages:

git config --global core.editor "code --wait"

(Replace "code --wait" with your preferred editor.)

To check your current configuration:

git config --list

Setting Up a Basic .gitignore File

A .gitignore file tells Git which files and directories to ignore, helping you avoid tracking unnecessary files like logs, builds, or environment settings.

Create a .gitignore file in the root of your project and add patterns for files you want Git to skip. Example:

# Ignore Python cache
__pycache__/
*.pyc

# Ignore node modules
node_modules/

# Ignore environment variables
.env

You can also find pre-built .gitignore templates for specific languages and frameworks at gitignore.io.

Core Git Command Line Commands


1. Repository Initialization and Setup

  • git init: Initializes a brand-new local Git repository in your current directory. It creates a .git folder to start tracking your project.

git init

  • README.md file: After initializing the repository, it is common to create a README.md file to describe the project. This file helps collaborators quickly understand the purpose and scope of your project.
touch README.md
git add README.md
git commit -m "Add project README
  • git clone [URL]: Creates a local copy of an existing remote repository. Ideal for collaborating on projects hosted on platforms like GitHub or GitLab.

git clone https://github.com/user/project.git

2. Tracking and Saving Changes

  • git status: Displays the status of your working directory and staging area, which files are untracked, modified, or staged for commit.

git status

  • git add [filename] and git add . : Stage changes for the next commit. You can stage individual files or all changes at once:

git add index.html

git add .

  • git commit -m "message": Records a snapshot of the staged changes, adding a descriptive message about what was changed.

git commit -m "Add new homepage layout"

  • git commit --amend: This lets you revise the last commit without creating a new one, keeping your history clean. If you need to modify the most recent commit, such as correcting a message or adding a missed file, you can use;

git commit --amend

3. Branching and Merging

  • git branch: Lists all branches in your repository and highlights the current branch.

git branch

  • git checkout [branch-name]: Switches to another branch.

git checkout feature-login

  • git checkout -b [new-branch-name]: Creates a new branch and immediately switches to it.

git checkout -b feature-signup

  • git merge [branch-name]: Merges the specified branch into your current branch.

git merge feature-login

4. Working with Remote Repositories

  • git remote -v: Displays the list of remote connections associated with the repository, usually showing URLs for fetch and push.

git remote -v

  • git push [remote] [branch]: Uploads your commits from a local branch to a remote repository.

git push origin main

  • git pull [remote] [branch]: Fetches changes from the remote branch and merges them into your current branch.

git pull origin main

5. Undoing and Fixing Mistakes

  • git reset: Unstages files from the staging area or moves commits back to a previous state.

git reset HEAD index.html

The git reset command has multiple modes:

  • --soft: Keeps your changes staged.
  • --mixed: Unstages the changes but keeps them in your working directory (default).
  • --hard: Discards all changes and resets the working directory to match the last commit.

Example: git reset --hard HEAD~1

This command discards the last commit and all changes; use it with caution.

  • git revert [commit]: Creates a new commit that undoes changes from a specific earlier commit, without rewriting history.

git revert 4a5e6f7

  • git checkout -- [filename]: Discards local changes in a file, restoring it to the last committed version.

git checkout -- index.html

6. Viewing Project History

  • git log: Shows a detailed history of commits, including author, date, and message.

git log

  • git log --oneline: Displays a simplified, one-line summary for each commit, great for quick reviews.

git log --oneline

  • git diff: Compares changes between your working directory and the staging area, between commits, or between branches.

git diff git diff main feature-login

Git Commands to Boost Productivity


1. git stash and git stash pop

Sometimes you are in the middle of making changes but need to switch tasks quickly without committing incomplete work. Using git stash saves your uncommitted changes and resets your working directory to the last commit, allowing you to focus on something else.

When you are ready to continue, git stash pop reapplies those changes, making it ideal for multitasking or handling urgent fixes. Alternatively, use git stash apply if you want to reapply the stashed changes without removing them from the stash list.

2. git cherry-pick [commit]

When you want to apply a specific commit from one branch onto another without merging the entire branch, you can use git cherry-pick. For example, running git cherry-pick 4a5e6f7 applies that specific commit to your current branch.

This command is especially useful for selectively moving bug fixes or features without including unrelated changes.

3. git rebase -i

Use interactive rebase to clean up commit history, squash commits, or change commit messages before pushing:

git rebase -i HEAD~3

Setting Up Git Aliases


Typing long Git commands repeatedly can slow you down. Git lets you set up aliases to shorten common commands:

  • To create an alias, use:
git config --global alias.co checkout
      git config --global alias.br branch
      git config --global alias.ci commit
      git config --global alias.st status
  • After setting these, you can use git co, git br, git ci, and git st instead of the full commands. This small tweak can save you significant time over hundreds of operations.

  • To see all your aliases and configurations:

git config --global --list

Best Practices for Using Git Command Line


1. Write Clear and Descriptive Commit Messages

Good commit messages help others and your future self quickly understand the purpose of each change by starting with a summary line of 50 characters or less. If the change is complex, you can add a brief explanation for context.

Using the present tense, such as “Add login feature” instead of “Added login feature,” keeps messages consistent and action-oriented. Clear, well-structured commits improve collaboration and make debugging faster and more efficient.

2. Commit Often but Logically

Frequent commits create a detailed project history that makes it easier to track progress and identify issues as they arise. Instead of committing every minor edit, group related changes into logical commits that represent a single purpose, feature, or fix.

3. Regularly Pull from Remote Before Pushing

Before pushing your work, always pull the latest changes from the remote repository using git pull origin [your-branch]. This keeps you aligned with your team’s updates and helps prevent merge conflicts.

4. Keep Branches Focused and Small

Use branches for specific features, fixes, or experiments rather than combining unrelated changes into one. Small, focused branches make code reviews faster, reduce merge conflicts, and keep the project history clean and easy to follow. Once the work is complete, promptly merge the branch and delete it if it's no longer needed.

From initializing repositories and committing changes to branching, merging, and advanced techniques like rebasing or stashing, Git provides powerful tools for efficient version control. With the right command-line skills, you can streamline development, reduce errors, and collaborate more effectively on any project.

25%

💸 EXTRA 25% OFF ALL VERPEX MANAGED CLOUD SERVERS

with the discount code

SERVERS-SALE

Use Code Now

Conclusion


Mastering Git command line skills is a fundamental step for any developer or team working on software projects. Understanding how to navigate repositories, track changes, manage branches, and collaborate through the command line gives you deeper control, efficiency, and confidence in your workflows.

Like any tool, the more you practice Git commands, the more natural they become. Building muscle memory through daily use, even for small projects, will make you faster, help you avoid mistakes, and prepare you to tackle more advanced Git workflows in the future.

Frequently Asked Questions

How do I check if my local repository is up to date with the remote repository?

You can run the git status command from the command line interface to view the current state of your local repository. It helps confirm whether your branch is ahead, behind, or aligned with the remote repository, ensuring you're working on the most recent version of your git project.

What happens when I create a new branch from an existing branch in a git repository?

When you create a new branch, Git makes an independent line of development starting from the commit history of the existing branch. This allows you to test changes, add features, or fix bugs without affecting the main branch or other parts of the central repository.

Why do I need to run git init command when starting a new git project?

The git init command initializes an empty git repository in your project directory. This sets up the revision control system locally on your computer, allowing you to track changes, manage commits, and push updates to a remote repository when you are ready.

What is the purpose of the staging area in Git before using the commit command?

The staging area is a space where you place changes using git add before recording them in the next snapshot with a commit command. It lets you review and group edits carefully, ensuring only the selected updates are saved in the project history on your local machine.