A Comprehensive Guide to Git Basics: Version Control and Collaboration for Software Projects

Git commands example for version control
Reading Time: 2 minutes

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:

Check .NET Core SDK version:

dotnet --version
Bash

Check Git version:

git --version
Bash

2. Initialize a .NET Core Project

Let’s create a new .NET Core application:

Steps:

  1. Open a terminal (or command prompt).
  2. Create a new .NET Core project:
dotnet new console -n GitDemoApp
cd GitDemoApp
Bash

Expected Output:

The template "Console App" was created successfully.
Bash

3. Initialize Git in the Project

After setting up your project, initialize a Git repository:

Steps:

  1. Inside the GitDemoApp folder, initialize a Git repository:
git init
Bash

Expected Output:

Initialized empty Git repository in /path/to/GitDemoApp/.git/
Bash

4. Add Files to the Repository

You need to add the files in your project to the staging area.

Command:

git add .
Bash

Expected 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
Bash

5. Commit Your Changes

Once the files are staged, commit the changes:

Command:

git commit -m "Initial commit - .NET Core Console App"
Bash

Expected Output:

[main (root-commit) 57f0e12] Initial commit - .NET Core Console App
 1 file changed, 5 insertions(+)
 create mode 100644 Program.cs
Bash

6. Create and Switch Branches

Create a new branch to work on a new feature.

Command:

git checkout -b feature/add-greeting
Bash

Expected Output:

Switched to a new branch 'feature/add-greeting'
Bash

7. 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"
Bash

Expected Output:

[feature/add-greeting 1b2c3d4] Added greeting feature
 1 file changed, 1 insertion(+), 1 deletion(-)
Bash

8. 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
Bash

Expected Output:

Updating 57f0e12..1b2c3d4
Fast-forward
 Program.cs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Bash

9. Push the Changes to a Remote Repository

To push your changes to a remote repository (like GitHub, GitLab, or Bitbucket):

  1. Add the remote repository URL:
git remote add origin https://github.com/your-username/GitDemoApp.git
Bash

2. Push the main branch to the remote repository:

git push -u origin main
Bash

Expected 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
Bash

10. Checking the Repository Status and History

Command for status:

git status
Bash

Expected Output:

On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
Bash

Command for log:

git log --oneline
Bash

Expected Output:

1b2c3d4 (HEAD -> main, origin/main) Added greeting feature
57f0e12 Initial commit - .NET Core Console App
Bash

Conclusion

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!

FAQ

Git is a distributed version control system that helps developers track changes, collaborate on code, and manage multiple project versions efficiently. It’s crucial for team-based development and maintaining a history of code changes.
Use the git init command inside your project folder to create a new Git repository. This sets up the .git directory where Git will store all version control data.
Branching allows developers to work on features or fixes independently of the main codebase. This ensures changes can be made and tested without affecting the stable version.
After staging your changes with git add, use the git commit -m “Your message” command to save a snapshot of the changes in the repository.
When a merge conflict occurs, Git will highlight conflicting lines in your files. Open the files, manually resolve the conflicts, and mark them as resolved using git add. Finally, commit the changes with git commit.
When a merge conflict occurs, Git will highlight conflicting lines in your files. Open the files, manually resolve the conflicts, and mark them as resolved using git add. Finally, commit the changes with git commit.
git fetch downloads changes from a remote repository without integrating them, while git pull fetches and merges changes into your current branch.
Yes, you can undo commits using commands like git reset (to undo locally) or git revert (to create a new commit that undoes the previous changes).
Use the git push command after linking your repository to a remote using git remote add origin .