Sorry if this post is a bit abrupt / lacks details, it was really written as a quick memo for myself. You will find plenty more details in the sources listed at the bottom of the post (although most of them are simply what shows up first in DuckDuckGo when you search for how to edit old commits).
For this example, we will edit the last 10 commits:
git rebase --interactive HEAD~10
NB: if you’re using SourceTree, you can open a console to Git via the Menu: Actions => Open in Terminal
This will open a text editor with a listing of commits to process, like:
edit e1186e8 Oldest commit edit 830e58c ANother commit edit 9d41af7 Latest commit description
By default on Windows, for me the editor was that HORRIBLE shit called Vim. To start editing the text, press “Insert” key. Change the pick
keyword to edit
for the commits you want to modify. When you’re done, to save and quit press “Escape” (this leaves text editing mode), then type “:wq” and hit “Enter”.
Rebase will then process, telling you “You can amend the commit now”. To change the date without opening an editor (the editor would allow you to edit the commit text I think), use:
git commit --amend --date="20160101T12:12:12" --no-edit
(NB: for supported date formats, see here)
If you’re happy with your change, you can go on to the next commit with
git rebase --continue
Once you’ve run this command on the last commit, your branch will be updated (“Successfully rebased and updated refs/heads/master.”). If you screw up, git rebase --abort
should revert all your changes (which seem to only get applied once you’re done with the last commit).
Last but not least, there seems to be a better way to edit the date, using something like this:
GIT_COMMITTER_DATE="20160101T12:12:12" git commit --amend
But I haven’t tried this yet.
A bunch of sources that helped (even though they all had big flaws that still made this a headache):
https://stackoverflow.com/questions/3042437/change-commit-author-at-one-specific-commit
https://stackoverflow.com/questions/9110310/update-git-commit-author-date-when-amending
https://gist.github.com/maciej/5875828
https://help.github.com/articles/about-git-rebase/
https://stackoverflow.com/questions/454734/how-can-one-change-the-timestamp-of-an-old-commit-in-git
PS: a little bonus: to “uncommit” the last commit (this cancels the commit but leaves the commit + the content of the commit is still selected and ready to be committed again): reset --soft HEAD^
PS2: if you try to run rebase while still having uncommitted changes, you’ll get an error saying “Cannot pull with rebase: You have unstaged changes. Please commit or stash them.” As the message says, you can commit your changes to bypass this, but if you don’t want to commit, you can run “git stash”, then do the rebase procedure, then at the end run “git stash pop”. Cf this stackoverflow post.
If the repo’s SSH key isn’t in your keyring and SourceTree isn’t able to automatically add it, run plink like this and say “yes” (or maybe just “y”)
plink -T git@bitbucket.org
(of course replace bitbucket with whatever your git provider is)