How to Use Git and Git Bash Locally: A Comprehensive Guide

bnew

Veteran
Joined
Nov 1, 2015
Messages
62,879
Reputation
9,559
Daps
172,187

How to Use Git and Git Bash Locally: A Comprehensive Guide​


By Nikhil

March 31, 2025


Table of contents Introduction
Installation
Windows
macOS
Linux
Verifying Installation
Git Bash Basics
Navigation Commands
File Operations
Keyboard Shortcuts
Git Configuration
Additional Configurations
Basic Git Workflow
Initializing a Repository
Checking Status
Staging Files
Committing Changes
Branching and Merging
Working with Branches
Merging Branches
Handling Merge Conflicts
Deleting Branches
Remote Repositories
Adding a Remote Repository
Advanced Git Commands
Stashing Changes
Reverting Changes
Interactive Rebase
Troubleshooting
Common Issues and Solutions
Git Best Practices
.gitignore Example
Conclusion

Introduction

Git is a distributed version control system that helps you track changes in your code, collaborate with others, and maintain a history of your project. Git Bash is a terminal application for Windows that provides a Unix-like command-line experience for using Git.

This guide will walk you through setting up Git, using Git Bash, and mastering essential Git commands for local development.

Check out how HOSTINGER HORIZONS can help to build and launch full-stack web apps, tools, and software in minutes without writing any code (Promoted)

Installation

Windows

Download Git for Windows from git-scm.com Run the installer with default options (or customize as needed) Git Bash will be installed automatically as part of the package

macOS

Install Git using Homebrew: brew install git Alternatively, download from git-scm.com

Linux

For Debian/Ubuntu: sudo apt-get install git For Fedora: sudo dnf install git For other distributions, use the appropriate package manager

Verifying Installation

Open Git Bash (Windows) or Terminal (macOS/Linux) and type:

AD_4nXfy9T0WbaE57Eh8npqtmlDtHVpITvKq_ED4i3F3iDFEoHx00mEUcj4KCy4wBN6c3_owMFCJKZR_tyAf2paX9bc0u-0QG1Ni9iRGpqNJ2smmTlpVvP5M3xY_h8XWyRX_5uDn7Q1CGw


This should display the installed Git version.

[FREE AI Webinar] What truly makes a system "agentic"? Join this deepest / Haystack webinar with Mathis Lucka, Lead AI Engineer, to learn more including a demo of the architecture behind an agent for GitHub actions (April 17, 11:00am ET/ 8:00am PT) [Sponsored]

Git Bash Basics

Git Bash provides a Unix-like shell experience on Windows. Here are some essential commands:

Navigation Commands

pwd – Print working directory ls – List files and directories cd [directory] – Change directory mkdir [directory] – Create a new directory rm [file] – Remove a file rm -r [directory] – Remove a directory and its contents

File Operations

touch [filename] – Create an empty file cat [filename] – Display file contents nano [filename] or vim [filename] – Edit files in the terminal

Keyboard Shortcuts

Ctrl + C – Terminate the current command Ctrl + L – Clear the screen Tab – Auto-complete commands or filenames Up/Down arrows – Navigate through command history

Git Configuration

Before using Git, configure your identity:


AD_4nXfmqYuXo7wp4op7w94i0ZMEVrl-Eq0Sig4MLjc4vawK43QwnmA0Fg7Dj93W012EWDxZKKjzY2uTopFXboyz2wbYBgEa23lHkmwR9ofDlCzivcYnVJtwMgPQln7X83Um0pF5nIevsA


AD_4nXc15v8i0fErULoGFRzsqrbiVR-IRjGHOAGRT8mAg6EAeJvqsAhNpn93fZQQSypwYRJGp4lKmWQinENEDqEPuZ5SaOpr5OIxQLw15UgbSF7JlF2VoXWb72HJEdL7q0Hr-hOg6in11A


Additional Configurations

Set your default editor:

