The Wayback Machine - https://web.archive.org/web/20240930220418/https://www.geeksforgeeks.org/useful-git-commands-and-basic-concepts/
Open In App

Useful Git Commands and Basic Concepts

Last Updated : 25 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Git is a powerful version control system widely used in software development to track changes, collaborate on projects, and manage code efficiently. Understanding the basic concepts and commands in Git is important for any developer. This article will cover the fundamental concepts and some of the most Useful Git Commands and Basic Concepts to help you get started.

Basic Concepts

1. Repository (Repo)

A repository is a storage space where your project’s files and their revision history are kept. It can be local (on your computer) or remote (on a server).

2. Commit

A commit represents a snapshot of your repository at a specific point in time. Each commit has a unique SHA-1 hash identifier and includes a message describing the changes.

3. Branch

A branch is a parallel version of your repository. It allows you to work on different features or fixes without affecting the main codebase. The default branch in a new repository is usually called master or main.

4. Merge

Merging is the process of combining changes from one branch into another. It helps integrate the work done in different branches.

5. Clone

Cloning is the process of creating a copy of a remote repository on your local machine.

6. Remote

A remote is a common repository that all team members use to exchange their changes.

7. Staging Area / Index

The staging area (or index) is where you prepare changes to be committed. It allows you to review and organize changes before making a commit.

8. Working Directory

The working directory is where you modify your files. Changes in the working directory are not tracked by Git until you stage them.

Setting up our Git Repository

Let’s make a Git Repository first of all and learn about all these Git commands.

Let’s start by creating a folder where we can have different files like a text file, an Excel Database file, a Markdown File, a Folder consisting of Source Code files (An actual Project may have many different files).

But right now for simplicity’s sake let’s keep these files empty.

Image

We can use Git Bash to write Git commands or we can also use VS Code which has built-in support for Git. You can install Git here or you can check out How to integrate Git Bash with Visual Studio Code?. If you are using Git for the first time to know how to set up Git Bash.

Now let’s open Git Bash by Right-clicking and selecting “Git Bash Here”.

Image

Initializing our Git Repository

Here we are initializing our Git Repository or you can say we are making our directory a Git Repository.

Image

Note: If you already have made a Git Repository then don’t use this command again else all the changes that you have made to this Repository will get re-initialized.

Staging Files Initially

We are staging these files initially so that now we can track them using Git.

Image

Creating Initial Commit

Let’s make our Initial Commit to our Repository.

Image

Now let’s take a look at some of the useful Git commands for merging branches, forking a Repository, Renaming, and Deleting files using the command line, and much more.

Useful Git Commands

1. git init

Initializes a new Git repository in the current directory.

git init

2. git clone

Creates a copy of an existing remote repository.

git clone <repository-url>

3. git status

Displays the state of the working directory and the staging area. It shows which changes have been staged, which haven’t, and which files are not being tracked by Git.

git status

4. git add

Adds changes from the working directory to the staging area.

git add <file>

To add all changes:

git add .

5. git commit

Records the changes in the staging area in the repository with a descriptive message.

git commit -m "Your commit message"

6. git log

Shows the commit history for the current branch.

git log

7. git branch

Lists all branches in the repository. The * indicates the current branch.

git branch

Creates a new branch:

git branch <branch-name>

8. git checkout

Switches to a different branch or restores files in the working directory.

git checkout <branch-name>

9. git merge

Merges changes from one branch into the current branch.

git merge <branch-name>

10. git pull

Fetches changes from a remote repository and merges them into the current branch.

git pull origin <branch-name>

11. git push

Uploads local commits to a remote repository.

git push origin <branch-name>

12. git remote

Manages the set of tracked repositories.

To add a new remote:

git remote add <name> <url>

13. git fetch

Downloads objects and refs from another repository.

git fetch

14. git reset

Resets the current HEAD to a specified state. It can be used to unstage changes or move the branch pointer.

To unstage changes:

git reset <file>

15. git diff

Shows changes between commits, commit and working tree, etc.

To see changes in the working directory:

git diff

To see changes between the working directory and the index:

git diff --cached

16. git stash

Temporarily shelves changes in the working directory that are not ready to be committed.

git stash

To apply stashed changes:

git stash apply

17. git tag

Creates a tag to mark a specific point in the repository’s history, typically used for releases.

git tag <tag-name>

18. git blame

