You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git提交时如何忽略文件或代码片段(如数据库连接串)?

Hey there! Let's break down how to handle your environment-specific database connection strings with Git, plus clear up how .gitignore actually works so you don't run into issues.

First: Why your initial .gitignore might not have worked

Before diving into solutions, let's clarify .gitignore basics:

  • .gitignore tells Git which files/paths to exclude from version control—meaning it won't track changes to them or add them to commits.
  • Critical note: If you already committed DBConnection.cs to Git before adding it to .gitignore, Git will keep tracking it! The file is already in Git's history, so .gitignore rules won't apply retroactively. We'll fix that in the solutions below.

Solution 1: Use Environment Variables (Most Flexible & Secure)

Instead of hardcoding connection strings or ignoring the entire file, pull environment-specific values from your system's environment variables. This keeps sensitive data out of your code entirely.

Modify your code to read from environment variables (with sensible defaults as fallbacks):

// Informations - DB 
private static string _SERVER => Environment.GetEnvironmentVariable("DB_SERVER") ?? "localhost";
private static string _DATABASE => Environment.GetEnvironmentVariable("DB_DATABASE") ?? "dbname";
private static string _UID => Environment.GetEnvironmentVariable("DB_UID") ?? "";
private static string _PASSWORD => Environment.GetEnvironmentVariable("DB_PASSWORD") ?? "";

// DB connection 
public static MySqlConnection v_DBConnection;
public static void InitializeDB() { // Fixed typo from "InititializeDB"
    string v_ConnectionString = null;
    MySqlConnectionStringBuilder v_MySqlConnectionStringBuilder;
    try {
        v_MySqlConnectionStringBuilder = new MySqlConnectionStringBuilder();
        v_MySqlConnectionStringBuilder.Server = _SERVER;
        v_MySqlConnectionStringBuilder.UserID = _UID;
        v_MySqlConnectionStringBuilder.Password = _PASSWORD;
        v_MySqlConnectionStringBuilder.Database = _DATABASE;
        
        v_ConnectionString = v_MySqlConnectionStringBuilder.ToString();
        v_DBConnection = new MySqlConnection(v_ConnectionString);
    } catch (Exception exception) {
        MessageBox.Show("InitializeDB failed due to:" + exception.ToString());
    }
}

Then set the environment variables on each computer:

  • Windows: Go to System Properties → Advanced → Environment Variables, then add new system/user variables for DB_SERVER, DB_DATABASE, etc.
  • Mac/Linux: Add lines like export DB_SERVER="your-local-server" to your ~/.bashrc or ~/.zshrc file, then restart your terminal.

Solution 2: Use a Template File (Good for Shared Structure)

If you want to share the file structure but keep individual configs private:

  1. If you already committed DBConnection.cs: First remove it from Git's tracking (this doesn't delete the file locally):
    git rm --cached DBConnection.cs
    
  2. Create a template file DBConnection.template.cs with placeholder values, and commit this to Git:
    // Informations - DB 
    private const string _SERVER = "[YOUR_LOCAL_SERVER]";
    private const string _DATABASE = "[YOUR_DB_NAME]";
    private const string _UID = "[YOUR_USER_ID]";
    private const string _PASSWORD = "[YOUR_PASSWORD]";
    
    // DB connection 
    public static MySqlConnection v_DBConnection;
    public static void InitializeDB() {
        string v_ConnectionString = null;
        MySqlConnectionStringBuilder v_MySqlConnectionStringBuilder;
        try {
            v_MySqlConnectionStringBuilder = new MySqlConnectionStringBuilder();
            v_MySqlConnectionStringBuilder.Server = _SERVER;
            v_MySqlConnectionStringBuilder.UserID = _UID;
            v_MySqlConnectionStringBuilder.Password = _PASSWORD;
            v_MySqlConnectionStringBuilder.Database = _DATABASE;
            
            v_ConnectionString = v_MySqlConnectionStringBuilder.ToString();
            v_DBConnection = new MySqlConnection(v_ConnectionString);
        } catch (Exception exception) {
            MessageBox.Show("InitializeDB failed due to:" + exception.ToString());
        }
    }
    
  3. Add DBConnection.cs to your .gitignore file:
    # Ignore user-specific DB config
    DBConnection.cs
    
  4. Commit the template and .gitignore changes:
    git add DBConnection.template.cs .gitignore
    git commit -m "Add DB connection template and ignore user-specific config"
    
  5. On each computer, copy DBConnection.template.cs to DBConnection.cs and fill in your own connection details—Git will ignore this local file entirely.

Solution 3: Ignore the Entire File (Quick Fix)

If you don't need to share the DBConnection.cs structure at all:

  1. If the file was already committed, run git rm --cached DBConnection.cs to stop Git tracking it.
  2. Add DBConnection.cs to .gitignore (as shown above).
  3. Commit the .gitignore change.

This way, each developer maintains their own DBConnection.cs file locally, and it never gets pushed to the Git repo.

内容的提问来源于stack exchange,提问作者InFiniTy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:55:54