Git is a version control system used by developers to collaborate and manage changes in their applications. It was created for Linux and macOS. But what about Windows? This is where Git Bash for Windows comes in. This tutorial will explain how to install Git Bash on Windows and introduce a few Git Bash commands to interact with the Git repository.

Git Bash installs Git, Bash, and a few basic Bash utilities on Windows. It provides an emulation layer for Git that allows users to run Linux commands at a Windows command prompt.

Install Git Bash on Windows

Follow the steps below to install Git Bash for Windows.

Step 1: Download the latest version of Git Bash.

Step 2: Double-click the downloaded file to start the installation. You will see the Select Destination Location screen.

Defining the Git Bash installation path

Defining the Git Bash installation path

Step 3: Define the path where you want to install Git Bash, and then click Next. You will see the Select Components screen.

Selecting components

Selecting components

Step 4: Select all the components that you want to install, and then click Next. You will see the Select Start Menu Folder screen.

Selecting the Git Bash Start Menu folder

Selecting the Git Bash Start Menu folder

Step 5: Leave the default option if you want to create a shortcut in the Start Menu, and then click Next. You will see the Choosing the default editor used by Git screen.

Choosing the Git default editor

Choosing the Git default editor

Step 6: Select Notepad as the default editor, and click Next. You will see the Adjusting the name of the initial branch in new repositories screen.

Defining the Git default branch

Defining the Git default branch

Step 7: Select the Override the default branch name for new repositories option to create a new branch with the name main. If you select the Let Git decide option, Git creates a new branch with the name master. Then click Next. You will see the Adjusting your PATH environment screen.

Defining the Git PATH environment

Defining the Git PATH environment

Step 8: Select the Git from the command line and also from 3rd-party software option to run the Git command on different consoles such as PowerShell, Windows command prompt, or other third-party consoles. Then click Next. You will see the Choose the SSH executable screen.

Defining the SSH executable

Defining the SSH executable

Step 9: Choose the Use bundled OpenSSH option that comes with Git Bash. Choose the second option if the OpenSSH package is already installed in your system. Then click Next. You will see the Choosing HTTPS transport backend screen.

Choosing HTTPS transport backend

Choosing HTTPS transport backend

Step 10: Select the Use the OpenSSL library option to validate certificates with the default OpenSSL library. Select the second option if your organization uses managed certificates. Then click Next. You will see the Configuring the line ending conversions screen.

Configuring line ending conversions

Configuring line ending conversions

Step 11: Select the Checkout Windows-style, commit Unix-style line endings option because we will be working with text files on Windows. Choose the second option if you are working with an editor that supports Unix-style line endings. The third option causes problems in cross-platform scenarios; I don't recommend it. Now, click Next, and you will see the Configuring the terminal emulator to use with Git Bash screen.

Defining the default terminal

Defining the default terminal

Step 12: Select the Use Mintty (the default terminal of MSYS2) option to run all Git Bash commands. MSYS2 offers a set of tools and provides a Unix-like environment for Windows. If you choose the second option, Git Bash uses the Windows command line as the default console. Then click Next. You will see the Choose the default behavior of 'git pull' screen.

Defining the default Git pull behavior

Defining the default Git pull behavior

Step 13: Select the Default option. The other options become relevant when you are more experienced with Git. Then click Next. You will see the Choose a credential helper screen.

Defining the credential helper

Defining the credential helper

Step 14: Select the Git Credential Manager option and click Next. Git Credential Helper is an external program that prompts you to enter your GitHub username and password when you push changes to the GitHub repository. You will see the Configure extra options screen.

Configuring extra options

Configuring extra options

Step 15: Select the Enable file system caching option to get a quick result after executing the Git Bash commands. Then click Next. You should see the Configuring experimental options screen.

Configuring experimental options

Configuring experimental options

Step 16: Leave both options unchecked as they are in the experimental stage. Then click Install to start installing Git Bash on your Windows system. Once Git Bash has been installed, click Finish to close the installation screen.

Launch the Git Bash console and set global username/email

