Sitemap
Ashmeet Kaur

Ashmeet Kaur

Writing articles on core Java Development .

Git

4 min readJul 12, 2025

--

🚨This article is all about Git — a powerful tool every developer should know.

We keep track of all the changes in case we want to go to the previous version.Git is all about the contributions.

📖 Under the hood, Git uses an n-Ary tree structure, where each version (or commit) is linked through unique hashes. These hashes, along with important metadata, help Git efficiently manage the entire project history also makes contributions easy.

🔧 Basic Git Commands

✅ git add – Tell Git to look at your changes

This command adds your file(s) to the staging area. It’s like saying, “Hey Git, look at what I’ve changed!”

💾 git commit – Tell Git to save your changes

Once your changes are staged, this command saves them to the repository. Think of it as a snapshot of your code at that moment.

🗂️ Folder vs Repository

Let’s clarify the difference between a folder and a repository:
📁 A Directory is just a normal folder on your system.
🗃️ A Repository (repo) is a directory where Git has been initialized using git init. It means Git is now tracking that folder.

🌀 The Four Stages of Git
Every file in your Git repository goes through one of these four stages:

1️⃣ Untracked

You’ve created a file, but Git isn’t tracking it yet. This is what happens right after you initialize Git with git init.
➡️ Once you run git add, the file moves to the Staged state.

2️⃣ Staged

📥 Git can now see the file and its changes. It’s in the queue, ready to be saved.
➡️ When you run git commit, the file moves to the Unmodified state.

3️⃣ Unmodified

✅ The file is safely saved in Git’s history. Nothing has changed since the last commit.
➡️ If you make any changes to this file, it becomes Modified.
➡️ If you remove the file, it becomes Untracked.

4️⃣ Modified

✍️ You’ve changed the file but haven’t told Git yet.
➡️ To save it again, you must add it back using git add (moves to Staged), then git commit (back to Unmodified).

🔧 Basic Git Commands

✅ git add

Adds your file to the staging area in the current folder. Git starts tracking the changes you’ve made.

💬 git commit -m "your message"

Saves the changes permanently with a message describing what you’ve done.

🧐 git status

Check the current status of your files — whether they’re staged, modified, or untracked.

📜 git log

Shows all the commits made so far, from newest to oldest.

🔍 git log -p -2

Want to see just the last 2 commits with their changes? This one’s for you.

🕹️ git checkout <filename>

If you made a mistake in a file, this command helps you revert it to the last committed state.

🧹 git reset <commit-id>

Reset your codebase to a specific commit. Be cautious — this can change history!

⚠️ Hard Reset Warning

git reset --hard <commit-id> will delete all changes from your files and working directory. ❌ These cannot be recovered — use only if you're sure!

⏮️ git reset HEAD~2

Jump back 2 commits (or however many you specify). Useful when you want to undo recent changes.

🙈 Ignore Unwanted Files

📂 .gitignore

Create a .gitignore file and list all the files or folders you don’t want Git to track. Perfect for temporary files, logs, build folders, etc.

🚫 git rm --cached <filename>

Untrack a file from Git while keeping it in your working directory.

🌿 Working with Branches

🌱 git branch <branch-name>

Create a new branch to work on features without touching the main code.

🔄 git checkout <branch-name>

Switch to the branch you just created.

👀 git branch

View all the branches in your project. The active one will have an asterisk *.

🔗 Merge Branches

To merge b1 into main:

  1. Go to main using git checkout main

2. Run git merge b1

Get Ashmeet Kaur’s stories in your inbox

Join Medium for free to get updates from this writer.

💡 Note: Always merge child branches into their parent first, and then merge the parent into main. Avoid merging a child branch directly into main.

☁️ Using GitHub

🖥️ What is GitHub?

GitHub is a cloud-based platform where you can store and share your Git repositories online.

🌐 Connect Local Repo to GitHub

git remote add origin https://github.com/yourusername/yourrepo.git

This links your local folder to the remote GitHub repository.

📤 Push Your Code

git push origin main

Pushes your code from your local main branch to GitHub.

💡 While branch names can be anything locally, the name should match exactly when pushing to GitHub.

🎯 Final Tips

Always double-check before using --hard reset.

Use .Gitignore to keep your repo clean.

Don’t directly merge child branches into main — keep things clean and maintainable.

👨‍💻 Mastering these Git commands will save you from headaches and help you work like a pro — whether you’re solo-building a side project or collaborating with a team on a large codebase.

--

--

Ashmeet Kaur
Ashmeet Kaur

Writing articles on core Java Development .