Git

How do I Make a Git Commit in the Past?

In Git, commits are utilized to save changes or modifications in the Git repository. Each commit represents a snapshot of the modifications made to the repository at a certain point in time. Whenever a user makes changes to their projects, they save those changes by committing them. However, sometimes, users may want to save the modifications in the past. In this situation, Git allows them to make commits in the past.

This article will illustrate the procedure to make a commit in the past in Git.

How to Make a Git Commit in the Past?

To make a Git commit in the past, the β€œgit commit –date=”YYYY-MM-DD HH:MM:SS” -am “<commit-message>”” command is used. Let us take a scenario to perform this operation:

Step 1: Redirect to the Local Repository

First, navigate to the particular local directory:

cd "C:\Git\Repo1"

 
Step 2: Create a New File

Next, run the following command with the particular file name to create it. For instance, we are creating a β€œdemo1.txt” text file:

touch demo1.txt

 

This command has created a new file named β€œdemo1.txt”.

Step 3: Track the File

Then, stage the newly created file for tracking purposes:

git add demo1.txt

 

The β€œdemo1.txt” file has been added to the Git staging area.

Step 4: Commit Changes in Past

Now, commit the file changes in the past using the β€œgit commit” command with the β€œ–date” format and specify the old date and time, and commit message:

git commit --date="2023-02-28 12:00:00" -am "demo1 file added"

 

This command has made the commit on β€œ2023-02-28” at β€œ12:00:00”.

Step 5: Verify Changes

Lastly, view the commit history to verify whether the commit has been made in the past or not:

git log

 

In the above output, the desired commit has been made in the past on the specified date and time.

Conclusion

To make a Git commit in the past, first, make desired changes and track them. Then, commit changes using the β€œgit commit –date=”YYYY-MM-DD HH:MM:SS” -am “<commit-message>”” command. To verify whether the commit has been made in the past or not, use the β€œgit log” command. This article illustrated the method to make a commit in the past in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.