As a developer, you spend a lot of time coding, debugging, and building applications. That's great, but to keep track of your code versions or collaborate with other team members, Git is usually your main tool. Since its release in 2005, Git has become the go-to version control software. StackOverflow has found that almost 94% of developers use it for their codebase.

The Birth of Git

In the early 2000s, the Linux kernel, THE most known open-source operating system kernel, was one of the largest collaborative software projects in the world. Developers from all over the globe contributed to the project, making it essential to have a system for tracking changes, managing contributions, and coordinating development efforts.

Initially, the Linux kernel project used a version control system called BitKeeper. However, in 2005, the relationship between the Linux community and BitKeeper's creators deteriorated due to several issues (we can write about that drama in another post), leading to the need for a new system. Linus Torvalds, the creator of Linux, decided to develop a new version control system that would meet the project's unique needs.

Key Design Goals

When Linus Torvalds released Git in April 2005, he had a few key goals in mind:

  • Speed: The system needed to be fast to handle the large number of changes and branches in the Linux kernel.

  • Simple Design: Linus wanted a system that was simple and straightforward, without unnecessary complexity.

  • Strong Support for Non-linear Development: Given the nature of open-source contributions, the system had to support multiple branches and merges effectively.

  • Distributed Development: The system should allow each developer to have a full copy of the project history, enabling work even without network access.

  • Integrity: The integrity of the source code history was very important, ensuring that it was impossible to modify past versions without it being detected.

The Rise of Git

Within just a few days, Linus had developed a basic working prototype of Git, and the first official release came on April 7, 2005. Git quickly proved itself to be a powerful tool, and by June 2005, it had been adopted by the Linux kernel project.

One of the key innovations of Git was its use of a directed acyclic graph (DAG) to represent the history of changes. This structure made it easy to track the progression of changes, branch out new lines of development, and merge them back into the main line without losing history.

Why Git Matters for .NET Developers

Git is a powerful version control system that helps you manage your source code history. It allows multiple developers to work on the same codebase without stepping on each other's toes, most of the time. Git also makes it easy to track changes, revert to previous versions, and branch out to experiment with new features.

For .NET developers, Git integrates seamlessly with Visual Studio, Visual Studio Code and Rider, making it an excellent fit for managing your .NET projects.

Getting Started with Git

First things first, you need to have Git installed on your machine. You can download it from git-scm.com.
Once Git is installed, you can start by creating a new repository. A Git repository is where your project’s history is stored.

Create a New Repository: Open your project in Visual Studio. Right-click on your solution in the Solution Explorer, go to "Add," and select "Add to Source Control." This initializes a new Git repository in your project directory.

Commit Your Code: After initializing the repository, you'll want to commit your current code. Committing is like taking a snapshot of your project at a certain point in time. To commit, click on the "Team Explorer" tab, write a commit message describing your changes, and hit "Commit All."

Basic Git Commands Every .NET Developer Should Know

Here are some of the most commonly used Git commands that you'll use in your day-to-day workflow:

git init: Initializes a new Git repository.
git clone [url]: Clones an existing repository from a remote location (like GitHub) to your local machine.
git status: Shows the status of your working directory and staging area.
git add [file]: Adds a file to the staging area, preparing it for a commit.
git commit -m "message": Commits your staged changes with a message.
git fetch: It downloads all refs and objects and any new branches to your local Repository.
git pull: Fetches and merges changes from the remote repository to your local repository.
git push: Pushes your local commits to the remote repository.
git merge [branch]: Merges the specified branch into the current branch, creating a new merge commit.
git rebase [branch]: Reapplies commits on top of another base tip, effectively moving or combining commits to a new base commit.

Branching in Git

Branching is one of the most powerful features of Git. It allows you to create a separate line of development for new features, bug fixes, or experiments without affecting the main codebase.

  • Create a Branch: To create a new branch, use the git branch [branch-name] command or simply use the Visual Studio GUI by selecting "New Branch" from the "Branches" tab.

  • Switching Branches: To switch to a different branch, use git checkout [branch-name] or click on the branch name in Visual Studio.

  • Merging Branches: Once your work on a branch is complete, you’ll want to merge it back into the main branch. Use git merge [branch-name] to merge changes, or use the "Merge" option in Visual Studio.

  • Working with Remote Repositories
    Most .NET projects involve collaboration with other developers, and that’s where remote repositories come into play. GitHub is the most popular platform for hosting Git repositories, but GitLab and Bitbucket are also widely used.

  • Connecting to a Remote Repository: If you haven’t already linked your project to a remote repository, you can do so in Visual Studio by clicking on "Sync" in the "Team Explorer" and adding the remote URL.

  • Pushing Changes: After committing changes locally, use git push or the "Push" button in Visual Studio to send your changes to the remote repository.

  • Pulling Changes: Similarly, if you want to update your local code with the latest changes from the remote repository, use git pull.

Handling Merge Conflicts

Merge conflicts happen when two developers make changes to the same line of code in different branches. Git will ask you to resolve these conflicts before you can complete the merge.

Modern IDEs like Rider or Visual Studio provide an easy-to-use merge conflict resolution tool. They highlight the conflicting areas of the code and allow you to choose which changes to keep.

Best Practices for .NET Developers Using Git

  1. Commit Often: Make frequent, small commits. This makes it easier to track changes and revert if needed.

  2. Use Meaningful Commit Messages: A good commit message explains what changes were made and why. This is especially helpful when working in teams.

  3. Leverage Branches: Don’t be afraid to create branches for new features or bug fixes. This keeps your main branch clean and stable.

  4. Regularly Pull from Remote: Before starting your work, pull the latest changes from the remote repository to avoid conflicts later.

  5. Review Pull Requests: If you’re working on a team, make sure to review pull requests carefully before merging them into the main branch.

Final Thoughts

Git is very important for all .NET developers, helping with collaboration and efficient code management. Mastering the basics of Git is a must-have since it is widely used by companies worldwide. Now I want to know from you! Are you using git? Do you want to learn more about the drama that led to its creation? Thanks for reading and as always, keep coding.

Next steps

If you're interested in mastering Git, you can always check our "From Zero to Hero: Git" course on Dometrain.

cover.jpg