At this point, Git Bash for Windows is installed on your system. Now you will need to configure a global username and email to start working with Git Bash.

To launch the Git Bash console, use the Windows search bar to search for Git Bash, and click Git Bash to launch the Git Bash console. Then, run the following command to verify the Git version:

git --version

You will see the Git version on the following screen:

Checking the Git version

Checking the Git version

Now run the following commands to define your Git global username and email:

git config --global user.name "hitjethva"
git config --global user.email "hitjethva@gmail.com"

To see the detailed information of your Git Bash configuration variables, run the following command:

git config --list
Viewing the Git configuration information

Viewing the Git configuration information

Initialize a local repository

Before initializing a repository, you will need to create a repository in your GitHub account.

First, open your web browser and log in to your GitHub account.

Then click on the + icon and click New repository. You will see the Create a new repository configuration screen.

Creating the Git repository

Creating the Git repository

Type your repository name and other required information, and click Create repository. Once the repository is created, you will see the following screen:

Verifying the created repository

Verifying the created repository

Next, open the Git Bash console and create a directory for your local repository.

mkdir examplerepo

Next, navigate to the examplerepo directory and create a README.md file in it.

cd examplerepo
echo "# examplerepo" >> README.md

Then, initialize the local repository using the following command:

git init

You should see the following screen:

Initializing the Git repository

Initializing the Git repository

Working with Git Bash commands

In this section, you will learn how to use Git Bash commands to manage local and remote repositories.

Add a file via Git Bash

The git add command allows you to add single or multiple files and other changes that you have made in the current working directory to the staging area. It tells Git that you are now ready to add a file or changes to the next commit.

To demonstrate this tutorial, let's add a README.md file to the local repository using the git add command.

git add README.md

You can now use the git status command to see the state of the working directory.

git status
Checking the Git repository status

Checking the Git repository status

If you want to add all files in the current directory, use the -A option with the git add command.

git add -A

To add all file changes in the current directory, use the "." option with the git add command.

git add .

If you want to choose what changes to add to the file, use the -p option with the git add command.

git add -p

Commit changes via Git Bash

The git commit command creates a snapshot of the staging environment and saves it in the local repository.

First, run the following command to add the README.md staged file to the local repository with a custom message:

git commit -m "added README.md file"

Committing the README.md file

You can now use the git log command to see the commit history of your current branch.

git log
Viewing the commit history log

Viewing the commit history log

If you want to see the full information of the given commit, run the git show command.

git show
Viewing the detailed information of the recent commit

Viewing the detailed information of the recent commit

If you want to change your recent commit message, then use the --amend option followed by a new message.

git commit --amend 'added new README.md file'

To commit all the modified or created files to the local repository, use the -a option with the git commit command.

git commit -a -m "add all modified files to the repository"

If you want to undo all commits and preserve the changes only locally, use the reset --hard option followed by your commit.

git reset --hard [your commit]

Push or upload files to GitHub repository via Git Bash

At this point, you have only a local copy of your repository. To add the local repository to the GitHub repository, you will need to add the URL of your remote repository to your system.

Run the git remote add command followed by your GitHub repository URL to add a new remote repository.

git remote add origin https://github.com/hitjethva/examplerepo.git

Now, push all changes in your local repository to the GitHub repository using the following command:

git push -u origin main

This command will push your committed changes to the main branch of your GitHub repository. You will be prompted to connect to your GitHub account, as shown on the following screen:

Connecting to the GitHub account

Connecting to the GitHub account

Provide your GitHub credentials, and click the Sign in button to sync your local repository with the GitHub repository. After successful authentication, you will see the following output:

Pushing the local repository to the GitHub repository

Pushing the local repository to the GitHub repository


Now, visit your GitHub repository page to verify your pushed file.

Verifying the README.md file in the GitHub repository

Verifying the README.md file in the GitHub repository

Create and manage branches via Git Bash

The branch is a new or duplicate version of the main Git repository. It creates a copy of all files in the main repository to avoid impacting the main repository. Branches are very useful when multiple developers are working on the same repository because they allow developers to work on different issues and bug fixes simultaneously.