AD_4nXekukhyjOsciGHa1SWczVpP-kVZcO6blTFB5AZbBcVeTIWyEXg5KVNRpfJFSnJ-nr1t1GqtjAM1UcaA7YPb3bGIhkp33VzdxVmBbPkeJMpzicAQ6LBUnDsWaZIl0ZxU4TyXr7SklQ


Enable colorful output:

AD_4nXdYmF64c9uxtkGcsynJYANspwjTCkYTNYVFBPuVxMKyVDdCtd6ieZMyr5gXa4HiqpNZmMqFxSvAInU7G0lOHJGXcF2zDLCrrekyLn-r0SUMCzolptVOfK7zkzwaGiBv7I5n8FguCA


View all configurations:

AD_4nXfjVLx2wSCDq42axUT5ACppz3StkjRAq9xgc_CsAkKzeXD5dzMZeprF8NqwMEw9EhUrCTEylR77YGsEmqYKMJ8J9ojQ1ZG6Bf-VGHjvAuZf69k0tw90z22ZzhgbTJUMqsy2DKXV


Basic Git Workflow

Initializing a Repository

Navigate to your project folder and initialize a Git repository:

AD_4nXemUtWDWeraOakJYB4627rxEq4NioY_siNX84m589Vne2hoFKoIZ-hCmhMNnlaEOmsKftCDbZSNcok5yjRB6r1NB734ZvRezFVxQ_t96zJQt49PyTL83ulijcYPBXB5BH3MBxj5zw


Checking Status

See which files are tracked, modified, or staged:

AD_4nXeuytICUnqKsI_ICPbjOzWWwafbr2kakBxEoIrY-udRrm0WUBrhiigT-1qRFX17W2j4aauJ0HxFWlfANJDI3C6n6yRiX2WbnnBZqpcQ6-vyjSdelxbOkPCXFbCJkLrJKPnAE5Cjgw


Staging Files

Add files to the staging area:

AD_4nXeKxFcHVW_UDQ-5bShB6ARWkNQBFIKUcZsvfy2fKXVRgPWrCUyBO93Jbv_QyCUxkN9oabHxIEnHufgTSlowd4d62xVSGVeW0CtwEORAcqTaDJKzbBRIyFG6OTjEhQGKY7vooPEB
 

bnew

Veteran
Joined
Nov 1, 2015
Messages
62,879
Reputation
9,559
Daps
172,187
Committing Changes

Save staged changes to the repository:

AD_4nXd6EDOQbCsqpU1YDYbGNcHv6Fs2THitxdBbNjZCY3MPZzVagFL3JC5zVKpBQbkPbpJr3n-FODSIHcL3l89GlxS9psWW1-Fv_lblUR-c7cDqwkSEFopUef_F5r-RdoP92XmqKec9Jg


Or open an editor to write a more detailed commit message:

AD_4nXfoPEksCnMckaxX5zIMMWQb7lypOcbGiB0EVgT5DCrQpkzqE5gLhw2ZXlgqEU1D7UYahYxZbFAA5DjepD1czYUszyWJdieebQKnd0eqpR6ltAZemcGjzO2x_-1kD8jeIuVtLC9v


Viewing Commit History

AD_4nXdTiLNGzLbS_-f3qQx7dAD-V0WdvpbrpDY-GhXfa7e1n_BdWkLa41JLwpSIUb8z32BnoRQNwbpX45wYeAjMAfsnSFiYJAkRR8XAzWrNi99uRQkZlMdSiQ2IosmPWIgRA-_BExq9aQ


Branching and Merging

Working with Branches

Create a new branch:

AD_4nXdRfezn2OKo04DSsSBypYc-HGnZIQUGECJvl8ZXAJe92x9eFMQH6tzZUupZrGGDdym1j1Ms_zIR8upNiLVWCW1yWZDgiLKbAhWp4Tw8D6XzzijNBVN-_HWJbOCf2sPtN2MFoiVP_w


Switch to a branch:

