Essential Git Commands and Basic Concepts

watch 5m, 49s
views 2

09:42, 26.05.2026

Article Content
arrow

  • Core Git Commands
  • Using git add to Stage Changes
  • Working with Branches Using git branch
  • Switching Branches with git checkout
  • Cleaning Up Untracked Files with git clean
  • Cloning a Repository with git clone
  •  Saving Changes with git commit
  • Modifying the Last Commit with git commit --amend
  • Setting Up Configurations via git config
  • Downloading Updates with git fetch
  • Initializing a Repository with git init
  • Viewing Commit History with git log
  • Combining Changes Using git merge
  • Updating Local Branch with git pull
  • Uploading Changes with git push
  • Reapplying Commits Using git rebase
  • Interactive Rebase with git rebase -i
  • Reviewing Git Activity via git reflog
  • H3 – Managing Remotes with git remote
  • Undoing Changes with git reset
  • Reverting Commits Using git revert
  • Checking Repository Status with git status
  • Git Terminology Explained
  • What is a Branch in Git?
  • Understanding the Centralized Workflow
  • What is the Feature Branch Workflow?
  • Forking: Creating Your Own Copy
  • Overview of Gitflow Workflow
  • What is HEAD in Git?
  • Git Hooks and Automation
  • Main Branch Explained
  • What is a Pull Request?
  • Understanding a Git Repository
  • Tags and Version Marking in Git
  • What is Version Control?
  • Exploring the Working Tree

Git is a powerful and widely used distributed version control system that enables developers to track changes, collaborate efficiently, and manage codebases of all sizes. Whether you're new to Git or seeking to reinforce your understanding, mastering its core commands and concepts is crucial for effective version control and collaboration.

Core Git Commands

Git’s power lies in its simplicity and flexibility. Below are the essential commands that every developer should know to work efficiently with any codebase. Each command plays a specific role in your development workflow.

Using git add to Stage Changes

The git add command stages changes in your working directory, preparing them for a commit. This allows you to selectively choose which modifications to include in your next snapshot of the project.

git add <filename>
git add .
  • git add <filename> stages a specific file.
  • git add . stages all changes in the current directory.

Working with Branches Using git branch

Branches in Git allow you to diverge from the main codebase to develop features, fix bugs, or experiment safely.

git branch
git branch <branch-name>
  • git branch lists all local branches.
  • git branch <branch-name> creates a new branch.

Switching Branches with git checkout

The git checkout command lets you switch between branches or restore working tree files.

git checkout <branch-name>

This updates your working directory to match the specified branch.

Cleaning Up Untracked Files with git clean

Over time, your working directory may accumulate untracked files. git clean helps remove these files.

git clean -f

  • -f stands for "force" and is required to execute the clean operation.

Cloning a Repository with git clone

To create a local copy of a remote repository, use:

git clone <repository-url>

This command downloads the entire repository, including its history.

 Saving Changes with git commit

After staging changes, git commit records them in the repository's history.

git commit -m "Your commit message"

A good commit message should be concise yet descriptive.

Modifying the Last Commit with git commit --amend

If you need to adjust your last commit—perhaps to correct the message or include additional changes—use:

git commit --amend

This replaces the previous commit with a new one.

Setting Up Configurations via git config

Configure Git settings such as your username and email:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

These settings are essential for identifying your commits.

Downloading Updates with git fetch

To retrieve updates from a remote repository without merging them into your local branch:

git fetch

This allows you to review changes before integrating them.

Initializing a Repository with git init

Start tracking a new project by initializing a Git repository:

git init

This creates a .git directory, enabling version control in your project folder.

Viewing Commit History with git log

Inspect the commit history of your repository:

git log

This displays a list of commits, including their hashes, authors, dates, and messages.

Combining Changes Using git merge

To integrate changes from another branch into your current branch:

git merge <branch-name>

This merges the specified branch into your current one.

Updating Local Branch with git pull

Fetch and integrate updates from a remote repository:

git pull

This is a combination of git fetch followed by git merge.

Uploading Changes with git push

To share your local commits with a remote repository:

git push

This uploads your changes to the specified remote branch.

Reapplying Commits Using git rebase

Rebase applies commits from one branch onto another, creating a linear history:

git rebase <base-branch>

This can simplify your project's history but requires caution to avoid conflicts.

Interactive Rebase with git rebase -i

For more control over your commits, use interactive rebase:

git rebase -i <base-branch>

This allows you to edit, reorder, squash, or drop commits.

Reviewing Git Activity via git reflog

git reflog records updates to the tip of branches and other references:

git reflog

This is useful for recovering lost commits or understanding branch movements.

H3 – Managing Remotes with git remote

View and manage remote repositories:

git remote -v
git remote add <name> <url>
  • git remote -v lists the current remotes.
  • git remote add adds a new remote repository.

Undoing Changes with git reset

To unstage changes or move the current branch to a different commit:

git reset <commit>

