- Install Nginx Proxy Manager with Docker on Ubuntu - Thu, Sep 21 2023
- Dockerizing a Node.js application with NGINX, Let’s Encrypt, and Docker Compose - Mon, Sep 11 2023
- Ansible yum module: Install RHEL/CentOS packages - Wed, Aug 30 2023
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.
Step 3: Define the path where you want to install Git Bash, and then click Next. You will see the Select Components screen.
Step 4: Select all the components that you want to install, and then click Next. You will see the Select Start Menu Folder screen.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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:
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
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.
Type your repository name and other required information, and click Create repository. Once the repository is created, you will see the following screen:
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:
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
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
If you want to see the full information of the given commit, run the git show command.
git show
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:
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:
Now, visit your GitHub repository page to verify your pushed file.
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
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
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
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
Now you can verify your created tag in your GitHub account, as shown below.
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
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}
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}
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.
Very detailed.. nice!