15 Basic Git Commands with Examples You Must Know

361
Complete guide with basic git commands

Mastering the basic Git commands is a vital skill for developers in today’s coding world. As the go-to distributed version control system, Git improves team collaboration and provides a solid platform for tracking code alterations. This article will introduce you to 15 core commands that will enhance your efficiency and simplify your workflow. We’ll explore commands that cover a range of Git operations, such as creating and cloning repositories, managing changes, and updating remote repositories, among others.

Before we get deep into the heart of this guide, it’s important to note that a basic understanding of Git’s operations will significantly improve your grasp of the content. We’ll be discussing several git bash commands, which may seem unfamiliar if you’re new to this environment. But, don’t fret! Even if you’re a novice, with a bit of patience and practice, these commands will soon become a part of your coding routine. So, let’s dive in and ease your Git journey with these essential command-line instructions.

What Are Git Commands?

In the realm of software development, Git commands are like the navigational compass guiding developers through the complexities of version control. They are the instructions that enable you to interact and manipulate repositories, providing you with the power to control your versioning system. These commands can be broadly categorized into a git commands cheat sheet, covering various functionalities from basic operations to advanced tasks.

Basic git commands, the focus of this guide, involve operations such as initializing a new repository (git init), tracking and committing changes (git add, git commit), and updating the repository (git push, git pull). 

Meanwhile, git branch commands, another category, help manage different versions of your project simultaneously, allowing you to work on and switch between various features or bug fixes without disrupting the main codebase.

In addition to basic and branch operations, there are git bash commands, which involve using the Git Bash shell – a text-based command-line interface, for executing Git operations. It’s particularly beneficial for developers working in a non-unix environment like Windows.

Understanding and mastering these common git commands can offer tremendous benefits. Firstly, they empower you to manage your project effectively, keeping track of changes and enabling seamless collaboration with other developers. Secondly, it allows you to explore your project’s history and changes over time. Additionally, it provides a safety net for your project, enabling you to revert changes and recover from mistakes when necessary.

In a nutshell, Git commands act as the keys to unlock the full potential of Git, helping you maintain an organized, efficient, and error-free coding environment. Whether you’re an individual developer working on a personal project or part of a large team working on a complex codebase, mastering these commands will significantly enhance your coding prowess.

How Does Git Workflow Operate?

How long does it take to learn a coding language

Git is known for its flexibility, meaning you can create git alias multiple commands to simplify and speed up your workflow. This flexibility extends to the ability to configure settings using git config commands, customizing the behavior of Git to suit your working style.

To effectively leverage git commands, it’s crucial to comprehend the Git workflow’s dynamics. This workflow comprises four main areas:

  1. Workspace: This is your local directory where you make changes to your files. Git recognizes any change made here, but it doesn’t automatically save or track it.
  2. Index (Staging Area): Once changes are made in the workspace, you can use the git add command to move them into the Index, also known as the Staging Area. This step allows for the preparation and review of changes before they are committed.
  3. Local Repository: After being satisfied with the changes in the Index, the git commit command is used to save them into the Local Repository. This action generates a unique ID (SHA-1 hash), which can be used to reference these changes later.

Remote Repository: The Remote Repository is the platform for sharing commits with others or fetching updates from them using git push and git pull commands. These repositories are usually hosted on platforms like GitHub.

Most Used Git Commands

As we delve deeper, it’s crucial to become familiar with its most commonly used commands. These commands represent the bread and butter of Git operations, enabling the user to navigate through the version control system with ease. This section will dissect 15 basic git commands essential in everyday Git usage.

We’ll be breaking down each command, briefly explaining its function, and supporting these explanations with practical examples. This way, you’ll gain a comprehensive understanding of each command’s role in the Git ecosystem. 

The goal is to equip you with a versatile git commands cheat sheet!

From creating a new repository to stashing changes and managing remote repositories, this guide will provide a solid foundation for novice developers and a useful refresher for experienced coders. So, let’s jump in and explore the most used git commands.

git clone

One of the basic git commands crucial in version control is git clone. Its function is to create a local copy of a remote repository. Not only does this command copy the repository, but it also retains the history and versions of every file. Executing git clone requires the URL of the repository you wish to clone. Here’s how you might use it:

git clone https://github.com/username/repository.git

git init

