1

I'm new here so I apologize if this isn't the place to ask this question. I'm writing a python script for my company that looks through files in certain commits and compares them. Well I'm not familiar with git and how commits work so maybe someone more knowledgeable than me can help. What I have so far is something along the lines of this:

import git   

# Directory for my repo
repo = git.Repo(<repo path>)

# Get the current commit and save it for later use
commit = repo.commit()

< Here is where I search through the current files to get my info >

# Checkout the old commit
repo.git.checkout("HEAD~1", force=True)

< Here is where I search through the old files to get my info >

# Re-checkout the current commit
repo.git.checkout(commit.hexsha, force=True)

< Now I want to be back where I started >

This works well and it almost accomplishes what I want it to. However... the whole reason for this script is to check changes to files. In other words, the developer will work on and change many of the files so that they are different from the last commit. The problem is when this checks out the newer commit again, the changes are gone (obviously very frustrating to the developer). So the process overall is something is like this:

--> new_commit on local machine (with changes from developer)
--> old_commit checked out to see what changed
--> new_commit checked back out (as if the developer never worked on it)

Overall, my question is: is there any way that I can save this new commit with the changes so that when re-checked out it still has the changes? Thank you for any help!

Edit: What I want to achieve is storing the uncommitted/unstaged changes to the version currently checked out, then checkout and older version, and finally bring back the uncommitted/unstaged changes.

  • May be this can help. https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit – Sachidanand Dev Jul 14 '20 at 16:17
  • If this has to be done directly in git-python (or some other Python wrapper for Git), you get a different answer than if this should be done with shell scripts using the Git CLI, where use of `git worktree` might be the way to do it. In the Python case you may need to specify the exact Python wrapper, as different ones have different feature sets. – torek Jul 14 '20 at 18:06
  • @torek it doesn't have to be done with git-python, i could do it with the git CLI. I'll look into 'git worktree', do you have any experience with 'git stash'? –  Jul 14 '20 at 20:07
  • Could you describe what you want to achieve with your script ? There are so many ways to check a diff or a previous content without running `git checkout thatotherversion` – LeGEC Jul 14 '20 at 21:38
  • @LeGEC I edited the post to describe how I want the script to go –  Jul 14 '20 at 21:51
  • That's `git stash; ... ; git stash pop` – LeGEC Jul 14 '20 at 21:55

1 Answers1

0

For anyone looking for the answer to this question, what worked was LeGEC's solution in the comments. I used:

import subprocess

# Here is where I got info on the current commit with changes

subprocess.run(["git", "stash"])

# Here is where I got info on the older commit version

subprocess.run(["git", "stash", "pop"])

# Back to the commit with changes
  • 1
    You have to be a bit careful with `git stash push` (or `git stash save`—these are essentially the same, except for options, and are the default action of `git stash` without a third argument). In particular, sometimes it says that there is nothing to save/push and does nothing. In that case, you want to *not* run the `git stash pop` step, as that would pop some *other* stash, if there are any others. – torek Jul 15 '20 at 17:38
  • @torek Thank you for the heads up! –  Jul 15 '20 at 22:53