Skip to main content

Git — Version Control for .NET Developers

What is Git?

Git is a distributed version control system — it tracks every change you make to your code, who made it, and when. Created by Linus Torvalds (creator of Linux) in 2005, Git is now used by virtually every software team on the planet.

Why Use Git?

Without Git, you have no history, no way to undo mistakes, and no safe way to collaborate. With Git, you can go back to any version of your code, experiment in branches, and merge work from multiple developers safely.

Where is it Used?

ScenarioUse
Every .NET project✅ Track all code changes
Team collaboration✅ Multiple developers on same codebase
CI/CD pipelines✅ GitHub Actions triggers on git push
Code review✅ Pull requests compare branches
Deployment✅ Deploy specific git tag to production

Key Benefits

  • Full history — every change saved forever, revert any time
  • Branching — work on features without breaking main code
  • Collaboration — merge work from multiple developers
  • Free & open source — runs locally, no internet needed
  • Industry standard — every company uses Git

Git is mandatory for every professional developer.

Install and Configure

# Download: git-scm.com/downloads
git --version # verify

# First-time setup (MUST do this once)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait" # VS Code as editor

Core Concepts

Repository → folder tracked by Git (.git folder inside)
Commit → snapshot saved forever with message
Branch → independent line of development
Staging Area → files prepared for next commit
Remote → repo copy on GitHub/server
Working Dir → your files as they are right now
HEAD → pointer to current branch/commit

Daily Commands

# Status
git status # what changed?
git diff # show unstaged changes
git diff --staged # show staged changes

# Stage and commit
git add Program.cs # stage one file
git add src/ # stage folder
git add . # stage everything
git commit -m "Add student enrollment endpoint"
git commit -am "Fix bug" # stage + commit (tracked files only)

# History
git log --oneline # compact history
git log --oneline --graph # with branch graph
git show abc123 # show specific commit details

⌨️ Git in Visual Studio Shortcuts

ActionVisual Studio
Open Git ChangesCtrl+0, Ctrl+G
Stage fileClick + in Git Changes window
CommitEnter message → Ctrl+Enter
PushGit menu → Push / Ctrl+0, Ctrl+P
PullGit menu → Pull
Create branchGit → New Branch
Switch branchGit → Checkout

Git in VS Code Shortcuts

ActionVS Code
Open Source ControlCtrl+Shift+G
Stage fileClick + next to file
CommitEnter message → Ctrl+Enter
Push... menu → Push
Open terminalCtrl+`` ``

Branching

# Create + switch
git switch -c feature/attendance-api # modern (Git 2.23+)
git checkout -b feature/attendance-api # classic

# Switch
git switch main
git checkout main

# List
git branch # local
git branch -a # all (local + remote)

# Merge
git switch main
git merge feature/attendance-api

# Delete
git branch -d feature/attendance-api # safe (merged only)
git branch -D feature/attendance-api # force delete

Fix Common Mistakes

# Undo last commit — KEEP changes
git reset --soft HEAD~1

# Undo last commit — DISCARD changes (dangerous)
git reset --hard HEAD~1

# Discard changes to specific file
git restore src/Program.cs

# Unstage a file
git restore --staged src/Program.cs

# Stash work-in-progress
git stash
git stash pop # restore
git stash list # see all stashes

# Fix last commit message (before push)
git commit --amend -m "Correct message"

.gitignore for .NET

# Build outputs
bin/
obj/
publish/

# Visual Studio
*.user
.vs/
*.suo

# NuGet
packages/
*.nupkg

# Secrets — NEVER commit
appsettings.Development.json
appsettings.Production.json
.env
*.pfx
*.p12
secrets.json

# Logs
*.log
logs/

# Test results
TestResults/
coverage/

# OS
.DS_Store
Thumbs.db

Git Interview Questions

Q1: What is the difference between git merge and git rebase?

merge → creates a merge commit, preserves full history
rebase → rewrites commits on top of target branch, linear history
Use merge for feature → main. Use rebase for keeping feature branch current.

Q2: What is git stash and when do you use it?

Temporarily saves uncommitted changes to a stack.
Use when: need to switch branch but not ready to commit.
git stash → git switch other-branch → do work → git switch back → git stash pop

Q3: What is the difference between git reset --soft and --hard?

--soft → undoes commit, keeps changes STAGED
--mixed (default) → undoes commit, keeps changes UNSTAGED
--hard → undoes commit AND discards all changes (destructive)

Q4: What is HEAD in Git?

HEAD = pointer to your current position (usually current branch tip).
Detached HEAD = HEAD points to specific commit, not a branch.

Q5: How do you resolve a merge conflict?

1. git merge feature-branch → conflict markers appear in files
2. Open conflicted files → choose between <<<< HEAD and >>>> feature
3. Remove conflict markers, keep correct code
4. git add conflicted-file
5. git commit
🤖Use AI to Learn Faster

Use ChatGPT, Claude, or Copilot to go deeper on Git version control for .NET developers. Try these prompts:

  • "What Git commands do I use most in daily .NET development?"
  • "What is the difference between git merge and git rebase?"
  • "How do I resolve a merge conflict in Git?"
  • "Quiz me on Git — ask 5 interview questions about Git"

💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.

nexcoding.in