Git Bash
Overview
Git Bash is an application that provides a Git command line interface based on Unix shell utilities and experience. Git Bash installs some common bash utilities along with Bash. Commonly used commands include git commit, git add, git branch, git clone, etc.
What is Git Bash?
Git Bash is a command line prompt for using Git on our personal computers. Git is a version control system that allows developers to work collaboratively on a project.
Git Bash is a Command line application software that provides Unix-based shell utilities and experiences for Git commands. Bash stands for Bourne Again Shell.
Git Bash is known to be built for Microsoft Windows OS environments as it provides Unix-based shell utilities. macOS and Linux operating Systems already have a built-in terminal shell that supports Unix-based commands. Whereas Microsoft Windows Operating System doesn't support Unix-based command line terminals.
Git Bash is now available for all operating systems whether it is Windows, Linux, or Mac OS. It provides command-line features to end users. Let us first see how we can install it on our systems.
How to Install Git Bash?
To install Git Bash on your PC, follow the step-by-step process given below.
Step 1
The very first step of installing Git Bash is downloading the latest version of Git for the specific operating system. To download the Git Bash visit here.
Here there are 3 options available to choose from i.e. Windows, Linux/Unix, or macOS. Depending on the operating system of the PC, select the appropriate link.
Step 2
Next, you will see the page below, this one is for windows. A standalone installer is recommended but all three options can be used to download. Now, install the downloaded .exe file.
Step 3
To open a particular file or folder in Git Bash, right-click on the file and select the Git Bash option as shown below:
The Git Bash screen is as follows, we are not required to locate the folder manually, simply right-clicking on the folder and selecting Git Bash opens the folder in the Git Bash itself.
How to Use Git Bash?
Let us now launch Git Bash from the start menu to start using it. We can also open the folder directly as discussed above by right-clicking on the folder and choosing Git Bash.
Navigate
If you opened Git Bash from the start menu using the cd folder_name command to change the active directory with the local repository workspace. We use cd to open the sub-folder and cd .. to move back to the parent folder.
The ls command is used to list the contents of the folder.
Configuration
Next, we configure the local Git repository with the GitHub using GitHub email and GitHub username.
Syntax to configure username and email address:
You can verify the details using the following commands. It returns the user's name and email.
Initialize
Next, we will be initializing the current folder as a Git repository using the git init command. It initializes an empty Git repository or reinitializes an existing one.
Add
The git add command adds the files to the staging area which is a pre-commit area, all the files that are to be committed are added to the staging area.
Syntax:
The first command adds a single file to the staging area, the next command is used to add multiple files using a single command.
Status
The git status command shows the status of the files in the current repository. The newly created files If files are only modified they are highlighted in red color and if files are modified and added to the staging area it is highlighted in green color.
Syntax:
git status
We have created two files such that one of them is added to the staging area and another is not yet added to the staging area.
Commit
After adding files to the staging index or staging area we need to commit these files to save them permanently. The git commit command is used to commit the changes made in the repository along with its file version.
Syntax:
Git Bash Commands
Let us have a look at a few complex git commands that are helpful with remote as well as local repositories.
Pull
The git pull command is used to fetch changes made to the remote repository and merges them to the local repository. Syntax:
Push
The git push command is used to push the changes made in the local repository to the remote repository.
Syntax:
Branch
The Git Branch command is used to create a personal branch separately to work on a feature without affecting the entire project. This branch can later be merged with the parent branch to implement and save the changes.
Syntax:
git branch [branch-name]
Here we are creating a new branch newSampleBranch.
Checkout
The git checkout command is used to switch between the branches.
Syntax:
Now, we are moving to the newly created branch. Any new changes will be saved to this new branch.
Merge
The git merge command as the name suggests is used when we are required to merge two branches.
Syntax:
Note: You must switch to the parent branch using the git checkout command and then merge the child or the new branch using the git merge command. Here we are first creating a new file on the newSampleBranch. Then we commit the changes on this branch and checkout the master branch. Later on the master branch, we merge the branch using the git merge command.
Clone
The git clone command clones or generates a copy of the existing project on the remote server to the local computer.
Syntax:
Here we are cloning a remote repository in our local computer.
Conclusion
- Git Bash is a Microsoft windows based command line application that provides Unix-like shell utilities and experience.
- Git Bash can be downloaded from Git's website for Windows, Mac OS, and Linux/Unix operating Systems.
- After installation it provides a command line on which we can run commands to create, update, clone, etc.
- Git add, commit, and status commands are used to add the files to the staging area, commit changes and check the status of the files respectively.
- Git clone, push, and pull commands are used to interact with remote repositories.
- Git branch, checkout and merge are git branch commands.