Basic Git Commands
On this page
- Installing Git
- Create a Repository
- Clone an Existing Repository
- Add Files to the Staging Area
- Create a Commit
- Send Changes to a Remote Repository
- Download Changes from a Remote Repository
- Create and Switch Branches
- Restore a File
- Merge Branches
- Compare Changes
- View Commit History
- Check Repository Status
- Summary
Git is a version control system that helps track changes to files and collaborate on projects. By recording changes over time, Git makes it easier to manage code, revert unwanted modifications, and work with other developers.
This article introduces some of the most commonly used Git commands for everyday work.
Installing Git
Before using Git, install it on your operating system.
Installation packages and instructions are available for:
- macOS
- Windows
- Linux
After installation, you can verify that Git is available by running:
git --version
Create a Repository
To initialize a new Git repository in the current directory, use:
git init
This creates a .git directory that stores the repository’s version history and configuration.
Clone an Existing Repository
To create a local copy of an existing repository:
git clone /path-to-repository
To clone a repository from a remote server:
git clone username@host:/path-to-repository
The repository and its history will be copied to your local machine.
Add Files to the Staging Area
Before changes can be committed, they must be added to the staging area.
To add a specific file:
git add filename
To add all modified files:
git add .
The staging area allows you to choose which changes will be included in the next commit.
Create a Commit
A commit records a snapshot of your changes in the repository history.
git commit -m "Description of the changes"
The message should briefly describe what was modified.
Send Changes to a Remote Repository
To upload local commits to a remote repository:
git push origin master
Depending on your repository configuration, the branch name may be different, such as main.
If a remote repository has not yet been configured:
git remote add origin repository-address
Download Changes from a Remote Repository
To retrieve and merge the latest changes from a remote repository:
git pull
This command combines:
git fetch
and
git merge
into a single operation.
Create and Switch Branches
To create and switch to a new branch:
git checkout -b feature_x
Branches allow you to work on features or fixes independently of the main codebase.
Restore a File
To discard local changes and restore a file to its last committed version:
git checkout -- filename
This removes any uncommitted modifications to the specified file.
Merge Branches
To merge another branch into the currently active branch:
git merge branch-name
For example:
git merge feature_x
This combines the changes from feature_x with your current branch.
Compare Changes
To view differences between files and the staging area:
git diff
This command highlights added, removed, and modified lines.
It is useful for reviewing changes before creating a commit.
View Commit History
To display the repository’s commit history:
git log --pretty=oneline
This shows each commit on a single line, including the commit identifier and message.
Check Repository Status
One of the most frequently used Git commands is:
git status
This displays:
- Modified files
- Staged files
- Untracked files
- Current branch information
It is often the first command used when checking the state of a repository.
Summary
Git provides tools for tracking changes, managing project history, and collaborating with other developers. Common commands such as git init, git clone, git add, git commit, git push, and git pull form the foundation of most Git workflows and are useful for projects of any size.
Didn't find an answer?
Our support team is here for you 24/7 – write to us and we will help you sort it out.