Lesson 9 of 40 Version Control Beginner 45 min

Git & GitHub Integration

Learn how to use Git confidently inside Visual Studio 2026: create repositories, commit cleanly, stage selected lines, branch safely, sync with GitHub, resolve merge conflicts, review history, and use Copilot to understand changes without losing control of your code.

BranchesWork safely without breaking main
CommitsSave related changes clearly
Pull RequestsReview before merging
Visual Studio 2026 · Git Repository
MAINmain
BRfeature/orders-api
+3Staged changes
PROpen pull request
AIExplain commit diff
Commit
3 files
Build
pass
Tests
pass
Review
2 notes

// Feature branch workflow
git checkout -b feature/orders-api
git commit -m "feat: add orders API"
git push -u origin feature/orders-api
Lesson overview

What you will learn

Git is the safety net for modern development. In this lesson, you will learn the practical Git workflow used by professional teams: create a repository, work on a branch, stage related changes, write meaningful commits, sync with a remote, fix merge conflicts, and create pull requests for review.

Track every changeUnderstand what changed, why it changed, and who changed it.
Collaborate safelyUse branches and pull requests instead of editing main directly.
Review with AIUse Copilot to explain diffs, but verify every change yourself.
Part 1

Why Git matters in Visual Studio projects

Git records snapshots of your project over time. This lets you compare changes, restore previous versions, experiment on branches, and collaborate with others through GitHub or Azure DevOps. Visual Studio brings common Git actions into the IDE so you do not need to leave your project for basic version control work.

Git conceptWhat it meansVisual Studio area
RepositoryThe folder being tracked by GitGit menu, Git Repository window
BranchA separate line of work for a feature or fixBranch selector, Git Repository window
CommitA saved snapshot of selected changesGit Changes window
RemoteA hosted repository such as GitHubFetch, Pull, Push, Sync
Pull requestA review request before merging codeGitHub or Azure DevOps integration
Beginner rule: Commit small, related changes. A commit should tell one clear story.
Part 2

Configure Git before your first commit

Before using Git seriously, check your name, email, default branch preference, diff/merge settings, and repository ignore rules. This avoids confusing commit authorship and prevents generated files from entering source control.

git config --global user.name "Dr. Liew Voon Kiong" git config --global user.email "your-email@example.com" git config --global init.defaultBranch main

For .NET projects, keep compiled output, local IDE files, and secrets out of Git. A simple .gitignore helps protect the repository.

bin/ obj/ .vs/ *.user *.suo appsettings.Development.json *.db *.log
Security habit: Do not commit passwords, API keys, database connection strings, or private certificates. Use user secrets, environment variables, or secure configuration providers.
Part 3

Create, clone, and open repositories

You can start in three common ways: create a new project and initialize Git, clone an existing GitHub repository, or open a folder that already contains a Git repository. When Visual Studio detects a repository, it shows Git status and enables Git tools.

Create a new repo

Open a project, choose Git > Create Git Repository, then select a local folder, GitHub, or Azure DevOps target.

Clone an existing repo

Use File > Clone Repository or the Visual Studio start window, then paste the repository URL.

git clone https://github.com/yourname/storeapp.git cd storeapp git status
Part 4

Use a safe feature-branch workflow

A feature branch lets you work without changing the main branch directly. This is especially important before large refactoring, AI-assisted edits, database migrations, or API changes.

1

Update main

Fetch or pull the latest changes so your branch starts from the current code.

2

Create a branch

Name it clearly, such as feature/orders-api or fix/cart-total-bug.

3

Commit in small steps

Separate unrelated work into different commits using staging and line staging.

4

Push and open a pull request

Ask for review before merging to main.

git switch main git pull git switch -c feature/orders-api # make changes git add . git commit -m "feat: add orders API endpoint" git push -u origin feature/orders-api
Part 5

Stage changes and write better commits

The Git Changes window shows modified, added, deleted, and untracked files. You can stage all files, stage selected files, or stage individual lines. Line staging is useful when one file contains two unrelated changes that should become separate commits.

TaskVisual Studio actionWhy it helps
Stage selected fileGit Changes > plus button beside fileCommit only related changes
Stage selected line or hunkUse line staging in the editor/diff viewSplit mixed edits into clean commits
Review diffOpen file from Git ChangesCatch accidental edits before commit
Amend last commitUse Amend when you forgot a small changeFix the previous commit before sharing
feat: add orders API endpoint fix: correct discount calculation for empty carts refactor: move order validation into service layer test: add unit tests for checkout rules docs: update setup instructions
Good commit message: Explain the intent, not only the file changed. A future reader should understand why the change exists.
Part 6