AD_4nXdMpX5CnOS9FJg-yvgd7hgDvZ4MoLPiGxHrlqUBy3LyGD-AmOILfur8MMdAfTsmZNC6p2UATwSh0f3yhCXUnj_J2fifzeOmgJ-FG4GmnpD2ChA5asuGuer_EHl457Fio4ETWJ_PAg


Create and switch to a new branch in one command:

AD_4nXeT-CFZ0gi77fK4R3UwqoXeKM0CACKD01zD8gcXI1qL6xJj2NkRTAD6AlJSe_Yo8uxVC7Xc7rEDqUTKjaGSlWNIieORyyBRpDLDboLp2jo8kKGL7jdCZ-ULecYBPRGopwPIqILN


List all branches:

AD_4nXebVxta20rg8inM3zYtsWHMa9BxyZoClRlcr_xDDwvKxuMqLynG0k-ADKD-fSvEShrAxRCKMUTkw-ElZFkrxbXsHoNaksezHOxLwp1LylTdIbi0nYwLwahcrNBeywAu4vnjw978


Merging Branches

Merge changes from another branch into your current branch:

AD_4nXcbeWY8a5oxqG94q4tMQGAAp-Ut9t60FZVKJj9lT8fOxC5oc6E4P3lvsybGGZM7u7gaC3OK1rEmkt46riQzB_64oIW8ZXeymRVZCJ-F2EUUa_HiqMgcA2DaZf5f97zg7a6pe9uT3A


Handling Merge Conflicts

When Git can’t automatically merge changes, you’ll need to resolve conflicts:

Git will mark the conflicted files Open the files and look for conflict markers (<<<<<<<, =======, >>>>>>>) Edit the files to resolve conflicts Add the resolved files: git add <filename> Complete the merge: git commit

Deleting Branches

Delete a branch after merging:

AD_4nXfZ7cll0vIthB7HwP4nXW0KHMWYEjZPqWmo07R-9HfrL6vrYtbqCDxcP9rb1gOUOTx_NslW2WbMjGZ4hisQbV0UUzMQ4Kh5j294pPplcjkIVjnSc-wLWz9nSY8XzCVTAA08D21cOA


Remote Repositories

Adding a Remote Repository

AD_4nXfZxQNV2c7tX5cEQNo5KChY_SC4WYHy5DiLd12OpkZ46h8R5OuQFJeXwd3Fv1QGQfr9FVGHfxbaGjfcBdX203NRjpWxSQ6iRx_M_lRMsZPvdTjfXteV0_8lBet1tOX5yyjHhE7-


Viewing Remote Repositories

AD_4nXfVILgQezMr_LhOaHRk4832lcjOQhvkOdIrc2q1GJ2KOp_2gtX9xMQJ-69GROEpOxpY-fIqUjDq_gHF2I2MHYner2xxTgmabjfObR6_Taz5Elmsg913SJqTy6PFbLUEah8q-1GodQ


Pushing to a Remote Repository

AD_4nXcbw5HUHwBr0QKok6VKP2TKwNlEtCBowmZhNp8OYc0DrTVTjrQClg4-9ychdL19WBT25_Ts6far8lva-e9_36RZIZFdoQIyjDPqm8JmzFJsVpovzhPSiPaNwCXSxCNc-YL0DXO2


Pulling from a Remote Repository

AD_4nXfp9nXlSFem-2imoqk46VB0dZJ7RernmWLEGd5UKyTOoQK6hVeaYnWIUVMfntqry_dnpcgsPeMLMPxIvl3_T8xR171bQj8ExTY-IrqxbErqsmGhGF4yv2bBqcaKSAroaZPXUkNS5w


Cloning a Repository

AD_4nXcZGxHqiDLSd0IyY-iKW4iaLNILLYabANi8PPPd0h_VibOfHHao3qbLC4uBa_rTldDR1T06sHHSwEPQRvJ2sYYYITTXVGo1lovJXXigCO4IKj-1eYVekhzhwdnAuInJ9MLPUSPKEw


