How do I make a Git commit in the past?

How do I make a Git commit in the past?

ยท

2 min read

Hey! ๐Ÿ‘‹ folks, today I want to share an interesting thing about GIT. So the fact is that I daily commit some code on my GitHub account & make a commit streak and one day I forgot to commit code on GitHub. I do not want to lose my GitHub commit streak, so I found a solution for that how do I make Git commit in past date?

How to create Git commit in past date?

So if you want to commit something on Git in the past date, you simply use these commands that help you to do so.

git commit --amend --no-edit --date="Sat Jun 5 20:00:00 2021 -0600"

  1. So to make a commit in the past date, you just want to add your changes in the local repo by running git add ..
  2. Commit changes using git commit -m "Your commit message".
  3. Run that command after a commit to amend the last commit with the timestamp noted. The --no-edit will leave the message as-is.
  4. Push changes to GitHub or whichever platform you use by running git push.

Make sure you have to change the date and time according to your preference. This will create a commit to the particular date in the past and you do not lose your GitHub streak.

Create empty Git commit in past date?

Another command is useful for creating an empty commit with a provided date.

git commit --allow-empty --date="Sat Jun 5 14:00 2021 +0100" -m '5 jun commit'

To create an empty commit just go to your repository, add changes and create an empty commit message by running this command and push changes. It reflects in GitHub at specified date.

If you like this post let me know in the comments below or follow me on Twitter