Run the git branch command to list all branches in your repository.

git branch
Listing all existing branches

Listing all existing branches

Now, let's create a new branch named dev1.

git branch dev1

This creates a new dev1 branch in your current repository. However, you are still on your active branch. You will need to switch to your new dev1 branch to start working on it.

Run the git checkout command, followed by a new branch to switch to your dev1 branch.

git checkout dev1

You can now list your newly created branch using the following command:

git branch
Verify the newly created dev1 branch

Verify the newly created dev1 branch

If you want to remove the branch, use the -d option, followed by the branch name.

git branch -d dev1

To rename your current branch name, use the -m option, followed by the existing and new branches.

git branch -m dev1 new-dev1

To better understand the branching feature, let's create a new file named dev1.txt using the echo command.

echo "This is branch dev1" >> dev1.txt

Now, run the git add command to add this file to the local repository.

git add dev1.txt

Now, commit the added file with your own custom message.

git commit -m "added dev1.txt file on dev1 branch"

Finally, run the git push command to push the dev1.txt file to the dev1 branch of the GitHub repository.

git push origin dev1

Pushing the dev1.txt file to the dev1 branch on the GitHub repository

Now, visit your GitHub repository page and verify the newly created branch.

Verifying the dev1 branch on the GitHub repository

Git also provides a merge feature that allows you to combine recent changes from several branches into a single branch. To demonstrate the merging feature better, we will merge the dev1 branch to the main branch.

First, run the git checkout command to switch to the main branch.

git checkout main

Now, merge the dev1 branch to the main branch using the git merge command.

git merge dev1
Merging the main branch with dev1

Merging the main branch with dev1

If you want to create a backup copy of your currently active branch, run the git branch command followed by the backup option.

git branch backup

This will create a backup copy of your active branch.

Create and manage tags via Git Bash

Tags allow developers to give meaningful names to a specific version in the repository. They help you identify different commits at specific stages during project development.

Let's create a new tag named Release-1.0 using the git tag command and specify HEAD as the commit to create the tag from.

git tag -a 'Release-1.0' -m 'This is my first release' HEAD

Now push the newly created tag to the GitHub repository using the git push command.

git push https://github.com/hitjethva/examplerepo.git tag Release-1.0
Pushing newly created tags on the GitHub repository

Pushing newly created tags on the GitHub repository

Now you can verify your created tag in your GitHub account, as shown below.

Verifying the pushed tag on the GitHub repository

Verifying the pushed tag on the GitHub repository

If you want to get a list of all your existing tags, run the git tag command.

git tag

To delete an existing tag, use the -d option, followed by the tag name.

git tag -d Release-1.0

Stashing via Git Bash

Stashing allows you to temporarily save your current working copy in the local repository before pushing it to the main repository. Git stashing is very useful when you are working on a project and you need to do another important task without committing your current work.

To understand stashing better, let's add some content to the README.md file.

echo "Add new code" >> README.md

Next, run the git stash command to save your current work in the local repository.

git stash

You can now get a list of all your stashes using the following command:

git stash list
Listing all stashes

Listing all stashes

As you can see in the above screenshot, your current work is saved in a stash with the name stash@{0}.

You can now run the git stash show command, followed by your stash name to see detailed information about your stash.

git stash show stash@{0}
Viewing detailed information about your stash

Viewing detailed information about your stash

If you want to bring your file out of the stash list, run the git stash apply command, followed by your stash name.

git stash apply stash@{0}

Bringing files out of your stash

Bringing files out of your stash


To remove your existing stash, run the following command:

git stash drop stash@{0}

If you have multiple stashes and want to remove all of them, run the following command:

Subscribe to 4sysops newsletter!

git stash clear

Conclusion

In this guide, we explained how to install Git Bash on Windows. We also showed you how to use different Git Bash commands to manage and track your repositories. I hope this guide will help you during the software development process. For more information, visit the Git Bash documentation page.

avataravataravatar
1 Comment
  1. Very detailed.. nice!

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account