2

Yesterday, I made changes on a project file but forgot to commit and push it on github. I don't want that my contribution streak breaks after 51 days..so I would like to push that commit to yesterday so that my streak keeps on... Is that possible?

Thank you very much in advance!

2 Answers2

4

I have that script in my path, called git-rcd, which changes GIT_COMMITTER_DATE and GIT_AUTHOR_DATE of any commit you want (not just the last one)

#!/bin/bash
# commit
# date YYYY-mm-dd HH:MM:SS

commit="$1" datecal="$2"
temp_branch="temp-rebasing-branch"
current_branch="$(git rev-parse --abbrev-ref HEAD)"

date_timestamp=$(date -d "$datecal" +%s)
date_r=$(date -R -d "$datecal")
echo "datecal=$datecal => date_timestamp=$date_timestamp date_r=$date_r"

if [[ -z "$commit" ]]; then
    exit 0
fi

git checkout -b "$temp_branch" "$commit"
GIT_COMMITTER_DATE="$date_timestamp" GIT_AUTHOR_DATE="$date_timestamp" git commit --amend --no-edit --date "$date_r"
git checkout "$current_branch"
git rebase --autostash --committer-date-is-author-date "$commit" --onto "$temp_branch"
git branch -d "$temp_branch"

What that allows me is take the last commit I just did and type:

git rcd @ '1 day ago'

And presto! My last commit has now been done yesterday.

It changes any commit you want:

git rcd @~2 '1 day ago'

That would only change the HEAD~2 (and not the HEAD~ or HEAD)

The script works even on Windows.

Once the change is done, push (or git push --force if you pushed before with the wrong date). And your streak is preserved.


Note (2020), David Fullerton mentions in the comments:

Getting this working on Mac OS X required:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • How can I add this script to my path and utilize this? Do you have any documentation/readme on this? – chopper draw lion4 Dec 17 '14 at 08:01
  • 1
    @chopperdrawlion4 create a file named `git-rcd` (no extension) and put it anywhere in your `PATH` (meaning in one of the folders listed by the environment variable `PATH`). Then the command `rcd` will be known by `git`: `git rcd` will work. – VonC Dec 17 '14 at 08:13
  • I receive an error when I try this: sudo git rcd @ '1 day ago' fatal: cannot exec 'git-rcd': Permission denied – chopper draw lion4 Dec 17 '14 at 08:22
  • @chopperdrawlion4 no sudo needed. And make sure the script is executable: `chmod 755`. And that you are using a recen version of git (not some old 1.7.10) – VonC Dec 17 '14 at 08:25
  • I got my permissions working so I can run this now. When I run it git throws an error for an invalid date format though. date: illegal option -- R .... fatal: invalid date format: – chopper draw lion4 Dec 19 '14 at 00:34
  • @chopperdrawlion4 try with double-quotes: `git rcd @ "1 day ago"` – VonC Dec 19 '14 at 06:29
  • Getting this working on Mac OS X required [installing coreutils to get GNU date](https://stackoverflow.com/questions/9804966/date-command-does-not-follow-linux-specifications-mac-os-x-lion) and then updating the script to use `gdate` – David Fullerton Apr 28 '20 at 19:09
  • @DavidFullerton Thank you for this feedback (and your service to Stack Overflow!). I have included your comment in the answer for more visibility. – VonC Apr 28 '20 at 19:49
2

Yesterday, I made changes on a project file but forgot to commit and push it on github

As far as I know, GitHub contribution graphs rely on commit datetimes, not push datetimes. FWIW, there are even tools abusing this to use the contribution graph as a drawing board (cf. this google search).

So the easy way would be to

  • Commit locally now

  • Then rewrite your latest commit to change the authorship date (pick the time and timezone you'd like) with something like git commit --amend --date="Wed Jul 12 14:17 2014 +0900"

  • Push

nulltoken
  • 64,429
  • 20
  • 138
  • 130