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:
.gitignoretells 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.csto Git before adding it to.gitignore, Git will keep tracking it! The file is already in Git's history, so.gitignorerules 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~/.bashrcor~/.zshrcfile, 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:
- 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 - Create a template file
DBConnection.template.cswith 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()); } } - Add
DBConnection.csto your.gitignorefile:# Ignore user-specific DB config DBConnection.cs - Commit the template and
.gitignorechanges:git add DBConnection.template.cs .gitignore git commit -m "Add DB connection template and ignore user-specific config" - On each computer, copy
DBConnection.template.cstoDBConnection.csand 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:
- If the file was already committed, run
git rm --cached DBConnection.csto stop Git tracking it. - Add
DBConnection.csto.gitignore(as shown above). - Commit the
.gitignorechange.
This way, each developer maintains their own DBConnection.cs file locally, and it never gets pushed to the Git repo.
内容的提问来源于stack exchange,提问作者InFiniTy