Setting the stage for your project begins with git init. This command creates a new Git repository, transforming a directory into a new Git repository. Here’s the basic usage:

git init

For creating a bare repository, which is typically used for sharing and other developers will clone from, you can use:

git init --bare

git stash

Juggling between different tasks is no sweat with git stash. This command temporarily shelves (or stashes) changes you’ve made to your working copy so you can work on something else and then come back and reapply them later on. Here’s how you stash changes:

git stash

To apply the stashed changes and remove them from the stash list, you can use:

git stash pop

git add

When you’re ready to include changes to a particular file in your next commit, git add steps up to the plate. This command moves these changes to the staging area, though it doesn’t affect the repository until git commit is run. Here’s how it works:

git add filename

git pull

If you need to retrieve and download content from a remote repository while simultaneously updating the local repository to mirror that content, git pull is the command you’re looking for. Essentially a git fetch followed by a git merge. Here’s the basic form:

git pull

git push

Imagine you’ve made some updates to your project, and now you wish to share these changes with others in your team. git push is the command that enables this sharing by uploading your commits to the remote repository. For example:

git push origin master

To push a specific branch, you can change ‘master’ to your branch name:

git push origin your-branch

git commit

Following the staging of your changes with git add, the git commit comes into play. The command captures a snapshot of the project, creating a new commit with a log message from the user to record the modification made. For instance:

git commit -m "Add new feature"

git config

As you start using Git, you must set some basic configuration details, such as your username and email. The git config command is used for this purpose, allowing users to set these configurations on the project or global level. Here’s an example:

git config --global user.name "Your name"
git config --global user.email "youremail@domain.com"

git checkout

The git checkout command is versatile, allowing developers to switch between different versions of a project or even create a new branch entirely. Whether you want to inspect a previous commit or start a new feature branch, git checkout has you covered. Usage:

git checkout branch_name

git remote

Managing connections to remote repositories is critical for any Git workflow. The git remote command shines here. It allows users to create, view, and delete connections to other repositories. For instance, when you want to add a new remote repository, you would use:

git remote add origin https://github.com/username/repository.git

git log

When it comes to tracing the history of a project, git log comes in handy. This command lists the history of commits in a repository, which includes details like author, date, and the commit message. This allows you to track and understand the progression of your project easily. A basic use of this command is as follows:

git log

Furthermore, you can customize the output by using various options. For instance, to get a graph representation of your commits, use:

git log --graph --oneline

git diff

For understanding the differences between commits, unstaged changes, or between your current work and the last commit, git diff is an invaluable command. It displays the changes between two data sets, enabling you to see what has been altered clearly. The following command shows unstaged changes to existing files:

git diff

To see the changes between the working directory and the index (changes ready to be committed), you can use:

git diff --cached

git reset

The git reset command is powerful for undoing changes, allowing you to discard commits in a private branch or uncommitted local changes. It operates in three modes: soft, mixed, and hard, affecting your working directory, index, and HEAD reference. Here is how you can discard the last commit but keep the changes in your working directory:

git reset --soft HEAD^

Alternatively, to discard all uncommitted changes, you can use:

git reset --hard

git status

This one is crucial for understanding the state of your repository. It tells you which changes have been staged, unstaged, or untracked, providing a clear picture of your progress. The basic usage is as follows:

git status

This command is also great for understanding what branch you’re currently on, as well as whether your branch is ahead, behind, or has diverged from the upstream branch.

git show

To dig deeper into the information about a git object, use the git show command. This tool displays details about a commit or the contents of a file at a specific commit. For instance, to display the details of the latest commit, you would use:

git show

You can also use it to show the contents of a specific file at a particular commit like this:

git show <commit>:<filename>

Conclusion

The world of software development is virtually unmanageable without a firm grasp of basic Git commands. These include the likes of git push commands and git add commands, facilitating efficient and effective version control, streamlining your workflow, and boosting productivity.

Indeed, the benefits of understanding and implementing these commands cannot be overstated. For those eager to gain a comprehensive understanding of the topic, consider enrolling in an online course on Git at WildLearner. This course covers all basic Git commands in depth, ensuring you’re well-equipped to handle any Git-related task in your projects.

Unleash the full power of Git with WildLearner and elevate your coding skills to the next level!

Learn coding online for free