0

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?

  • Perhaps there's a way to hack into git to maker it work but this would be like trying to ski up hill. What are you trying to do? – BenKoshy Mar 26 '20 at 11:02
  • I have a local environment in which the access data of my database is different than in the live environment. – ProgrammingDude Mar 26 '20 at 11:29
  • You have five grained access to what you can add to the staging area. `git add -i`. Now you can add the changes you want, while leaving other changes. But to solve your data access problem consider using environmental variables. This is a common problem. Consider the ruby gem `dotenv` - there should be similar solutions for your particular programming language or preferred platform – BenKoshy Mar 26 '20 at 15:58
  • 1
    Does this answer your question? [Committing Machine Specific Configuration Files](https://stackoverflow.com/questions/1396617/committing-machine-specific-configuration-files) – phd Mar 26 '20 at 18:17
  • https://stackoverflow.com/search?q=%5Bgit%5D+track+files+ignore+changes – phd Mar 26 '20 at 18:18
  • This question has been marked as duplicate, and if the title would be about configuration files, it would be. But as it is now, about ignoring some parts of the files forever, I don't think I've seen the same exact question on SO, so I don't think it should be closed. – Max Yankov Mar 26 '20 at 21:44

5 Answers5

2

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.

chelmertz
  • 20,399
  • 5
  • 40
  • 46
1

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.

BenKoshy
  • 33,477
  • 14
  • 111
  • 80
0

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
Paolo
  • 21,270
  • 6
  • 38
  • 69
rioV8
  • 24,506
  • 3
  • 32
  • 49
  • But I never want to commit these changes. Then I would have to stash the desired changes every time – ProgrammingDude Mar 26 '20 at 11:31
  • @Dominik Unfortunately: yes. Or you have to find a way to conditionally add these changes based on a compiler define (#if). This will move the variation to a possible file that is less frequently modified (the make file, project file, settings) or an environment variable. You will always have to stach when you pull the repository and unstach-merge to addin your changes – rioV8 Mar 26 '20 at 12:00
  • You can discard the stash content with `git stash drop`, no need to `git stash pop`. – samthegolden Mar 26 '20 at 12:47
  • As I've already answered BKSpurgeon: _I have a local environment in which the access data of my database is different than in the live environment._ So i can't drop the changes – ProgrammingDude Mar 26 '20 at 12:52
  • 1
    @Dominik: what is your environment/language, like my comment: most environments have some sort of conditional/configurable options, show a few lines of code (edit your question) – rioV8 Mar 26 '20 at 13:45
  • It is a website and I use php, js etc. I have already considered to check whether the top-level domain is "Localhost" and to choose the access data depending on it, but my employee, for example, uses a different port for the Localhost – ProgrammingDude Mar 26 '20 at 13:51
  • @Dominik: then use an environment variable to pass on the port number – rioV8 Mar 26 '20 at 15:19
  • But like I said my employee does have an other port number than me. I have to find a way to set the access data depending on the port number of each one – ProgrammingDude Mar 27 '20 at 09:00
  • @D-Dogg - then read an environment variable in php and use it to construct your database access – rioV8 Mar 27 '20 at 10:34
  • And how do I do that? – ProgrammingDude Mar 27 '20 at 10:44
  • @D-Dogg Google for: PHP environment variable. As far as I know `ENV` is a dict with all the environment content – rioV8 Mar 27 '20 at 10:46
0

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.

samthegolden
  • 1,366
  • 1
  • 10
  • 26
0

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.

Max Yankov
  • 12,551
  • 12
  • 67
  • 135