Beginner 7 min read

Git Basics: init, add, commit, branch, merge and rebase

Learn Git fundamentals, including initializing repositories, staging and committing changes, creating branches, merging code, and rebasing branches in a .NET development workflow.

Git Basics: init, add, commit, branch, merge and rebase

Git is a distributed version control system that helps developers track code changes, maintain project history, and collaborate without overwriting one another's work. For .NET developers, Git is an essential tool for managing C# applications, ASP.NET Core projects, bug fixes, and new features.

Instead of keeping multiple copies of a project to preserve earlier versions, Git records changes as commits. You can create separate branches for development, compare changes, and combine work when a feature is ready. Understanding Git basics helps you work confidently with teams, GitHub, and CI/CD pipelines.

1. Initializing a Repository with git init

A Git repository is a project directory whose changes are tracked by Git. The git init command initializes a new repository in the current directory by creating the internal .git directory.

Navigate to your project folder and run:

git init

Git now has a repository in which you can track changes and create commits.

To check the current repository status, use:

git status

This command shows which files are untracked, modified, staged, or ready to commit.

If you are starting a new .NET project, you can initialize Git directly inside its project directory. Before your first commit, create a suitable .gitignore file to prevent generated files, build outputs, and local configuration secrets from being tracked.

2. Staging and Committing Changes

Git separates saving changes into two important steps: staging and committing.

Staging selects the changes you want to include in your next commit. Committing records those staged changes in the repository's history.

git add — Stage Changes

To stage a specific file, run:

git add Program.cs

To stage all changes in the current directory and its subdirectories, use:

git add .

Review the staged changes before committing:

git status
git diff --staged

The first command displays the repository status, while the second shows the differences included in the staging area.

git commit — Record Changes

Once your changes are staged, create a commit:

git commit -m "Add product validation"

A commit records a snapshot of the staged changes along with a message describing the work.

Good commit messages explain the purpose of a change rather than simply stating that files were updated. For example, Fix API validation for invalid product IDs communicates more useful information than Changes done.

Remember: Git commits are local until you push them to a remote repository, such as GitHub.

3. Creating and Switching Branches

A branch allows you to develop a feature or fix a bug independently of other work. Branches are especially useful when multiple developers contribute to the same .NET application.

Create a new branch with:

git branch feature/product-api

Switch to that branch using:

git switch feature/product-api

You can combine these operations into one command:

git switch -c feature/product-api

To list local branches, run:

git branch

The current branch is marked with an asterisk.

For example, you might keep main as your primary branch and create feature/product-api to implement a new ASP.NET Core endpoint. Your changes remain isolated from other branches until you integrate them.

4. Merging Branches with git merge

Merging combines changes from one branch into another. It is commonly used when a feature is complete and ready to be integrated into the main development branch.

First, switch to the branch that should receive the changes:

git switch main

Then merge the feature branch:

git merge feature/product-api

Git combines the changes when it can do so safely. Depending on the commit history, the merge may create a merge commit or perform a fast-forward update.

What Is a Merge Conflict?

A merge conflict occurs when Git cannot automatically reconcile competing changes, such as when two branches modify the same lines of a file differently.

To resolve a conflict:

  1. Open each conflicted file.
  2. Review the conflicting changes.
  3. Edit the file to keep the intended code.
  4. Stage the resolved file using git add.
  5. Complete the merge if Git requires a merge commit.

For example:

git add Program.cs
git commit

The final commit command is needed when the merge is still awaiting a merge commit. If Git completed a fast-forward merge or the merge commit was created automatically, an additional commit is unnecessary.

5. Rebasing Branches with git rebase

Rebasing moves or replays commits from your current branch onto a different base commit. It is often used to incorporate the latest changes from a shared branch while maintaining a more linear project history.

Suppose you are working on feature/product-api and want to rebase it onto the latest main branch.

First, update your local main branch as appropriate, then run:

git switch feature/product-api
git rebase main

Git replays your feature branch's commits on top of main.

If a conflict occurs, resolve the affected files, stage the resolutions, and continue:

git add Program.cs
git rebase --continue

To cancel an in-progress rebase and return to its original state, use:

git rebase --abort

Merge vs. Rebase

Featuregit mergegit rebase
PurposeIntegrates changes from another branch.Replays commits onto a new base.
HistoryPreserves branch history and may create a merge commit.Creates new commit identities for replayed commits.
Commit historyCan contain branching and merge history.Often produces a more linear history.
Conflict resolutionConflicts may need to be resolved during the merge.Conflicts may need to be resolved as individual commits are replayed.
Best useIntegrating completed work while preserving merge history.Updating a feature branch before integration.

Important: Avoid rebasing commits that other developers already rely on unless your team has agreed to rewrite that history. Rebasing changes commit identities and can complicate collaboration.

6. A Practical Git Workflow for .NET Developers

Imagine you are adding a new product endpoint to an ASP.NET Core Web API. A typical workflow might look like this:

git switch main
git switch -c feature/product-api

# Make changes to your C# files
git status
git add .
git commit -m "Add product API endpoint"

# Integrate the feature into main
git switch main
git merge feature/product-api

This workflow creates a separate branch, records your changes in a commit, and merges the completed feature into main.

In a team environment, you would typically update your local branches, review the changes, and open a pull request before integrating code into the shared branch.

For .NET projects, a .gitignore file should generally exclude generated directories such as bin/ and obj/. Never commit passwords, API secrets, or sensitive environment configuration files.

Key Takeaways

  • git init initializes a Git repository.
  • git add stages changes, while git commit records them in the repository history.
  • git branch lists or creates branches, and git switch changes the current branch.
  • git merge integrates changes from one branch into another.
  • git rebase replays commits onto a different base to create a potentially more linear history.
  • Merge conflicts require you to review and resolve competing changes.
  • Git helps .NET developers collaborate, track changes, and maintain a reliable development history.

Frequently Asked Questions

What is Git, and why do .NET developers use it?

Git is a distributed version control system that tracks changes in source code. .NET developers use it to manage project history, develop features on separate branches, collaborate with teammates, and integrate code safely.

What is the difference between git add and git commit?

git add stages selected changes for the next commit. git commit records the staged changes in the repository's history.

What is the difference between git merge and git rebase?

git merge integrates changes from another branch while preserving the existing branch history. git rebase replays commits onto a different base, often creating a more linear history with new commit identities.

What is a Git branch?

A Git branch is a movable reference to a commit that provides an independent line of development. Developers use branches to build features and fix bugs without making immediate changes to the main development branch.

What is a merge conflict in Git?

A merge conflict occurs when Git cannot automatically combine competing changes. Developers must inspect the affected files, resolve the differences, stage the resolved files, and complete the merge or rebase.

Related Topics

  • Terminal and Command Line Basics
  • The .NET CLI
  • VS Code and C# Dev Kit
  • GitHub Basics and Remote Repositories
  • Pull Requests and Code Reviews
  • CI/CD for .NET Applications

Last updated 10/26/2026

Your notes

Sign in to save personal notes on this topic — they'll sync across your devices.

Was this tutorial helpful?

Help us create more free learning resources. Learn more

Make a Donation