Advanced Git Commands

Stashing Changes

Temporarily store modified files to work on something else:

AD_4nXeesq2kVbhdt7VIVTss3U8jycWBzUb2uUYoCv1mPRoN6B4mGXDFQpm4Bg63kfRVSO5KcbD1lHikYlmFmzJyc9FHpT2uyZOeWTKBXqGiqbwTYkvcMdybcrRnhgOD7tFnVB6SWBKh


Reverting Changes

Undo commits:

AD_4nXcoBGhCI9BxDtW8FN-pdVXZkjTCd_ApETuuBWkCD8f25pQnAkQqvEqlxv9MayYDtiDcDPiE-ZY5XVHSOKbVhm0IXTIhtzVQAZuIhWH8qTj0szkGdnezWzfwqzF4aEz_-rgtl2bG


Reset to a previous state (use with caution):

AD_4nXf_8xYkNcW-Pyxyjg_74O2ZhpjTRfc7VKcHSSz70tp1FXBry1WDMFPqiW_o2XDSKvqgKAySJbd-xMUiB4C9XhN9RK_4G3agyiQrVZooSLVhg2WdAMZcOeTQKASBEcoHg3eK6_wITw


Viewing and Comparing Changes

AD_4nXfal3-s9kpkr8uDLFqh9L8joPVSfmXApx-woKTViiHa09CCc-BI03T73iu-y9Gug4pfDmjj-ZvIoHXCdWLJHCot8hN5I9bxpq-OBdlSjKuiKE-FgBVKGMViSnd3seB9SK3zY53XtQ


Interactive Rebase

Rewrite, squash, or reorder commits:

AD_4nXdxm2cdcN6DfJkZ-nEfnK2Xedy_sdt3KFwyWfFwH1pdiocwpJ0KtdLIEa00yBHwCkwdvxQ0pvVsBIgHSznDCPiJWSagKggvFvKBhCfbJJ4AFXYUNEq6ebf4QR1hR8or6iUwqS2R3Q


Troubleshooting

Common Issues and Solutions

Problem : “fatal: not a git repository”

Solution : Make sure you’re in the correct directory or initialize a repository with git init

Problem : Unable to push to remote repository

Solution : Check if you have the correct permissions Pull latest changes first: git pull origin main Check if remote URL is correct: git remote -v

Problem : Merge conflicts

Solution : Resolve conflicts manually, then git add the resolved files and git commit

Problem : Accidental commit

Solution : Use git reset –soft HEAD~1 to undo the last commit while keeping changes

Git Best Practices

Commit frequently with clear, descriptive commit messages Create branches for new features or bug fixes Pull before pushing to minimize conflicts Write meaningful commit messages that explain why changes were made Use .gitignore to exclude unnecessary files (build artifacts, dependencies, etc.) Review changes before committing with git diff and git status Keep commits focused on a single logical change Use tags for marking releases or important milestones Back up your repositories regularly Document your Git workflow for team collaboration

.gitignore Example

Create a .gitignore file in your repository root:

AD_4nXc8hsknj8SvunKqfB9b0bTVVnI7C80_-U9gh7FehKa45urOekU--AQiTiIGsR-ryLyBIpmnFIIejgyqgBdgDis016BKU3skNeTHbAMwfxfCuN5dYfJyvKz4vgF2LBwtS3-dOxdKcg


Customize this file according to your project’s specific needs.

Conclusion

Git and Git Bash provide powerful tools for version control and collaborative development. In this guide, we covered installation across platforms, essential Git Bash commands, repository initialization, the core add-commit workflow, branching strategies, remote repository management, and advanced operations like stashing and rebasing. We also addressed common troubleshooting scenarios and best practices to maintain a clean workflow. With these fundamentals, you’re now equipped to track changes, collaborate effectively, and maintain a structured history of your projects.
 
Top