Use with caution, as it can alter commit history.

Reverting Commits Using git revert

To create a new commit that undoes changes from a previous commit:

git revert <commit>

This is a safe way to undo changes without rewriting history.

Checking Repository Status with git status

Check the state of your working directory and staging area:

git status

This shows which changes are staged, unstaged, or untracked.

Git Terminology Explained

Beyond commands, understanding Git's underlying structure and workflows is key to using it effectively.

This section covers core terminology and concepts. It will explain how Git operates and how developers use it in real-world projects.

What is a Branch in Git?

This workflow involves creating a new branch for each feature or bug fix, promoting organized and manageable development.

Understanding the Centralized Workflow

In a centralized workflow, all collaborators commit to a single central repository, simplifying the collaboration process.

What is the Feature Branch Workflow?

This workflow involves creating a new branch for each feature or bug fix, promoting organized and manageable development.

Forking: Creating Your Own Copy

Forking involves creating a personal copy of someone else's repository, enabling you to experiment freely without affecting the original project.

Overview of Gitflow Workflow

Gitflow is a branching model that defines strict roles for branches and provides a robust framework for managing large projects.

What is HEAD in Git?

HEAD is a reference to the current commit in your working directory. It tells Git where you are in the repository's history.

Git Hooks and Automation

Git hooks are scripts that run automatically on specific Git events, such as commits or merges, allowing for automation of tasks like code formatting or testing.

Main Branch Explained

The main branch is the default branch in a repository, representing the production-ready state of your codebase.

What is a Pull Request?

A pull request is a method of submitting contributions to a project. It allows maintainers to review and discuss changes before merging them.

Understanding a Git Repository

A Git repository is a directory that stores your project's files and the entire history of changes made to them.

Tags and Version Marking in Git

Tags are references to specific points in Git history, often used to mark release versions.

git tag <tag-name>

What is Version Control?

Version control is a system that records changes to files over time, enabling you to recall specific versions later.

Exploring the Working Tree

The working tree is the directory where you have checked out your project files. It's where you make changes before staging and committing them.

Mastering these Git commands gives you control over your code. It simplifies teamwork. It keeps your project history clear and organized.

Share

Was this article helpful to you?

VPS popular offers

-10%

CPU
CPU
8 Epyc Cores
RAM
RAM
32 GB
Space
Space
200 GB NVMe
Bandwidth
Bandwidth
Unlimited
Keitaro KVM 32768
OS
CentOS
Software
Software
Keitaro

77.54 /mo

/mo

Billed annually

-7.1%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB HDD
Bandwidth
Bandwidth
Unlimited
wKVM-HDD 4096 Windows

21 /mo

/mo

Billed annually

-9.9%

CPU
CPU
3 Xeon Cores
RAM
RAM
1 GB
Space
Space
40 GB HDD
Bandwidth
Bandwidth
300 Gb
KVM-HDD HK 1024 Linux

4.96 /mo

/mo

Billed annually

-8.4%

CPU
CPU
4 Xeon Cores
RAM
RAM
2 GB
Space
Space
75 GB SSD
Bandwidth
Bandwidth
Unlimited
10Ge-wKVM-SSD 2048 Windows

37.4 /mo

/mo

Billed annually

-10%

CPU
CPU
3 Xeon Cores
RAM
RAM
1 GB
Space
Space
20 GB SSD
Bandwidth
Bandwidth
Unlimited
KVM-SSD 1024 Linux

6.6 /mo

/mo

Billed annually

-10%

CPU
CPU
6 Xeon Cores
RAM
RAM
8 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
Unlimited
MT5 KVM 8192 Windows

29.99 /mo

/mo

Billed annually

-5%

CPU
CPU
3 Xeon Cores
RAM
RAM
1 GB
Space
Space
40 GB HDD
Bandwidth
Bandwidth
Unlimited
wKVM-HDD 1024 Windows

12.1 /mo

/mo

Billed annually

-10%

CPU
CPU
6 Xeon Cores
RAM
RAM
8 GB
Space
Space
200 GB HDD
Bandwidth
Bandwidth
Unlimited
KVM-HDD 8192 Linux

25.25 /mo

/mo

Billed annually

-15.6%

CPU
CPU
2 Xeon Cores
RAM
RAM
512 MB
Space
Space
10 GB SSD
Bandwidth
Bandwidth
1 TB
KVM-SSD 512 Metered Linux

5.33 /mo

/mo

Billed annually

-10%

CPU
CPU
4 Xeon Cores
RAM
RAM
2 GB
Space
Space
30 GB SSD
Bandwidth
Bandwidth
Unlimited
KVM-SSD 2048 Linux

8.3 /mo

/mo

Billed annually

Other articles on this topic

cookie

Accept cookies & privacy policy?

We use cookies to ensure that we give you the best experience on our website. If you continue without changing your settings, we'll assume that you are happy to receive all cookies on the HostZealot website.