Shows who made changes to each line in a file.

git blame <file>

19. git reflog

Records updates to the tip of branches. It’s useful for recovering lost commits.

git reflog

20. git rm

Removes files from the working directory and the index.

git rm <file>


Previous Article
Next Article

Similar Reads

Basic Git Commands with Examples
Nowadays open source contribution is on the peak. It's getting so much popular among students and there are also some open-source programs and competitions to learn so much about development. Open source is a great way to learn and make a better community. There are so many organizations where one can contribute and learn. For that one have to know
3 min read
Difference Between Git remote prune, Git prune and Git fetch --prune
Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
4 min read
Most Confusing GIT Concepts
Git is an important tool for modern software development, offering powerful features for version control and collaboration. However, certain concepts in Git can be quite confusing, especially for beginners. In this article, we will see some of the most confusing Git concepts, providing clear explanations and practical examples. Table of Content 1.
3 min read
Simple and Concise Git Commands That Every Software Developer Should know
As developer git is the most useful arena which is not operated directly so do we have git commands that become very crucial that one should know after having git installed in the local system. if not you must download git from here Download Git. If you need any help with the further installation process. So, Now let's start with the basic commands
4 min read
Essential Git Commands
When it comes to commands, there come numerous commands in a software developer head or one that is mastering over git but even only a few of them are used frequently in the enterprising domains that are used frequently by developers in order to boost workflow. So here we will be listing a couple of them prior to dividing them into categories in or
3 min read
Automating some git commands with Python
Git is a powerful version control system that developers widely use to manage their code. However, managing Git repositories can be a tedious task, especially when working with multiple branches and commits. Fortunately, Git's command-line interface can be automated using Python, making it easier to manage your code and automate common tasks. One p
3 min read
How to Use Git Commands in Android Studio Terminal?
Android Studio is the official IDE (Integrated Development Environment) for Android app development and it is based on JetBrains’ IntelliJ IDEA software. Git is a Distributed Version Control System. It is used in Software Development. Git was developed by Linus Torvalds in the year 2005 to manage the development of the Linux Kernel Need of Git Git
2 min read
How to Push Folders From Local PC to GitHub using Git Commands?
Git offers a powerful command-line interface for managing code repositories, understanding how to push folders from your local PC to GitHub repositories using Git commands can enhance your workflow and streamline the process of sharing code with collaborators or deploying projects. In this article, we'll see the step-by-step process of pushing fold
2 min read
All Git Commands You Should Know
Git is a distributed version control system (VCS) that allows multiple developers to work on a project at a time, giving the possibility of changing each other's work based on requests and authorizations. Created by Linus Torvalds in 2005 for the development of the Linux kernel, Git has since become the most widely used version control system in th
8 min read
How to Dry Run Git Commands?
Git is used for version control in software development. Understanding the effects of Git commands before executing them can help prevent mistakes and ensure that your repository remains in a good state. This is where "dry running" Git commands become incredibly useful. A dry run simulates the execution of a command without making any changes, allo
3 min read
Top 15 Git Commands to Become a Pro Developer [2024]
Git is a distributed version control system and open-source software used by developers to manage different versions of source code by tracking changes in files. It is used by developers to work together on the same project from anywhere. Whether you are a professional developer or just starting your coding journey, Git is essential for becoming a
8 min read
How to Generate Public SSH or GPG Keys Using Git Commands?
Generating public SSH or GPG keys is important for securing your Git operations and ensuring the authenticity of your commits. In this guide, we'll walk you through the process of generating these keys using Git commands, making it easy to track along and understand. What Are SSH Keys?SSH (Secure Shell) keys are a pair of cryptographic keys used to
2 min read
How to Use Git Shell Commands?
Git is a powerful version control system that is important for managing source code in modern software development. While there are many graphical user interfaces (GUIs) available for Git, mastering Git shell commands can significantly enhance your productivity and control over your repositories. This article provides an overview of essential Git s
3 min read
Top 12 Git Commands for Every Developer
Git is a distributed version control system and open-source software. It enables developers to manage many versions of a source code with ease. You can use it to figure out who did what, when, and why. Git has become a must-have tool for any developer nowadays, and knowing Git commands is required for developers to fully utilize Git. There are hund
9 min read
Redux - Basic Understanding of the Concepts for Beginners
Redux is one of the most confusing and probably difficult topics for someone who is trying to learn it from scratch. But why?? Are the number of boilerplates or the terminologies in Redux the reasons that turned you off from learning it? You are searching for a Redux guide, you read some blogs/tutorials, and you watch some YouTube videos but things
15+ min read
React JS Basic Concepts Reference
React is a library and not a framework. It was developed by Facebook to address their specific challenges. Understanding a new library or framework can be exciting, but it's important to first grasp its core concepts.  Example: In this example, we have implemented a basic react app with the hello world output. C/C++ Code import React from 'react';
3 min read
Difference between “git add -A” and “git add”
When working with Git, the git add command is used to move changes from your working directory to the staging area. However, there are different options and flags that can be used with git add, such as -A, which might lead to some confusion. In this article, we'll explore the differences between git add -A and git add. 1. Git add- CommandThe git ad
2 min read
Git - Difference Between Git Revert, Checkout and Reset
Git offers a range of commands to manage and manipulate your codebase. Among these commands, git revert, git checkout, and git reset are frequently used for different purposes. Understanding the differences between these commands is important for effective version control. In this article, we'll explore the functionalities and use cases of git reve
6 min read
Difference Between "git commit" and "git push"?
Git commit and git push are two essential commands you'll use a lot when working with Git. even their frequent use together, they have different functions. In order to help you understand when and how to use these two commands effectively in your version control workflow, this article will break down their differences. What is git commit?git commit
2 min read
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch and git pull is important for effective version control in Git. Git Fetch and Git Pull are two important commands in Git that help in managing remote repositories. While both commands involve retrieving data from remote repositories, they serve distinct purposes and are used in different contexts. In t
5 min read
Difference Between Git Push Origin and Git Push Origin Master
Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
3 min read
Git - git-show Command Line Utility
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be
3 min read
Git LFS: Managing Large Files in Git Repositories
Git, undoubtedly one of the most popular version control systems, is widely used for managing source code in software development projects. However, one of its limitations is its handling of large files. Traditional Git repositories struggle to efficiently manage large files, leading to bloated repositories and slow performance. This is where Git L
4 min read
Git Subtree vs. Git Submodule
Git Subtree and Git Submodule are both mechanisms in Git that allow you to incorporate external repositories into your own repository. They provide a way to manage dependencies and include code from other projects while keeping your repository organized and maintainable. Git Subtree: Git Subtree allows you to insert a separate repository as a subdi
4 min read
How to Set Up Git Using Git Config?
Git is a powerful version control system that helps developers manage and track changes in their code. Setting up Git correctly is important for maintaining an organized and efficient workflow. In this article, we'll walk you through the process of setting up Git using the git config command. PrerequisitesBefore we dive into the setup process, make
3 min read
How To Exit Git Log or Git Diff?
When working with Git, the git log and git diff commands are important for viewing commit history and changes between commits, respectively. However, if you're new to Git, you might find yourself stuck in these views, unsure how to exit. This article will guide you through the steps to exit git log or git diff, ensuring a smooth experience while na
2 min read
How to Fix Git Error "Unable to create '/path/my_project/.git/index.lock'"?
The Git error "Unable to create '/path/my_project/.git/index.lock'" typically occurs when Git is unable to create or write to the index.lock file. This file is used to prevent simultaneous processes from modifying the repository, which could lead to corruption. Table of Content Remove the Lock File ManuallyUse Git Command to Remove the Lock FileRes
2 min read
Git - git prune
Git prune is used to remove the unreachable objects and clear up the local repository. Git is a powerful version control system that helps developers manage their code efficiently. However, as your project evolves, you may accumulate unnecessary data that clutters your repository. This is where git prune comes in view. Table of Content What is git
6 min read
Git For Non-Developers: How Different Industries Are Adopting Git?
While Git is widely recognized for its role in software development, it's increasingly being adopted by non-developers across various industries. This shift is driven by the growing need for version control, collaboration, and efficient project management in fields like marketing, design, data science, education, and even legal and finance sectors.
8 min read
Git vs. Other Version Control Systems: Why Git Stands Out?
Version control systems (VCS) are an essential tool for developers, enabling them to track changes, collaborate on projects, and manage codebases efficiently. Among the various VCS options available, Git has emerged as the dominant choice, used by millions of developers and organizations worldwide. While other systems like Subversion (SVN), Mercuri
8 min read
Article Tags :