Top 10 Git Commands Every Developer Should Know
Git is the most popular version control system. Today, in fact it’s used by over 90% of developers. Let's learn the most-used Git commands.
8 min read
Software developers tend to work in packs. We pool our creativity (and our code) to build software and our strength in numbers helps reduce the risk of errors. The more people who check code and scan for bugs, the cleaner our final product will be.
However, this places a premium on effective communication. We will end up accessing, reading, writing and changing the same files over and over again, and it’s vital we keep duplication of work to a minimum.
Version Control System(VCS) is great for this. It’s a change management system that records all the changes to files and folders over a period of time. You can go back in history to fetch a specific version of the change you’re interested in, and it allows multiple people to collaborate on the same project without getting in each other’s way (a bit like Google Docs and Google Sheets allow us to collaborate on written documents).
Git is the most popular, most-widely used example of a VCS; today, in fact it’s used by over 90% of developers. However many people don’t know all the commands that
git offers, so they may be missing out on some of its most powerful and beneficial features.
But fear not, coding comrades. In this post, we’re going to take you through the best commands, starting right from the very beginning.
But first, a quick clarification…
Git ≠ GitHub
Some Git rookies may confuse it with GitHub and think both are the same. They are not.
Git is a version control system that helps track file and folder changes. These files and folders finally get pushed to a centralized place called
GitHub is a cloud-based application that helps you manage multiple repositories, and it’s also extremely popular (there are other alternatives to GitHub that serve the same purpose, but GitHub has attracted over 100 million developers).
Ok, on with the tutorial!
Right, let’s start from the top.
You need to install Git on your computer before you start using it. You can install Git on Windows, Linux, and macOS operating systems.
You can find the official Git builds on the Git website. Download the installer from https://git-scm.com/download/win and start the installation.
The easiest way to install Git on Linux is using the
sudo apt install git-all
For RPM-based distributions, you can use the
sudo dnf install git-all
To install Git on macOS, visit the website https://git-scm.com/download/mac and follow the easy steps.
Right, now the installation’s done, let’s go down into the engine room and look at command line interfaces (CLIs)
There are several graphical user interfaces (GUIs) available to carry out Git operations, but CLIs are the most powerful way to interact directly with Git commands. These user interfaces enable you to provide commands as simple lines of text, triggering a command-and-response interaction that is often faster than an alternative GUI.
After installing Git, you can use the terminal or command prompt to execute your commands. And when you install Git for Windows,
Git Bash will make things even simpler by enabling CLIs for you. We assume you’ll be using Git CLI while learning all the commands discussed in this article.
And Now… Onto The Git Commands
We’re going to run through 10 Git commands that are particularly useful when working with your team members. Let’s dive straight in….
1. Configuring Git
git config is a powerful Git command that helps customize Git’s workflow. Git creates a special file called
~/.gitconfig to keep all these configurations, and you can add or override entries using the
git config command.
Let’s start by setting a Git username and email:
git config --global user.name "Tapas Adhikary" git config --global user.email "firstname.lastname@example.org"
You can read back these values as:
git config --list
user.name=Tapas Adhikary email@example.com
When you open the global configuration file
~/.gitconfig, you will see the content saved as:
[user] name = Tapas Adhikary email = firstname.lastname@example.org
2. Initializing a Repository
git init command creates a new Git repository, a
.git subdirectory which sits under the current working directory. This subdirectory contains all required metadata for the new repository.
You can use the
git init command in two ways.
Either change a directory using the
cd command and run
git init to create a Git repository….
Or create an empty Git repository by specifying a directory name using the
git init command.
git init <directory-name>
3. Cloning a Repository
git clone command helps you clone a local or remote repository by copying it into a new directory. You can work on the cloned copy in isolation by cloning the repository locally; after your modifications, you push the changes back to the remote repository.
You can clone a repository using HTTPS or SSH. Here is an example of how HTTPS looks.
git clone <https://github.com/reactplay/react-play.git>
This will clone the
react-play project locally for you. Then you can change to the directory and start working on it.
4. Staging/UnStaging Your Changes
In the Git workflow, you modify the file locally, put it in a special area called
stage and then put it into the project history before you finally push the changes to the remote repository. The staging area is a buffer where you can still do/undo some changes before putting them into the commit history. The
git add command is used to send your file changes to the staging area.
git add <file-name>
git add <directory-name>
If you want to reverse operations and remove some changes before you write to the commit history, no worries: you can use the
git rm command.
git rm <file-name>
5. Status Check
It is always important to check the state of the working directory and the staging area when working with Git. At a bare minimum, this will tell you exactly what is tracked vs untracked and what’s happening with your files.
> git status On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
If you want more detailed and verbose output, please use the
git log command.
6. Committing Your Changes
git commit command commits the files from the staging area and writes to the commit history, launching a text editor to provide a meaningful commit message.
Important: after you provide the message and save the file, all the changes will be committed.
git commit -m "a meaningful commit message"
7. Push to the Remote
git commit command doesn’t push your local changes to the remote repository. For this, you need to use the
git push command. This is how it should look:
git push <remote> <branch-name>
Please note that you can push single or multiple commits to the remote simultaneously.
8. Pulling from the Remote
The reverse of
push is… yep, you guessed it
git pull command pulls the changes from the remote repository to your cloned workspace. With a pull, you can keep your working directory synced with all the changes being pushed to the remote by your team members.
Standard practice is to pull as a first step any time you work on a file and commit your changes.
Merging is a way to combine changes from two branches and integrate them into a single branch.
git merge <branch-name>
When multiple people are working on a target branch, they may want to get each other’s changes. The
git pull won’t work here as these branches may not have merged to the
main remote repository. Git merge combines multiple commit sequences into one history. At times, however, this function may make things difficult to debug.
As an alternative option, you can use
git branch command has several options for working with branches. Using this command, you can:
List all the branches:
Create a new branch with a branch name:
git branch <branch-name>
Delete a specific branch:
git branch -d <branch-name>
Rename a branch:
git branch -m <branch-name>
List all Remote branches (with a marking of the current branch):
git branch -a
Additionally, when you want to switch from one branch to another, you can use the
git checkout <branch-name>
We hope this quick article has given you a decent snapshot of the most-useful Git commands. Remember, thought, that this is just the tip of the iceberg; there are many more commands to handle corner cases, edge cases and other rare but complex situations.
Before we go, though, I’ll leave you with a few additional resources that may help you to explore Git further.
This article was Originally Published on Bugfender.
Bugfenderis a log storage service for application developers. It collects everything happening in the application, even if it doesn’t crash, in order to reproduce and resolve bugs more effectively and provide better customer support. Get started with Bugfender from here.
Before We End...
Let's connect. You can follow me on Hashnode, and I also share tips on Software Development, Content Creation, Open Source, and Career on these platforms.
Did you find this article valuable?
Support Tapas Adhikary by becoming a sponsor. Any amount is appreciated!