Oct 12, 2023
Most Used Git Commands From Basic to Advanced to Simplify Your Workflow
Using Git commands when developing software or applications helps you simplify repository management and workflow. Whether you’re an individual developer or a team member, learning Git simplifies version control and enhances effectiveness.
This Git commands tutorial offers a list of Git commands along with their purposes and usage examples. It covers the basics and introduces some advanced techniques.
Understanding the Git Workflow
A Git project consists of three major sections: the working directory, the staging area, and the git directory.
The working directory is where you add, delete, and edit files. Then, the changes are indexed in the staging area. After you commit your modifications, a snapshot of the changes will be saved in the directory.
Git can be downloaded from its official site. It is available for Linux or Unix, Windows, and macOS.
If you’re using a Hostinger VPS hosting plan, you can install Git directly to your server by typing one of the commands below depending on your installed OS:
For Debian-based distributions, including Ubuntu:
sudo apt install git
For Fedora-based distributions, such as CentOS:
sudo yum install git
sudo dnf install git
Most Commonly Used Git Commands
For some users, Git may have a steep learning curve. Fortunately, this section will guide you through the most frequently used Git commands in the command line interface (CLI) tool.
If you prefer a graphical user interface (GUI) for writing Git commands, consider using one of the best Git GUI clients.
Let’s explore some essential Git commands and their functionalities.
Basic Git Commands
This command initiates a new Git repository within a directory. Here’s the basic git init usage:
To create a new repository while specifying the project’s name, use the following command:
git init [project name]
This command is used to stage file changes, preparing them for the next commit:
git add file1.txt
Use this command to create a commit message for the changes, making them part of your project’s history:
git commit -m "Add new feature"
This command displays valuable insights into your files’ modifications and staging status.
The basic git log usage lets you view a chronological list of commit history:
This command lets you compare changes between your working directory and the most recent commit. For example, this git diff usage identifies the differences in a specific file:
git diff file1.txt
To compare changes between two commits, use the following:
git diff commit1 commit2
This command removes files from your working directory and stages the removal for the next commit.
git rm file1.txt
Use this command to rename and move files within your working directory. Here’s the Git command to rename a file:
git mv file1.txt file2.txt
To move a file to a different directory, enter:
git mv file1.txt new_directory/
This command configures various aspects of Git, including user information and preferences. For example, enter this command to set your email address for commits:
git config --global user.email "firstname.lastname@example.org"
The –global flag applies the configurations universally, impacting your local repository.
Git Branching and Merging Commands
Use this command to manage branches in your Git repository. Here’s the basic git branch usage to list all existing branches:
To create a Git branch named “feature”, use:
git branch feature
To rename a Git branch, enter this command:
git branch -m branch-name new-branch-name
This command lets you switch between branches and restore files from different commits.
The following is a git checkout usage to switch to an existing branch:
git checkout branch_name
To discard changes to a specific file and revert it to the last commit, use:
git checkout -- file_name
To combine a feature or topic branch into the main Git branch, use this command. Below is an example of the basic git merge usage:
git merge branch_name
This command allows you to apply specific commits from one branch to another without merging an entire branch.
git cherry-pick commit_hash
This command is used to apply changes from one Git branch to another by moving or combining commits. It helps maintain a cleaner commit history:
git rebase main
This command marks specific points in your Git history, such as v1.0 or v2.0:
git tag v1.0
Git Remote Repository Commands
This command creates a copy of a remote repository on your local machine. A basic git clone usage is to clone a repository from GitHub:
git clone https://github.com/username/my-project.git
This command sends your local Git branch commits to a remote repository, updating it with your latest changes.
For example, you want to push changes from the local repository called “main” to the remote repository named “origin”:
git push origin main
This command fetches and integrates changes from a remote repository into your current local branch. Here’s a git pull usage example to pull changes from the master branch:
git pull origin master
To retrieve new commits from a remote repository without automatically merging them into your current branch, use this command:
git fetch origin
This command manages remote repositories associated with your local repository. The basic git remote usage lists the remote repository:
To add a new remote repository, specify its name and URL. For example:
git remote add origin https://github.com/username/origin.git
This command is used to manage separate repositories embedded within a Git repository.
To add a submodule to your main repository, use:
git submodule add https://github.com/username/submodule-repo.git path/to/submodule
Advanced Git Commands
This command is for undoing changes and manipulating the commit history. Here’s a basic git reset usage example to unstage changes:
git reset file1.txt
To store temporary changes that are not yet ready to be committed, use this command:
To see a list of stashes:
git stash list
To apply the most recent stash and remove it from the stash list:
git stash pop
This command is primarily used for identifying bugs or issues in your project’s history. To start the bisecting process, use this command:
git bisect start
Git will automatically navigate you through commits to find the problematic ones using the following:
git bisect run <test-script>
This command determines the author and the most recent change to each file line:
git blame file1.txt
This command logs Git branch changes. It allows you to track your repository’s timeline, even when commits are deleted or lost:
Last but not least, this command removes untracked files from your working directory, resulting in a clean and organized repository:
git clean [options]
The [options] can be customized based on your specific needs, such as -n for a dry run, -f for force, or -d for directories.
Basic Git Commands Cheat Sheet
If you’re just starting out, it can be hard to remember even the essential Git commands. Lucky for you, we’ve created a Git cheat sheet to help you master this tool. Download it or print it out to always have it ready when you’re stuck remembering Git commands.
We have explored a wide range of Git commands essential for effective source control management and seamless code collaboration. Whether you’re an experienced developer or just starting out, mastering these Git commands will elevate your coding journey.
So, apply this knowledge and continue refining your Git commands skill. Good luck!
Basic Git Commands FAQ
What Are the Most Used Git Commands?
There are hundreds of Git commands. Some of the frequently used ones are git config, git clone, git init, git status, git push, git add, git commit, git branch, git pull, git merge, and git stash.
How to Get Started With Git?
Installing Git on your operating system involves downloading its installer from its official website. Next, configure your name and email address using git config. Then, create a new repository with git init and begin adding and committing files using git add.
Can Git Commands Be Customized?
Yes, customizing Git commands allows you to tailor the version control system to your specific workflows and preferences. You can personalize user information, default behaviors, aliases, and more using the git config command.