ProgrammingDude
ProgrammingDude

Reputation: 51

Is it possible to ignore changes in git?

I want to ignore a specific change in my file, but I can only find suggestions to ignore an entire file. Or is there a better way to do so?

The problem is that if I make a change to that ignored file, I cannot commit it without committing the change that should be ignored.

I also work with Visual Studio Code. Is there a way to do that using the editor?

Upvotes: 0

Views: 3256

Answers (5)

BenKoshy
BenKoshy

Reputation: 35731

I know you asked for a Git based answer, but I don't think that is your real problem!

Solution: use an existing tool to manage variables in different environments

The key to solving this problem is to load and manage environmental variables based on on different environments that you might be running: testing, production, development, features etc.

There are existing tools out there like: Figaro, and dotenv which can solve your problem - and if you are not using Ruby, or Rails, you will likely find equivalents of the above for your specific language/platform.

Upvotes: 1

Max Yankov
Max Yankov

Reputation: 13327

I can only find suggestions to ignore an entire file

Although other answers solve your particular problem better, the answer to the question, is it possible to ignore some portions of the file in the same way you can ignore entire files in .gitignore? is no.

You can choose to ignore or add certain changes to a particular file with git --patch -- fileName.txt, but you will have to do it every time you stage changes. There's no way to mark some lines in the file as ignored forever the same way .gitignore marks entire files.

Upvotes: 0

chelmertz
chelmertz

Reputation: 20601

A common way to solve this, is to have your application code always read your "base config file" and try to read an "override config file". The latter overrides every config option found and falls back to the base for options not existing in the "override".

The base config file is checked in, the other is not.

Upvotes: 2

samthegolden
samthegolden

Reputation: 1510

You can discard in the left pane file options:

enter image description here

And then make your changes in the file.

You can also do a git stash, then make your changes, commit them and discard the old changes with git stash drop.

Upvotes: 0

rioV8
rioV8

Reputation: 28868

git works on file level in the repository, not change level.

  • stash the file
  • you now have the file without your "do not commit"
  • make you needed changes
  • commit this file
  • unstash the file
  • this will result in a merge, with possible merge conflict to resolve if your "do not commit" part overlaps the needed changes
  • the new file stays modified

Upvotes: 0

Related Questions