Skip to main content

Command Palette

Search for a command to run...

Git & GitHub: A Beginner's Guide Using Git Bash (Hands-on Tutorial)

Introduction Version control is an essential skill for every developer. Whether you're working individually or collaborating with a team, Git helps you track changes in your code, while GitHub allows you to store and share your repositories online. In this blog, I'll walk through: Installing and using Git Bash Creating a local Git repository Making commits Connecting to GitHub Pushing code to GitHub Creating and working with branches Deleting files in another branch Pushing branch changes to GitHub This is a complete hands-on example with screenshots. -What is Git? Git is a distributed version control system that helps developers: Track file changes Maintain project history Collaborate with others Roll back to previous versions -What is GitHub? GitHub is a cloud platform that hosts Git repositories. It allows you to: Store projects online Collaborate with teams Create Pull Requests Manage branches Review code

Updated
4 min read
Git & GitHub: A Beginner's Guide Using Git Bash (Hands-on Tutorial)

Step 1: Create Your Project

Create a folder for your project.

Example:

mkdir Github
cd Github

Create some files.

Example:

touch dev{1..15}.txt

Verify:

ls

Step 2: Initialize Git Repository

Initialize Git.

git init

Check repository status.

git status

Step 3: Add Files

Stage all files.

git add .

Verify staging.

git status

Step 4: Commit Changes

Create your first commit.

git commit -m "satya"

You should see output showing that all files have been committed.

Step 5: Create a GitHub Repository

Go to GitHub.

Create a new repository.

Example:

gitsamp

Do not initialize it with README if your local repository already exists.


Step 6: Connect Local Repository to GitHub

Add the remote repository.

git remote add origin https://github.com/USERNAME/gitsamp.git

Rename the default branch.

git branch -M main

Verify.

git branch -a

Push the project.

git push -u origin main

The first push may ask you to authenticate using your browser.

Step 7: Verify on GitHub

Refresh your GitHub repository.

You should now see:

  • All project files

  • Commit history

  • Main branch

Step 8: Create a New Branch

Create a new branch.

git checkout -b usr2

Verify.

git branch

Current branch:

usr2

Step 9: Delete Some Files

Remove the first five files.

rm -rf dev{1..5}.txt

Verify.

ls

Check Git status.

git status

Git will display deleted files.

Step 10: Stage the Deletions

Stage all changes.

git add .

Check status again.

git status

Now the deleted files are ready to commit.


Step 11: Commit Changes

Commit the deletion.

git commit -m "Removed dev1 to dev5"

Git creates a new commit containing the file deletions.


Step 12: Push Branch to GitHub

Push the new branch.

git push -u origin usr2

GitHub creates a new remote branch.



Step 13: Verify Branch on GitHub

Open GitHub.

Switch from main to usr2.

You will notice:

  • Only dev6–dev15 remain.

  • The branch is ahead of main by one commit.

Useful Git Commands

Command Description
git init Initialize repository
git status Check repository status
git add . Stage all files
git commit -m "message" Create commit
git log View commit history
git branch List branches
git checkout branch-name Switch branches
git checkout -b branch-name Create and switch branch
git branch -d branch-name Delete branch
git remote -v Show remote repositories
git push origin branch Push branch
git pull origin branch Pull latest changes
git clone repository-url Clone repository

Common Errors

Remote already exists

git remote remove origin
git remote add origin <repository-url>

Authentication Failed

Sign in using your GitHub account or use a Personal Access Token (PAT) if prompted.


Nothing to Commit

git status

This means your working tree is clean and there are no changes to commit.


Conclusion

Git and GitHub are essential tools for every developer. In this tutorial, we:

  • Initialized a Git repository

  • Added and committed project files

  • Connected a local repository to GitHub

  • Pushed code to the main branch

  • Created a new branch (usr2)

  • Deleted files in the new branch

  • Committed and pushed those changes

  • Verified everything on GitHub

Mastering these basic workflows will make it much easier to collaborate on software projects and manage your code with confidence.