Master Git basics with this comprehensive guide. Whether you’re initializing repositories, committing changes, or managing branches, this tutorial covers the foundational tools and techniques you need for streamlined software collaboration.
1. Install Git and .NET Core SDK
Before starting, ensure Git and .NET Core SDK are installed on your system:
- Install Git: Download Git
- Install .NET Core SDK: Download .NET Core
Check .NET Core SDK version:
dotnet --version
BashCheck Git version:
git --version
Bash2. Initialize a .NET Core Project
Let’s create a new .NET Core application:
Steps:
- Open a terminal (or command prompt).
- Create a new .NET Core project:
dotnet new console -n GitDemoApp
cd GitDemoApp
BashExpected Output:
The template "Console App" was created successfully.
Bash3. Initialize Git in the Project
After setting up your project, initialize a Git repository:
Steps:
- Inside the
GitDemoApp
folder, initialize a Git repository:
git init
BashExpected Output:
Initialized empty Git repository in /path/to/GitDemoApp/.git/
Bash4. Add Files to the Repository
You need to add the files in your project to the staging area.
Command:
git add .
BashExpected Output from git status
:
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: Program.cs
Bash5. Commit Your Changes
Once the files are staged, commit the changes:
Command:
git commit -m "Initial commit - .NET Core Console App"
BashExpected Output:
[main (root-commit) 57f0e12] Initial commit - .NET Core Console App
1 file changed, 5 insertions(+)
create mode 100644 Program.cs
Bash6. Create and Switch Branches
Create a new branch to work on a new feature.
Command:
git checkout -b feature/add-greeting
BashExpected Output:
Switched to a new branch 'feature/add-greeting'
Bash7. Make Changes and Commit
Let’s say you want to add a “greeting” feature to your .NET Core
app. Open Program.cs
and update it with a new greeting message.
Modify Program.cs
:
using System;
namespace GitDemoApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, Git!");
}
}
}
C#Command:
git add .
git commit -m "Added greeting feature"
BashExpected Output:
[feature/add-greeting 1b2c3d4] Added greeting feature
1 file changed, 1 insertion(+), 1 deletion(-)
Bash8. Merge the Feature Branch
Once the feature is complete, merge it back into the main
branch.
Command:
git checkout main
git merge feature/add-greeting
BashExpected Output:
Updating 57f0e12..1b2c3d4
Fast-forward
Program.cs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Bash9. Push the Changes to a Remote Repository
To push your changes to a remote repository (like GitHub, GitLab, or Bitbucket):
- Add the remote repository URL:
git remote add origin https://github.com/your-username/GitDemoApp.git
Bash2. Push the main
branch to the remote repository:
git push -u origin main
BashExpected Output:
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 456 bytes | 456.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://github.com/your-username/GitDemoApp.git
* [new branch] main -> main
Bash10. Checking the Repository Status and History
Command for status:
git status
BashExpected Output:
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
BashCommand for log:
git log --oneline
BashExpected Output:
1b2c3d4 (HEAD -> main, origin/main) Added greeting feature
57f0e12 Initial commit - .NET Core Console App
BashConclusion
In this article, we’ve explored Git basics for a .NET Core project. You’ve learned how to initialize a repository, add and commit files, create branches, merge changes, and push to a remote repository. These commands and their outputs should help you understand how Git works in practice.
To deepen your Git skills, explore Git’s official documentation. Start using Git today and take control of your software projects!