https://github.com/bigbang1112/simplest-git-source-generator
Simplest solution to get Git information in your .NET apps.
https://github.com/bigbang1112/simplest-git-source-generator
branch build commit csharp dotnet git metadata msbuild roslyn source-generator version
Last synced: 6 months ago
JSON representation
Simplest solution to get Git information in your .NET apps.
- Host: GitHub
- URL: https://github.com/bigbang1112/simplest-git-source-generator
- Owner: BigBang1112
- License: mit
- Created: 2026-01-13T00:22:20.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2026-01-13T20:03:15.000Z (6 months ago)
- Last Synced: 2026-01-18T15:47:26.802Z (6 months ago)
- Topics: branch, build, commit, csharp, dotnet, git, metadata, msbuild, roslyn, source-generator, version
- Language: C#
- Homepage:
- Size: 46.9 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SimplestGitSourceGenerator
[](https://www.nuget.org/packages/SimplestGitSourceGenerator/)
[](https://github.com/BigBang1112/simplest-git-source-generator/releases)
Happy to showcase the simplest solution for retrieving Git information in your .NET apps.
## Requirements
- Project must be a Git repository with at least 1 commit
- Git must be installed
If these requirements are not fulfilled, the build will not pass!
## Usage
Install the NuGet package:
```
dotnet add package SimplestGitSourceGenerator
```
**Code:**
```cs
using SimplestGitSourceGenerator;
Console.WriteLine(SimplestGit.CommitHash);
Console.WriteLine(SimplestGit.CommitDate);
Console.WriteLine(SimplestGit.Branch);
Console.WriteLine(SimplestGit.Tag);
```
If you want to include remote URL as well, you can enable it via property:
```xml
true
```
Once that's done, you can access it via `SimplestGit.RemoteUrl`.
### Docker build
Ensure that you remove `.git` folders from your `.dockerignore` file and that you multi-stage your build so that the `.git` folder is not included in the final image.
Make sure to install `git` before building your project (Alpine example):
```dockerfile
RUN apk add --no-cache git
```
## How does it work?
Before the source generator kicks in, several Git commands are called and stored as compiler properties before they are baked into the code.
- `git rev-parse HEAD` (`SimplestGitCommitHash`)
- `git rev-parse --abbrev-ref HEAD` (`SimplestGitBranch`)
- `git log -1 --format=%cI` (`SimplestGitCommitDate`)
- `git describe --tags --always` (`SimplestGitTag`)
- `git ls-remote --get-url` (`SimplestGitRemoteUrl`)
These commands must complete with the exit code 0, otherwise, the build will not pass. All of that is done thanks to the `build/SimplestGitSourceGenerator.targets` file.
This is a simpler alternative to SourceLink or GitInfo packages if you don't mind requiring the Git dependency.