Home > other >  Avoid overwritte files when .git push with .gitignore
Avoid overwritte files when .git push with .gitignore

Time:10-06

I have a project that is worked in local and then in cloud. When we are in local we need to do some changes in some file to work well, and then rewrite this files to upload again when make the git push.

For example a file:

'use strict';
const mysql = require('mysql');

/* const dbConn = mysql.createPool({
  connectionLimit: 5,
  host: process.env.MYSQL_CONNECTION_STRING.split(':')[0],
  user: process.env.MYSQL_USER,
  password: process.env.MYSQL_PASSWORD,
  database: process.env.MYSQL_CONNECTION_STRING.split('/')[1],
  charset: 'utf8mb4'
});
module.exports = dbConn; */

const dbConn = mysql.createPool({
  connectionLimit: 5,
  host: 'localhost',
  user: 'root',
  password: 'Mraixa2015L',
  database: 'tool_bbdd',
  charset: 'utf8mb4'
});
module.exports = dbConn;

in local use mysql local with my credentials in cloud the others, and everytime i have to comment and uncomment to work.

this is one example, but i need to do things like this in other files.

I think if is possible to include in .gitignore file this files. Inside my .gitignore

config/db.config.js

And when i make the git push, not upload this changes and not overwritte the data to use then in the cloud.

is it possible? Thanks

CodePudding user response:

The point is not about pushing. What will be pushed is what is the the commits.... whatever it is. Git can't just remove a file from a commit to push. What you should care about is about committing it in the first place.

If the file is already tracked, .gitignore makes no difference. You can ask git to ignore it if it is already tracked with git update-index --assume-unchanged.... or, what I do sometimes, is keep those changes in a private branch.... so you can "easily" apply them / unapply them

git show X/some-private-branch | git apply # boom! I have my changes there
# when I want to remove them
git show X/some-private-branch | git apply -r # The change is gone

CodePudding user response:

The situation here is that you want to ignore some changes to tracked files with Git. Unfortunately, as the Git FAQ mentions, there's no way to do that with Git. Specifically, using git update-index for this purposes doesn't work properly:

It’s tempting to try to use certain features of git update-index, namely the assume-unchanged and skip-worktree bits, but these don’t work properly for this purpose and shouldn’t be used this way.

The easiest way to solve this problem is to include two separate files, one which has the production values and one which has the development values, both with names independent of the actual desired location, and then use a script to copy the correct file into the desired location, which is ignored (and not tracked). You can even have that script adjust the development or production values to include additional data that's appropriate based on things like environment variables.

  • Related