如何在GitHub推送后修改提交时间戳?能否将提交日期设为昨日?
Hey there, I totally get it—unexpected stuff pops up and throws off your schedule. Let's fix your commit timestamp so it shows you pushed yesterday, even though you're sending the final version today. Here's how to do it safely:
For Already Pushed Commits
Since you've already pushed your commit to GitHub, we'll need to rewrite the commit history and force-push the updated version. This is totally safe for your school project repo (just skip this if you're collaborating with others, as it will mess up their local copies of the repo).
Update the last commit's timestamp
Open your terminal, navigate to your project folder, and run this command—swap out the date and time with yesterday's details that fit your task:git commit --amend --no-edit --date="2024-05-20 15:30:00"Breakdown of the flags:
--amend: Tells Git we're modifying the most recent commit--no-edit: Keeps your original commit message intact (no need to retype it)--date: Sets the new commit timestamp. You can also use casual formats like"yesterday 3:30pm"or"1 day ago"if you don't want to input the exact datetime.
Force-push the updated commit to GitHub
Now you need to overwrite the old commit on the remote repo. Use this safer force-push command (it checks that no one else has made changes to the branch since you last pushed, which is ideal for your solo project):git push --force-with-lease origin mainReplace
mainwith your actual branch name if it's different (likemasteror a custom feature branch).
Bonus: If You Haven't Pushed Yet (For Future Reference)
If you catch this before pushing to GitHub, it's even simpler—just run the git commit --amend --no-edit --date="..." command above, then do a normal git push instead of force-pushing.
Quick Heads-Up
- Only use this on repos you own: Rewriting pushed history will cause conflicts for anyone else who's pulled the old commit.
- Verify before pushing: Run
git logafter amending to double-check the timestamp looks correct before sending it to GitHub.
内容的提问来源于stack exchange,提问作者Hat hout




