Tuesday, November 26, 2024

How to Copy Git Repository Without History

There are several methods to do this using git clone, git push or using git archive. But I personally prefer the one using git clone.

Objective is to copy repo 1 which is source repo to a new Repo which is NewRemote repo with out commit history.

Precautions before you proceed with this:

  1. Ensure you have write access to the repository.
  2. Backup any important local changes before proceeding.
  3. This will permanently remove the old commit history.
  4. Collaborators will need to re-clone the repository.

Here are step by step git examples for this specific repo

# 1. Clone the source repository
git clone https://github.com/inagasai/SourceRepo.App.git

# 2. Enter the cloned repository directory
cd vGlence.App

# 3. Verify current branches
git branch -a

# 4. Checkout master branch
git checkout master

# 5. Create a new branch without history
git checkout --orphan clean-main

# 6. Add all files to the new branch
git add .

# 7. Commit the files with a new initial commit
git commit -m "Initial commit - reset repository history"

# 8. Delete the old main branch (if it exists)
git branch -D main 2>/dev/null

# 9. Rename current branch to main
git branch -m main

# 10. Remove the original remote
git remote remove origin

# 11. Add the original repository as a new remote
git remote add origin https://github.com/inagasai/NewRemote.App.git

# 12. Force push to overwrite the remote repository
git push -f origin main
  

Detailed Breakdown of the outcome:

  1. This process creates a new branch with no commit history.
  2. It adds all existing files to a new initial commit.
  3. Force pushes to overwrite the remote repository.
  4. Removes all previous commit history.

Hope this helps.

Monday, November 04, 2024

Using multiple environments in ASP.NET Core

ASP.NET Core configures app behavior based on the runtime environment using an environment variable.

IHostEnvironment.EnvironmentName can be set to any value, but the following values are provided by the framework:

  • Development : The launchSettings.json file sets ASPNETCORE_ENVIRONMENT to Development on the local machine.
  • Staging
  • Production : The default if DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT have not been set.

When comparing appsettings.development.json and appsettings.json, the key difference lies in their deployment environments. appsettings.development.json is typically used for development and testing environments, whereas appsettings.json is used for production environments.

The .development.json file contains sensitive information such as database credentials and API keys, which are not committed to source control and are generated locally. In contrast, appsettings.json contains non-sensitive configuration settings that are committed to source control and used in production.

Here is how this con be done in Program.cs file

public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();

        }

        public static IWebHost BuildWebHost(string[] args) =>
         WebHost.CreateDefaultBuilder(args)
           .UseStartup<Startup>()
           .ConfigureAppConfiguration((context, config) =>
           {
               var env = context.HostingEnvironment;
               config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                     .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
           })
           .Build();
    }
  

Here is sample appsettings.json file

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "ApiSettings": {
  },
  "AllowedHosts": "*",
  "isLocal": "1",
  "Email": {
  },
  "LanguageService": {
  }
}
  

These appsettings.json or appsettings.staging.json or appsettings.production.json can be set from launchSettings.json

Here is how it looks

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:44459",
      "sslPort": 44393
    }
  },
  "profiles": {
    "Client.PWA": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5198",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development" //Development//Staging//Production
      }
    }
  }
}
  

ASPNETCORE_ENVIRONMENT in above launchsettings.json will determine which configuration it needs to pick. In above case, its looking for Development settings, here I have Staging and Production configured as well.

This approach helps maintain secure practices while allowing for different configuration settings between environments.