Open In App
Related Articles

Git Bash

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Pre-requisite: Git, GitHub

Git Bash is an application that provides Git command line experience on the Windows Operating System. It is a command-line shell for enabling git with the command line in the system. A shell is a terminal application used to interface with an operating system through written commands. Git Bash is a package that installs Bash, some common bash utilities, and Git on a Windows operating system. In Git Bash the user interacts with the repository and git elements through the commands.

What is Git?

Git is a version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goal is to increase efficiency, speed and easily manage large projects through version controlling. Every git working directory is a full-fledged repository with complete history and full version-tracking capabilities, independent of network access or a central server. Git helps the team cope with the confusion that tends to happen when multiple people are editing the same files.

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.

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

 

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

git components to be installed

 

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

Adjusting the path

 

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

 

Git Opened

 

Git Bash Commands

Git Bash comes with a set of commands to perform various operations related to code management and collaboration. Here are some essential Git Bash commands:

  1. git init: Initializes the new git repository. 
     
  2. git clone: “git clone command is used to clone the repositories which are already available in the remote repository. 
     
  3. git add: Files present in the working area will move to the staging area
     
  4. git commit: Files that are available in the staging area will be committed to the local repository. 
     
  5. git status: This command shows the status of the working tree and the staging area.
     
  6. git log: “git logcommand will help you to see the logs of all the commits made. 
     
  7. git branch: Creates a new branch. 
     
  8. git merge: For merging the changes from one branch to another branch. 
     
  9. git config: “git configwill help you to configure the username and email id.
     
  10. 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. 
 

 

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

 

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

 

New changes can be seen:

 

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

 

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

1. What is the Git bash?

Answer:

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

2. Is Git Bash a tool?

Answer:

Git Bash tool provides command line features in Windows OS.

3. What is the difference between git and git Bash?

Answer:

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.

4. Is Git Bash different from GitHub?

Answer:

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.

and



Last Updated : 01 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads