IntroductionGit is a version control system that lets developers track source code changes during software development. Git Bash is an application for Microsoft Windows, allowing developers to use Git in a command-line interface.In this article, you will learn what Git Bash is and how to use it.
- A system running Windows
- A network connection
What Is Git BashGit Bash is a Microsoft Windows application with a Git command-line shell experience and utilities, such as Secure Shell Protocol (SSH), Secure Copy Protocol (SCP), CAT (concatenate command), etc. Bash is an acronym for Bourne Again Shell, which is a GNU Project shell.A shell is used to interface with an operating system by executing commands, and Bash is the default shell used on Linux and macOS.
Note: For a detailed overview of basic Git functionalities, read our Beginner's guide for using Git.
What Is Git Bash Used ForGit Bash emulates a bash environment on Windows, allowing users to use the Bash shell and most of the standard Unix commands on a Windows OS. Users can interact with a repository and Git elements by running commands in Git Bash.
How to Install and Set Up Git Bash (A Step-by-Step Guide)Follow the steps below to install and set up Git Bash.
Step 1: Download and Install Git BashFirst, you must install Git on your machine. Follow the steps outlined in the tutorial to download and install Git on Windows systems.
Step 2: Launch Git BashAfter installing Git, search for Git Bash in the start menu. Press Enter to launch the app. Alternatively, to run Git Bash from a location where you want to keep your project files, press the right mouse button and click the Git Bash Here option from the dropdown menu.
Step 3: Connect to a RepositoryThe syntax to configure your local Git installation to use your GitHub credentials is:
git config --global user.name "github_username" git config --global user.email "email_address"
email_addresswith your GitHub credentials. If you already have a repository on GitHub, you can clone the repository to your local machine. Use the following syntax:
git clone [repository_url]
repository_urlin the Code section of your GitHub page: Use it with the
clonecommand: Our Beginner's guide on using Git offers more information on creating a new local repository or repository on GitHub.
How to Use Git BashThe following section explains the basic functionalities of Git Bash and the available commands.
git initcommand creates an empty .git repository or reinitializes an existing one.
git initwhen you already have an existing repository doesn't overwrite your existing files but adds new templates.
cdcommand allows you to change the directory in which Git Bash operates. The syntax is:
For example: If you want to see all the files and subdirectories in the current directory, run:
git statuscommand lists all the modified files ready to be added to the local repository. In this example, the
git statuscommand shows the modified and new files that haven't been added to the index and that Git doesn't track yet. While Git is aware of the files, you have to let Git know you want to track changes to these files.
git addcommand updates the index with the content in the working tree and prepares the content in the staging area for commit.You can add both files and directories to the index. The syntax is:
For example: Here, we see that examplefile.md has been added to the index and is ready for commit.If you have several files ready to be committed, you can use the
git add [directory] | [file]
git add -Acommand to add all the files from the directory that haven't been added to the index yet.
git commitcommand. The
git commitcommand instructs Git to store that file version. Git only commits the changes made in the repository.The syntax is:
For example: In this example, Git commits the examplefile.md, the only change in the repository.
git commit -m "Commit notes"
git pullcommand fetches changes from a remote repository to your local repository.Before running the
git pullcommand, make sure that your central repo is set as origin. Run:
After setting your origin repository, run:
git remote add origin [your-repository-link]
For example: In this example, Git states that everything is already up to date, and there are no new files to add to the local repository.
git pull origin master
git pushcommand is the opposite of the
git pullcommand. This command sends files from the local repository to the remote repository.Run the following command:
git push origin master
For example: In this example, we create a branch named new-feature.When you are working on several branches, you can easily switch between branches. The syntax is:
git branch [branch-name]
git checkout [branch]
[branch]with the branch name you want to switch to.For example:
git mergecommand allows you to merge two branches together.
The syntax is:
Important: Ensure you are on the target (merge-receiving) branch when running the
git mergecommand. Switch to the target branch using
For example: In this example, we merged the new-feature branch into the master branch, adding a new file.
git merge [branch-name]