The Wayback Machine - https://web.archive.org/web/20240822232057/https://www.geeksforgeeks.org/working-on-git-bash/
Open In App

Git Bash

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

Git Bash is an application that provides a Git command line interface (CLI) emulation for Microsoft Windows. It allows users to run Git commands and interact with a repository, as well as offering Unix command line features. Essentially, Git Bash brings the powerful functionalities of Unix-based systems to Windows, making it easier for developers to manage and control their projects.

What is Git?

Git is a distributed version control system (DVCS) created by Linus Torvalds in 2005. Unlike traditional version control systems that use a central repository, Git allows every developer to have a full-fledged repository on their local machine, complete with the project’s entire history. This decentralized approach offers greater flexibility and speed, especially in collaborative environments.

What is Git Bash?

Git Bash is a command-line interface (CLI) application for Windows that lets you communicate with Git, the version control system. Clone the repositories, commit changes, push and pull changes, and more are all possible using Git Bash. Git Bash can automate manual tasks with the scripts written by you. Git Bash helps you in a greater way to learn about Git and version control.

Why Use Git Bash?

  1. Compatibility: Git Bash is designed to be fully compatible with Git, making it a crucial tool for developers who use Git for version control.
  2. Powerful Command Line Tools: It provides a range of Unix command line tools, which are often preferred by developers for their flexibility and power.
  3. Ease of Use: With Git Bash, you get a familiar Bash experience on Windows, which is particularly useful for those who are accustomed to Unix/Linux environments.

Git GUI

Git is a version-control system it is available in the form of a Graphical User Interface (GUI) and a Command-Line Interface(CLI). Graphical User Interface (GUI) makes the tasks easier when compared to the Command-Line Interface(CLI) most common tasks which are performed in Git that are committing changes, creating branches, and merging changes. Git GUI clients are available, each with its own strengths and weaknesses.

  1. Sourcetree.
  2. GitHub Desktop
  3. TortoiseGit
  4. GitKraken

Choose any one of the Git GUIs according to your needs and preferences. If you are a beginner use simple Git GUI and for advanced you can use customized GUI.

​How to Install Git Bash?

Follow the steps given below to install Git Bash on Windows:

Step 1: The .exe file installer for Git Bash can be downloaded from “https://gitforwindows.org/” Once downloaded execute that installer, following window will occur.

GitInstall

Git Bash Installation

Step 2: Select the components that you need to install and click on the Next button.

git components to be installed

Install Git Bash

Step 3: Select the path where you want to install git as shown in the image below.

Adjusting the path

Install Git Bash

Step 4: Let the installation process finish to begin using Git Bash. To open Git Bash navigate to the folder where you have installed the git otherwise just simply search in your OS for git bash.

Installing Git

Install Git Bash

Git Opened

Install Git Bash

Basic Git Bash Commands

Here are some fundamental Git Bash commands to get you started:

  1. Navigating Directories:
    • ls: Lists files and directories in the current directory.
    • cd <directory>: Changes the current directory to the specified directory.
    • pwd: Prints the current working directory.
  2. Managing Files and Directories:
    • touch <file>: Creates a new file.
    • mkdir <directory>: Creates a new directory.
    • rm <file>: Removes a file.
    • rmdir <directory>: Removes an empty directory.
  3. Using Git Commands:
    • git init: Initializes a new Git repository.
    • git clone <repository>: Clones an existing repository.
    • git status: Displays the status of your working directory and staging area.
    • git add <file>: Adds a file to the staging area.
    • git commit -m "message": Commits the changes in the staging area with a message.
    • git push: Pushes changes to a remote repository.
    • git pull: Pulls changes from a remote repository.
    • git log: “git log” command will help you to see the logs of all the commits made. 
    • git branch: Creates a new branch. 
    • git merge: For merging the changes from one branch to another branch. 
    • git config: “git config” will help you to configure the username and email id.
    • git tag: It will display the all tags.

How to use Git Bash?

Git Bash is a powerful tool that allows you to interact with the Git version control system in a command-line environment on Windows. Let’s go through the basics of How to use Git Bash

Step 1: Configuring Git

Set your global username/email configuration

Open Git Bash and begin creating a username and email for working on Git Bash.

Set your username:

git config --global user.name "FIRST_NAME LAST_NAME"

Set your email address:

git config --global user.email "MY_NAME@example.com"

cd command refers to the command line change directory and is used to get into the desired directory. To navigate between the folders the cd command is used

cd folder_name

ls command

ls command is used to list all the files and folders in the current directory.

ls

