3

The code is pretty straightforward, it just opens windows command prompt and executes the calling() function. It has basic git commands which help me push to a git repo. I have configured ssh and remote repo.

Link:https://github.com/vivekpatani/git-script-gitter

I can change the date, but when I push it to git, it displays the current date on which I pushed rather than the one I committed.

The Commit List where it shows committed 9 days ago and 11 days ago, I want it to actually show the same date as committed.

def calling():

    #Simply opening command prompt in Windows
    subprocess.call("git --version")
    subprocess.call("git status")
    subprocess.call("git add .")
    subprocess.call("git commit -am \"Changing Things\" --date=\"Sat, 26 Mar 2016 18:46:44 -0800\"")
    subprocess.call("git push origin master")

    #To stop from cmd closing automatically
    temp = input("Enter to close:")

def main():
     calling()

if __name__ == "__main__":
    main()

After looking around I read that I need to change the AUTHOR DATE and COMMIT DATE together? Can somebody please help me out.

EDIT 1: I'm working on Windows OS.

It works when I run it through Git Bash, somehow just need to convert that to Python.

git --version
git status
git add .
GIT_AUTHOR_DATE='Fri Mar 25 19:32:10 2016 -0800' GIT_COMMITTER_DATE='Fri Mar 25 19:32:10 2016 -0800' git commit -am "Hello Laney"
git push origin master

EDIT 2: Solution

def calling(git_date):
    subprocess.call("git --version")
    subprocess.call("git status")
    subprocess.call("git add .")

    #The next statement is important as updates/adds new GitCommiterDate in environment making it the current commit date.
    os.environ["GIT_COMMITTER_DATE"] = 'Fri Mar 25 19:32:10 2016 -0800'

    #The date in commit command only changes author date.
    subprocess.call("git commit -am \"Changing Things\" --date=\"Fri Mar 25 19:32:10 2016 -0800\"")
    subprocess.call("git push origin master")
Viv
  • 1,706
  • 1
  • 18
  • 27

1 Answers1

2

--date only modifies the author date.

You need to set GIT_COMMITTER_DATE environment variable in order to have the same date as author date (using the env option of Popen(), and merging it with the current environment).

subprocess.call("git commit -am \"Changing Things\" --date=\"Sat, 26 Mar 2016 18:46:44 -0800\"", env=dict(os.environ, "GIT_COMMITTER_DATE":"Sat, 26 Mar 2016 18:46:44 -0800"))
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks, but when I print os.environ, I cannot find the GIT_COMMITTER_DATE. Also the line contains a syntax issue. Please could you guide me? – Viv Mar 28 '16 at 16:12
  • 1
    @VivekPatani the goal is to *add* that variable to the environment variables, not to find it. – VonC Mar 28 '16 at 16:13
  • @VivekPatani Also, a double-quote was missing. – VonC Mar 28 '16 at 16:13
  • I just made a few changes to what you said. The answer you mentioned is correct, but since it was still giving issues, I just split the commands into two parts. os.environ["GIT_COMMITTER_DATE"] = "Fri, 25 Mar 2016 18:46:44 -0800" and then subprocess.call("git commit -am \"Changing Things\" --date=\"Fri, 25 Mar 2016 18:46:44 -0800\"") – Viv Mar 28 '16 at 16:32
  • 1
    @VivekPatani Great! Don't hesitate to edit this answer. – VonC Mar 28 '16 at 16:33