0

I would like to leave out a few lines of my python script from each commit that I do, for example this line:

DEBUG_OFFSET = 600

as I change this value every time, depending on from which frame I want to start a video, and I change this value every time, so I don't changes on values like this to be tracked, only on the rest of the code.

Is such a thing possible?

I know that is possible to use something like git add --patch like on this topic:

Commit only part of a file in Git

but this is very time consuming to do every time that I make a new commit, so I was thinking of something done automatically, more like adding just a portion of a file to the .gitignore, or something like this.

Any recommendation please :) ?

Many thanks for your help!

  • 2
    Have you considered loading this value from another file? – Wooble Aug 24 '21 at 13:04
  • 1
    The best way may be to load that value from another file. Either a config file or a Python module that just contains configuration. Then you can just exclude that file. – Kemp Aug 24 '21 at 13:06
  • 1
    https://stackoverflow.com/a/22171275/7976758, https://stackoverflow.com/a/1976900/7976758 – phd Aug 24 '21 at 14:50
  • 1
    To be clear, the answer to your title question is no, you can't ignore part of a file with .gitignore. As suggested in comments and the current answer you can split out the lines in question into a new file and ignore that. – TTT Aug 24 '21 at 18:44
  • Excellent! the solution was indeed to save all the config values to a file, like a json or yaml and change this config files, but keep the code the same. Many thanks for your answers! – garrofederico Sep 22 '22 at 16:01

1 Answers1

1

You can add the value in another file in different formats. One way is to create a .env file, add .env to .gitignore, and change the value as needed.

pip install python-dotenv

create a file called .env

put the value inside .env:

DEBUG_OFFSET = 600

Add it to gitignore Load the required value in python file as follows:

import os
from dotenv import load_dotenv

# Load .env file
load_dotenv(dotenv_path=".env")

# Assign it to variable
DEBUG_OFFSET = os.environ["SECRET_KEY"]
if DEBUG_OFFSET:
    DEBUG_OFFSET  = int(DEBUG_OFFSET)

Then, you can change the value of DEBUG_OFFSET inside .env as needed