Open Git Bash and change the current working directory to your local project by use of the cd command. 
 

Change Working Directory

Step 2: Commit Repository in Git Bash

Initialize the local directory as a Git repository.

 git init 

Stage the files for the first commit by adding them to the local repository

git add .

By “git status“ you can see the staged files after that Commit the files that you’ve staged in your local repository.

git commit -m "First commit"

Now After the “git status” command, it can be seen that nothing to commit is left, Hence all files have been committed.

Step 3: Initializing a Local Git Repository

Follow the steps given below to initialize your Local Repository with Git.

Open GitHub through the internet and click on create new repository Give a suitable name for your repository and create the repository.

Creating New Repository

Initiailzed Git Repo

Note: You can choose to initialize your git repository with a README file, and further, you can mention your project details in it. It helps people know what this repository is about. However, it’s absolutely not necessary. But if you do initialize your repo with a README file using an interface provided by GitHub, then your local repository won’t have this README file.

The following will appear after creating the repository

New Repository

Step 4: Connect the local Repository to GitHub

Go to the GitHub repository and in the code, section copy the URL and In the Command prompt, add the URL for your repository where your local repository will be pushed.

git remote add origin repository_URL

Push the changes in your local repository to GitHub.

git push origin master

Here the files have been pushed to the master branch of your repository. Now in the GitHub repository, the pushed files can be seen.

Step 5: Pulling and Pushing Changes to GitBash

Suppose the files are being changed and new files are added to the local repository. To save the changes in the git repository:

Download all the other changes from the remote repository to the local repository.

git pull 

Changes have to be staged for the commit.

git add .

or

git add file_name

Now commit the staged files.

git commit -m "commit_name"

Push the changes.

git push origin master

Pushing Changes to GitBash

New changes can be seen:

Updated Directory

How to Create and Manage Branches in GitBash?

Suppose a team is working on a project and a branch is created for every member working on the project. Hence every member will work on their branches hence every time the best branch is merged with the master branch of the project. The branches make it a version-controlling system and make it very easy to maintain a project source code.

Syntax

  • List all of the branches in your repository.
git branch
  • Create a new branch
git branch branch_name
  • Safe Delete the specified branch
git branch -d branch_name
  • Force delete the specified branch
git branch -D branch_name

To navigate between the branches git checkout is used. To create a new branch and switch on it.

git checkout -b new_branch_name

To simply switch to a branch

git checkout branch_name

After checkout to the branch, you can see a * on the current branch Now the same commit add and commit actions can be performed on this branch also.

new branch

Git Bash

Merging Branches in GitBash

First, reach the target branch

git checkout branch_name

Merge the branch to target branch

git merge new_branc

Cloning Repositories in GitBash

Cloning is used to get a copy of the existing git repository. When you run the git clone command it makes the zip folder saved in your default location

git clone url

This command saves the directory as the default directory name of the git repository To save the directory name as your custom name an additional argument is to be passed for your custom name of the directory

git clone url custom_name

Undoing Commits in GitBash

When there is a situation when you forget to add some files to commit and want to undo any commit, it can be committed again using –amend

Syntax:

git commit --amend

Conclusion

You may manage your code using the Git Bash tool. Because it has a command-line interface, you can use scripts to automate processes. You can use it on Windows, Mac, and Linux because it is a cross-platform application. It is open-source and free. Some of the fundamental and sophisticated Git Bash commands have been discussed in this post. Git Bash’s capabilities for cloning repositories, creating and managing branches, merging branches, and undoing changes have also been addressed.

FAQs On GitBash

What is the Git bash?

GitBash provides users to work on Git command line on Microsoft Windows environment.

Is Git Bash a tool?

Git Bash tool provides command line features in Windows OS.

What is the difference between git and git Bash?

Git is the version control system used to manage source code and project history, while Git Bash is a command-line terminal environment for Windows that provides a way to use Git commands.

Is Git Bash different from GitHub?

Git Bash is a command-line tool for working with Git locally, while GitHub is a web-based platform for hosting and collaborating on Git repositories.



Previous Article
Next Article

