.NET Core  

Mastering Git: The Definitive Guide from Basic to Advanced for .NET Developers

In the modern DevOps landscape, Git is no longer just a "nice-to-have" skill; it is the backbone of professional software engineering. Whether you are building an ASP.NET Core multi-tenant application or managing complex Oracle migrations, understanding the nuances of Git can save hours of debugging and prevent catastrophic data loss.

This article provides an end-to-end roadmap of Git commands, categorized by mastery levels.

1. The Core Fundamentals: The Local Workflow

Before pushing to a remote server, every developer must master the local "Three-Tree" architecture: the Working Directory, the Staging Area (Index), and the Commit History.

Essential Commands

  • git init: Initialize a new Git repository in your project root.

  • git status: Displays the state of the working directory and staging area.

  • git add <file>: Moves changes to the staging area. Use git add . to stage everything.

git commit -m "feat: implement user authentication": Records the snapshot.

  • git log --oneline --graph --all: View a visual representation of your branch history.

2. Branching & Team Collaboration

Working directly on the main branch is a recipe for disaster. Professional .NET teams use Feature Branching or GitFlow.

Commands for Collaboration

  • git checkout -b feature/payment-gateway: Creates and switches to a new branch.

  • git switch <branch>: The modern, safer way to switch between branches.

  • git merge <branch>: Combines changes into your current branch.

  • git remote add origin <url>: Connects your local repo to GitHub, Azure DevOps, or GitLab.

  • git push -u origin <branch>: Uploads your branch and sets the upstream tracking.

  • git pull: Fetches and merges updates from the remote in one command.

3. The "Undo" Palette: Fixing Mistakes

As developers, we often make mistakes—committing to the wrong branch or accidentally deleting a file. Git provides a safety net for almost every scenario.

  • Discarding Local Changes: git restore <file>

  • Unstaging a File: git restore --staged <file>

  • Fixing the Last Commit: git commit --amend

    • Scenario: You forgot to include a .cs file in your last commit. Stage it and run this.

  • Soft Reset: git reset --soft HEAD~1

    • Undoes the commit but keeps your code changes in the staging area.

  • Hard Reset: git reset --hard HEAD~1

    • Deletes the commit and all changes. Use only when you want to wipe the slate clean.

4. Intermediate Mastery: Stashing & Cleaning

Sometimes you aren't ready to commit, but you need to switch branches to fix a critical bug in production.

  • git stash: "Freezes" your current work and clears the working directory.

  • git stash pop: "Unfreezes" your work and brings it back to your branch.

  • git clean -fd: Removes all untracked files and directories. This is great for cleaning up /bin and /obj folders that weren't ignored.

5. Advanced Architect: History Manipulation

This is where you separate the juniors from the seniors. Clean history is vital for enterprise projects like AJK_CFRS or CitizenInfo.

Rebase vs. Merge

While merge creates a "merge commit," rebase rewrites history to make it a straight line.

  • git rebase main: Integrates the latest changes from main into your feature branch without an extra merge commit.

Interactive Rebase (The "Squash")

  • git rebase -i HEAD~n: Opens a menu to "Squash" (combine) multiple small commits into one clean, professional commit.

Cherry-Picking

  • git cherry-pick <commit-hash>: Snatches a specific fix from one branch and applies it to another without merging the entire branch.

6. The Wizard Level: Deep Recovery & Debugging

When things go horribly wrong, these "plumbing" commands are your best friend.

  • git reflog: The "Log of Logs." It shows every single move the Git HEAD has made. Even if you deleted a branch, you can find the commit hash here and restore it.

  • git bisect: A binary search tool to find the exact commit that introduced a bug.

    1. git bisect start

    2. git bisect bad (current version is broken)

    3. git bisect good <hash> (this old version worked)

    4. Git will guide you through the commits to find the culprit.

Pro-Tip: The .NET Developer's .gitignore

In the world of Visual Studio, your repository should only contain source code—never binaries or user settings. Ensure your .gitignore includes:

  • [Bb]in/, [Oo]bj/

  • .vs/, .user

  • *.suo, *.user

  • appsettings.Development.json (if it contains secrets)

Conclusion

Mastering Git is an iterative process. Start with the basics of staging and committing, then move to branching. Once comfortable, explore Interactive Rebasing to keep your project history clean.