Essential Git Commands and Basic Concepts
09:42, 26.05.2026
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.