Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
936 views
in Technique[技术] by (71.8m points)

git - How to push a file to past time?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...