DEV Community

Bugfender
Bugfender

Posted on • Originally published at bugfender.com on

Top 10 Git Commands Every Developer Should Know

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.

A 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 repository. 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!

Installation

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.

On Windows

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.

On Linux

The easiest way to install Git on Linux is using the apt.

.sudo apt install git-all
Enter fullscreen mode Exit fullscreen mode

For RPM-based distributions, you can use the dnf.

sudo dnf install git-all
Enter fullscreen mode Exit fullscreen mode

On macOS

To install Git on macOS, visit the website https://git-scm.com/download/mac and follow the easy steps.

Git CLI

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 "tapas@someemail.com"
Enter fullscreen mode Exit fullscreen mode

You can read back these values as:

git config --list
Enter fullscreen mode Exit fullscreen mode

Output:

user.name=Tapas Adhikary
user.email=tapas@someemail.com
Enter fullscreen mode Exit fullscreen mode

When you open the global configuration file ~/.gitconfig, you will see the content saved as:

[user]
    name = Tapas Adhikary
    email = tapas@someemail.com
Enter fullscreen mode Exit fullscreen mode

2. Initializing a Repository

The 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….

git init
Enter fullscreen mode Exit fullscreen mode

Or create an empty Git repository by specifying a directory name using the git init command.

git init <directory-name>
Enter fullscreen mode Exit fullscreen mode

3. Cloning a Repository

The 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>
Enter fullscreen mode Exit fullscreen mode

This will clone the react-play project locally for you. Then you can change to the directory and start working on it.

cd react-play
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Also,

git add <directory-name>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

For output:

> git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
Enter fullscreen mode Exit fullscreen mode

If you want more detailed and verbose output, please use the git log command.

6. Committing Your Changes

The 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"
Enter fullscreen mode Exit fullscreen mode

7. Push to the Remote

The 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>
Enter fullscreen mode Exit fullscreen mode

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 pull.

The 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.

git pull
Enter fullscreen mode Exit fullscreen mode

Standard practice is to pull as a first step any time you work on a file and commit your changes.

9. Merging

Merging is a way to combine changes from two branches and integrate them into a single branch.

git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

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 rebase.

10. Branching

The git branch command has several options for working with branches. Using this command, you can:

List all the branches:

git branch
Enter fullscreen mode Exit fullscreen mode

Create a new branch with a branch name :

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

Delete a specific branch :

git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

Rename a branch :

git branch -m <branch-name>
Enter fullscreen mode Exit fullscreen mode

List all Remote branches (with a marking of the current branch):

git branch -a
Enter fullscreen mode Exit fullscreen mode

Additionally, when you want to switch from one branch to another, you can use the checkout command:

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

Before We Go…

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.

Top comments (0)