Fetch, pull, push, and sync with GitHub

When working with a remote repository, your local branch and the GitHub branch can become different. Visual Studio provides buttons and menu actions for fetch, pull, push, and sync.

ActionMeaningWhen to use
FetchDownload remote updates without changing your filesBefore comparing or deciding what to pull
PullFetch and integrate remote changes into your branchBefore continuing work or before pushing
PushUpload your local commits to the remote branchAfter local tests pass
SyncPull then pushWhen you want one combined update operation
Team habit: Pull regularly on long-running branches. The longer a branch stays separate, the more likely conflicts become.
Part 7

Browse history with the Git Repository window

The Git Repository window gives you a broader view of branches, commits, remotes, and history. Use it when you need to compare branches, inspect who changed a file, review old commits, cherry-pick a useful change, or understand what happened before a bug appeared.

Compare commits

Inspect what changed between two commits or branches before merging.

Use Git Blame

Find the last commit that touched a line and read the commit message for context.

Squash local commits

Combine noisy work-in-progress commits before opening a pull request.

Cherry-pick carefully

Bring a specific commit from one branch into another when needed.

Part 8

Resolve merge conflicts carefully

A conflict happens when Git cannot automatically combine changes from two branches. Visual Studio marks the conflicted files and provides a merge editor so you can compare incoming changes, current changes, and the final result.

<<<<<<< HEAD return Results.Ok(await service.GetOrdersAsync(customerId)); ======= return Results.Ok(await service.GetOrdersForCustomerAsync(customerId, includeItems: true)); >>>>>>> feature/include-order-items
  1. Open the conflicted file from the Git Changes or Git Repository window.
  2. Compare current and incoming changes.
  3. Edit the result so the final code is correct, not merely conflict-free.
  4. Build and run tests after resolving all conflicts.
  5. Create the merge commit only after you are confident the result works.
Conflict rule: Do not choose “current” or “incoming” blindly. Understand both sides, then produce the correct final version.
Part 9

Create pull requests for review

A pull request is where you explain what changed, why it changed, and what reviewers should check. Even if you work alone, a pull request-style review helps you catch mistakes before merging to main.

## Summary - Added Orders API endpoint - Added validation for missing customer id - Added unit tests for order lookup rules ## How I tested - Ran all unit tests - Tested GET /api/orders/{id} manually - Checked unauthorized request behavior ## Review notes - Please review error handling and route naming
Keep PRs small

A focused pull request is easier to review than a giant mixed change.

Run tests first

Do not ask others to review code that you already know fails basic tests.

Part 10

Use Copilot with Git responsibly

Copilot can help explain diffs, summarize commits, find missing tests, and draft review notes. Use it as an assistant, not as the final authority. You remain responsible for the code, commit message, pull request description, and merge result.

Explain the staged changes in this branch in beginner-friendly language. Identify risky areas, missing tests, and possible breaking changes. Do not rewrite the code yet.
Draft a concise pull request description for these changes. Include summary, test steps, risk notes, and reviewer checklist. Do not invent completed tests that are not shown in the diff.
Review this merge conflict resolution. Check whether both branches' intended behavior is preserved. Suggest tests I should run before committing the merge.
AI review habit: Ask Copilot to explain risks and missing tests before asking it to generate code.
Part 11

Git habits for professional projects

HabitWhy it mattersExample
Commit before risky editsLets you revert safelyBefore refactoring or Agent Mode
Keep commits focusedMakes review and rollback easierDo not mix formatting, feature, and bug fix
Pull before pushingReduces surprise conflictsSync with main before PR
Review diffs before commitCatches accidental editsInspect generated files and secrets
Use pull requestsEncourages review disciplineExplain what changed and how it was tested

Hands-on exercise: Put the Orders API under Git

  1. Open the project you created in Lessons 5–8, or create a small ASP.NET Core Orders API.
  2. Create a Git repository from Visual Studio if the project is not already tracked.
  3. Add a useful .gitignore and make the first commit.
  4. Create a branch named feature/orders-validation.
  5. Add simple validation logic and one test.
  6. Stage only the related files and write a clear commit message.
  7. Push the branch to GitHub and draft a pull request description.
  8. Ask Copilot to review the diff for missing tests, then manually verify the suggestions.
Visual Studio 2026 Made Easy book cover
Recommended companion book

Visual Studio 2026 Made Easy

Visual Studio 2026 Made Easy gives you a structured path for learning Visual Studio, C#, .NET, debugging, testing, web development, databases, profiling, Git source control, deployment, and AI-assisted workflows.