Mastering Git & GitHub: The Ultimate Guide for Beginners
What is Git and GitHub?
Git is a powerful version control system that tracks changes in your code over time. GitHub is a cloud-based platform that hosts your Git repositories, allowing you to store, share, and collaborate on your projects online.
Why Use Git and GitHub?
Using Git and GitHub brings huge benefits to software development:
- Safer Development
You can easily undo mistakes or go back to an earlier version of your code. This reduces risk when experimenting or fixing bugs.
- Better Team Collaboration
Developers can work independently on different features without interfering with each other. Git merges everyone’s work together seamlessly.
- Smarter Workflow
Features like pull requests and issue tracking keep the project organized. Tasks are easier to manage, and reviews are built right into the workflow.
How to Get Started?
- Install Git on your machine.
- Create a local Git repository.
- Git will track your changes as you code.
- Push your code to GitHub to back it up or share it.
- Others can review, suggest, or contribute directly to your repository.
Initial Setup Using Git Commands
To begin with version control, I created three sample files manually:
- gitPoc101
- gitPoc102
- gitPoc103
These files were added to a new folder created specifically for practicing Git. After that, I followed these Git steps:
git init
This command initialized a new Git repository in the current folder. It tells Git to start tracking changes from here onward.
git add -A
This command stages all changes — including newly created, modified, and deleted files — for the next commit. In my case, it staged the three newly added files (gitPoc101, gitPoc102, gitPoc103).
git commit -m "Initial commit"
This command saved the current staged changes to Git’s history with a custom message — here, "Initial commit".
📌 Note:
After the initial commit, if you modify or rename any file, Git does not automatically include those changes.
You must run git add again to stage the updated files, and then use git commit -m "Your message" to save them in Git history.
I have attached a screenshot below that shows this process in action for better understanding.
Adding and Removing Files the Right Way
While working on any project, it is common to create or delete files as your work evolves. But when you're using Git, simply creating or deleting a file isn’t enough — Git must be informed of these actions explicitly so it can track them properly.
Creating a File and Adding It to Git
I created a new file named x.txt.
At this stage, the file was in the folder, but Git was not tracking it yet.
To make Git track the file, I followed these steps:
touch x.txt – Create the file.
git add -A – Stage the file so Git is aware of it.
git commit -m "Added x.txt" – Save the file addition in Git history.
Now Git recognizes the file and can monitor future edits or deletions.
Deleting a Tracked File
To delete the file properly while keeping Git informed, I used:
git rm x.txt – Remove the file from the folder and mark it for deletion in Git.
git commit -m "Removed x.txt" – Save the deletion in Git history.
The file was cleanly removed, and this action is logged in Git.
⚠️ Common Mistake to Avoid
If you try to create and delete a file without first adding it to Git:
touch x.txt
git rm x.txt
🚫 Git will throw an error.
Why? Because x.txt is untracked — it was never staged or committed.
Git can only remove files that were previously committed.
Conclusion
To ensure smooth and error-free file management in Git:
Always use git add and git commit before attempting to delete a file.
This ensures that Git properly tracks every action and your version history remains clean and reliable.
Handling Accidental Changes with git checkout -f
Imagine this: You step away from your system, and a family member out of curiosity opens one of your files and unknowingly edits or deletes something. Their intention was not harmful, but the changes they made are not relevant to your project. Now your file has unsaved, unwanted edits.
So what can you do?
Instead of manually undoing everything, Git provides a quick solution:
git checkout -f
This command forcefully restores all files in your working directory to match the latest committed version. Any unsaved changes will be discarded.
⚠️ Warning:
git checkout -f will permanently remove all uncommitted changes. Use it only when you're sure the current edits are not needed.
This is especially useful in scenarios where:
Irrelevant or mistaken edits are made
You want to quickly reset your workspace to a clean, last-committed state
This simple command can save you from hours of frustration.
I have attached a screenshot below that shows this process in action for better understanding.
Working with Branches in Git: Create & Switch Easily
Branches are used in Git to work on features or bug fixes independently, without affecting the main codebase. Here’s how to create a new branch and switch between them.
Step 1: Check the Current Branch
Before creating a new branch, it’s a good idea to see which branch you're currently on.
git branch
This lists all local branches and highlights the current one with a *.
Step 2: Create a New Branch
To create a new branch (e.g., feature101):
git branch feature101
This creates the branch, but you’re still on your current branch (e.g., main or master).
Step 3: Switch to the New Branch
Now, to start working on that new branch:
git checkout feature101
You are now on feature101. All your work will be saved here independently.
I have attached a screenshot below that shows this process in action for better understanding.
Git Merge Explained (Bringing Changes from One Branch to Another)
When working with multiple branches in Git, you often want to combine work from one branch into another — this is done using the git merge command.
Scenario:
You have worked on a new feature in the feature101 branch and now want to bring that work into the master branch.
Steps to Merge Feature Branch into Master:
Switch to the master branch — where you want to bring the changes:
git checkout master
Merge the feature branch (feature101) into master:
git merge feature101
This will apply all the commits and changes from feature101 into master.
⚠️ Avoiding Merge Conflicts:
Always commit all changes in both branches before merging.
Avoid editing the same lines of code in both branches — that’s the most common cause of conflicts.
Use git status before and after merging to stay aware of what's changing.
How to Push Your Local Project to GitHub
Let’s say you have completed your local project and now want to upload it to GitHub. Follow these steps:
Steps to Push a Project to GitHub:
Create a new repository on GitHub. You can name it anything you like.
In your local project folder, run:
git init – Initialize Git in your project.
git add -A – Stage all files for tracking.
git commit -m "Initial commit" – Save the current version.
Connect your local project to GitHub:
git remote add origin <your-repo-URL> – Link your project to the GitHub repo.
Push your files to GitHub:
git push -u origin master (or main or feature101) – Upload your project to GitHub.
Now your project is live on GitHub.
How to Pull or Clone a GitHub Project
If you want to download a project from GitHub and keep it updated:
Steps to Clone and Pull:
Clone the project to your system:
git clone <repo-URL> – Downloads the GitHub repository.
Navigate to the project folder:
cd <folder-name>
git pull – Fetches and merges changes from GitHub.
I have attached a screenshot below that shows this process in action for better understanding.
Comments
Post a Comment
For more information kindly inbox at yousufbgp@gmail.com