call python code of different git branch other than the current repository without switching branch
<p>So, basically I have 2 versions of a project and for some users, I want to use the latest version while for others, I want to use older version. Both of them have same file names and multiple users will use it simultaneously. To accomplish this, I want to call function from different git branch without actually switching the branch.
Is there a way to do so?</p>

<p>for eg., when my current branch is <code>v1</code> and the other branch is <code>v2</code>; depending on the value of variable <code>flag</code>, call the function</p>

<pre><code>if flag == 1:
    # import function f1() from branch v2
    return f1()
else:
    # use current branch v1
</code></pre>

<p>You <em>must</em> have both versions of the code present / accessible in order to invoke both versions of the code dynamically.</p>

<p>The by-far-simplest way to accomplish this is to have both versions of the code present in different locations, as in <a href="http://stackoverflow.com/a/39290774/1256452">VonC's answer</a>.</p>

<p>Since Python is what it is, though, you <em>could</em> dynamically extract specific versions of specific source files, compile them on the fly (using dynamic imports and temporary files, or <code>exec</code> and internal strings), and hence run code that does not show up in casual perusal of the program source.  I <em>do not</em> encourage this approach: it is difficult (though not very difficult) and error-prone, tends towards security holes, and is overall a terrible way to work unless you're writing something like a Python debugger or IDE.  But if this is what you want to do, you simply decompose the problem into:</p>

<ul>
<li>examine and/or extract specific files from specific commits (<code>git show</code>, <code>git cat-file -p</code>, etc.), and</li>
<li>dynamically load or execute code from file in file system or from string in memory.</li>
</ul>

<p>The first is a Git programming exercise (and is pretty trivial, <code>git show 1234567:foo.py</code> or <code>git show branch:foo.py</code>: you can redirect the output to a file using either shell redirection or Python's <code>subprocess</code> module), and when done with files, the second is a Python programming exercise of moderate difficulty: see <a href="https://docs.python.org/3/library/modules.html" rel="nofollow">the documentation</a>, paying particularly close attention to <code>importlib</code>.</p>

python git gitpython