Similar Reads

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
How to Run the Java Code in Git Bash?
Git Bash is a command-line interface that provides a Unix-like environment on Windows. You can use it to compile and run Java programs just as you would in traditional terminals on Linux or macOS. Step to Run Java Code in Git Bash:1. Install JavaMake sure you have Java installed on your Windows computer. You can download and install Java from the o
2 min read
How to Integrate Git Bash with Visual Studio Code?
prerequisitesGit Bash is an application that provides Git command line experience on the Operating System. It is a command-line shell for enabling git with the command line in the system. VSCode is a Text editor that provides support for development operations and version control systems. It provides tools for a user to build hassle-free codes. One
2 min read
How to Push Anything to GitHub using Git Bash?
GitHub has become the go-to platform for collaborative software development, offering powerful tools for version control, collaboration, and project management. Git Bash, a command-line interface for Git on Windows, provides a quick way to interact with GitHub repositories. In this guide, we'll learn about the process of pushing anything to GitHub
3 min read
How to Install jq in Git Bash?
jq is a lightweight and flexible command-line JSON processor. It allows you to slice, filter, map, and easily transform structured data. This guide will walk you through installing jq in Git Bash on Windows. Steps to Install jqStep 1: Download jqFirst, you need to download the jq executable for Windows. Follow these steps: Go to the jq download pag
2 min read
How To Change Folder With Git Bash?
Git Bash is a powerful tool that provides a command-line interface (CLI) for Git, allowing users to interact with their repositories in a Unix-like environment. One common task when working with Git Bash is navigating between folders, also known as directories. This article will guide you through the steps to change folders with Git Bash, making yo
2 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
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch and git pull is important for effective version control in Git. These commands are important for managing your repository and collaborating with team members. In this article, Let us look at Git Fetch and Git Pull separately with the help of an example. What is Git Fetch?The Git Fetch command is used t
4 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
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
Git - git prune
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. What is git prune?git prune is a Git command that removes objects that are no longer referenced by any branch or tag
5 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
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
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
Git - Changing History
Git provides a very important option of altering the commit history. As the master of git, you can control and make it function according to you. Git gives you the authority to change the order of commits, change messages, remove commits entirely. Moreover, it also allows you to manage the project's history. However, it is advisable to commit the w
3 min read
Introduction to Git Branch
Branching means diverging from the mainline and continue to work separately without messing with the mainline. Nearly every VCS has some form of branch support. In Git, a branch is simply a reference to the commit, where the following commits will be attached. Git vs SVN: In recent years, the use of git has risen tremendously. Unlike SVN, git allow
3 min read
Git Features
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 which may b
9 min read
Bare Repositories in Git
Repositories in Git are a snapshot of the folder in which you are working on your project. You can track the progress and changes made to the project by making commits and also revert changes if not satisfactory. Repositories can be divided into two types based on the usage on a server. These are: Non-bare Repositories Bare Repositories What is a N
7 min read
Getting changes from a Git Repository
Git allows performing various operations on the Repositories including the local repositories and remote repositories. The user when downloads a project to work upon, a local repository is created to store a copy of the original project. This local repository stores the changes that are being made by the user. This way, the user can edit the change
4 min read
Git | Working with Stash
Pre-requisites: Git Git allows multiple users to work on the same project simultaneously. Suppose a developer is working on a feature in a branch and he needs to pull changes from some other developer's branch or if he has to work urgently on some other feature, but the feature he is currently working on is incomplete. In this case, you cannot comm
8 min read
Handling repositories with Git remote
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. Syncing ability of the git allows the developers to access and work on any project anytime anywhere. The collaboration among developers via git takes place when git provides each individual deve
4 min read
Using Git on CommandLine
Git is very important and powerful tool when it comes to the software development life cycle. Now in this article, we are going to see why git is used vastly in the software industry and what kinds of operations we can perform with git. So git is basically a distributed version control system for tracking changes in source code during software deve
5 min read
Working on BitBucket using Git
Git provides various tools and applications to perform better handling of the work environment. Git is a type of version control system that is used to allow more than one developer to access the source code of a specific application and can modify changes to it that may be seen by other developers. BitBucket is a git-based tool that helps with col
5 min read
Working on Git for GUI
Git has its native environment within the terminal. All the new features are updated first at the command line, and only there is the full power of Git. But plain text isn't the simplest choice for all tasks; sometimes some users are much more comfortable with a point-and-click interface, a visual representation is what they need. Note: There's not
4 min read
Git Rebase
Pre-requisites: Git Git rebase can integrate the changes from one branch to another by overcoming the problems that we might have faced while using the git merge command. The changes we will do will be recorded in the form of logs which are useful to go through if any mistakes happen. What is Branching?Branching means splitting from the master bran
12 min read
Recovering Lost Commits in Git
Git is a very powerful tool, and with great power comes great responsibilities. If not used and handles properly, It might cause you to lose your commits. There might be situations when you may find all of your work missing at once. If you have regularly committed your work, there is a way to recover these lost commits. This tutorial will aim at us
2 min read
Article Tags :
three90RightbarBannerImg