aboutBlog

Learn DevOps Step-by-Step Tutorials and fixing related issues.

Welcome to Py-Bucket, your go-to blog for DevOps tutorials and production issues fixes guide.

  • ✔ Beginner-friendly DevOps guides
  • ✔ Real-world production issues and fixes

Wednesday, 1 February 2023

Git for Beginners: Repositories, Commits, Branches and Merges Explained

git

What is Git? A Simple Guide for Beginners

Git is a free, open-source version control system that helps developers manage their code efficiently. Created by Linus Torvalds in 2005 for Linux development, Git is now used worldwide for tracking changes, collaborating on projects, and maintaining clean code history.


Basic Git Concepts You Should Know

Understanding these key Git concepts will help you work smarter:

1. Repository (Repo)

repository is where Git stores all project files, folders, and version history. There are two types:

  • Local repository – Stored on your computer.

  • Remote repository – Hosted online (e.g., GitHub, GitLab).

2. Commit

commit is a saved snapshot of your changes at a specific time. Each commit has a unique ID, allowing you to revert changes if needed.

Git Commit Workflow:

  1. Edit files in your local repo.

  2. Stage changes using git add (prepares changes for commit).

  3. Commit changes using git commit (saves changes with a message).

3. Branch

branch is a separate line of development. It lets you work on new features without affecting the main code. Once ready, you can merge it back into the main branch (usually main or master).

Git Branching Policy

Teams follow branching policies to keep work organized. Common rules include:

  • Naming conventions (e.g., feature/login-page).

  • Branch creation rules (who can create branches).

  • Merge rules (approvals needed before merging).

  • Code review standards (ensuring quality before merging).

A good branching policy keeps teamwork smooth and avoids conflicts.

4. Merge

Merging combines changes from one branch into another. Git supports different merge types:

Merge TypeDescription
Fast-forwardMoves branch pointer forward (no conflicts).
Three-wayCombines changes from two branches (may cause conflicts).
SquashMerges multiple commits into one (cleans up history).
OctopusMerges multiple branches at once.
RecursiveDefault merge strategy (handles complex merges).

Note: Fast-forward and Squash merges are most commonly used.

5. Stash

Stashing temporarily saves changes you’re not ready to commit. Useful when switching branches without losing work.

6. Revert

Reverting undoes changes—helpful for fixing mistakes or rolling back to an older version.

7. Push & Pull

  • git push – Uploads local changes to a remote repo (e.g., GitHub).

  • git pull – Downloads remote changes to your local repo.

Push & Pull Workflow:

  1. Push → Share your changes with the team.

  2. Pull → Get the latest updates from others.


Conclusion

Git is a must-know tool for developers. By mastering these basics—repositories, commits, branches, merges, and remote operations—you can collaborate efficiently and manage code like a pro.

Whether you're a beginner or an expert, Git improves productivity and keeps projects organized. Start using Git today! 

----------------------------------------------------------------------------------------

FAQ:

Q1. What is Git and why do developers use it?

Git is a free, open-source distributed version control system created by Linus Torvalds in 2005. Developers use it to track every change made to their code over time, collaborate with teammates without overwriting each other's work, and roll back to a previous working version if something breaks. It is the most widely used version control system in the world and is a foundational skill for any developer, DevOps engineer, or data scientist working with code.


Q2. What is the difference between Git and GitHub?

Git is the version control tool that runs on your local machine — it tracks changes and manages your code history. GitHub is a cloud-based hosting platform that stores Git repositories online so teams can collaborate remotely. Think of Git as the engine and GitHub as the garage where you park and share your car. Other platforms like GitLab and Bitbucket serve the same purpose as GitHub but with different features.


Q3. What is the difference between git merge and git rebase?

git merge combines two branches by creating a new merge commit that preserves the full history of both branches. git rebase replays your commits on top of another branch, creating a cleaner linear history. Merge is safer for shared branches because it doesn't rewrite history. Rebase is preferred for keeping feature branches up to date with the main branch before creating a pull request, but should never be used on public shared branches.


Q4. What is the difference between git pull and git fetch?

git fetch downloads changes from the remote repository but does not apply them to your working branch — it lets you review what changed before merging. git pull is essentially git fetch followed by git merge in one step — it downloads and immediately applies remote changes to your current branch. For safer workflows, especially on shared branches, git fetch followed by a manual review is preferred over a direct git pull.


Q5. When should I use git stash?

Use git stash when you have uncommitted changes in your working directory but need to switch to a different branch urgently — for example to fix a production bug on the main branch. Stash temporarily shelves your changes so your working directory is clean. Run git stash pop when you return to resume where you left off. You can also name your stashes with git stash save "description" if you have multiple stashes active at the same time.


Q6. What is a Git branching strategy and which one should I follow?

A branching strategy defines how your team creates, names, and merges branches to keep the codebase organised. The most popular strategies are Git Flow (separate branches for features, releases, and hotfixes — good for versioned products), GitHub Flow (single main branch with short-lived feature branches — good for continuous deployment), and Trunk Based Development (everyone commits to one main branch with feature flags — used by large tech teams). For most small DevOps teams, GitHub Flow is the simplest and most practical to start with.


Q7. What is a Git commit best practice?

Good commit messages follow the pattern: a short imperative summary under 50 characters on the first line, followed by a blank line, then an optional detailed explanation. For example: "Fix null pointer in user auth service" rather than "fixed stuff". Each commit should represent one logical change — avoid mixing bug fixes and feature additions in the same commit. This makes code reviews cleaner, git log more readable, and rollbacks more precise when something goes wrong in production.


No comments:

Post a Comment

Devops

DevOps Tutorials

Featured posts

🔥 Featured Tutorials

Author Details

Hi, I'm Prashant — a full-time software engineer with a passion for automation, DevOps, and sharing what I learn. I started Py-Bucket to document my journey through tools like Docker, Kubernetes, Azure DevOps, and PowerShell scripting — and to help others navigate the same path. When I’m not coding or writing, I’m experimenting with side projects, exploring productivity hacks, or learning how to build passive income streams online. This blog is my sandbox — and you're welcome to explore it with me. Get in touch or follow me for future updates!