Suppose I have a log of commit messages.Is there a way I can amend first commit message?Or this is why we make first commit as initial commit since it cannot be done.
1 Answer
You cannot actually change any commit. What "amending" does (or "reword" in an interactive rebase) is make a new, different copy of the commit. In this copy, you change exactly what needs to be changed, and all the rest is the same.
The problem here is that all subsequent commits point back, eventually, to your original first commit, not the copy. For instance, suppose there are exactly three commits. Let's call them A, B, and C. The branch label, master, points to commit C. But commit C points to B, and B points to A. Since A is the initial commit, the chain ends here (that's how you know a commit is an initial commit: it points to no one.)
A <- B <- C <-- master
You now propose to copy commit A to some new commit, let's call it A':
A <- B <- C <-- master
A'
To make this useful, you will now have to copy B and C as well, making one change in each one: the copy of B, which we'll call B', must point to A', and the copy of C must point to B':
A <- B <- C <-- master
A' <- B' <- C'
Once you've copied every commit in this fashion, you can then change the label master to point to the last copy (here C'):
A <- B <- C
A' <- B' <- C' <-- master
It is now safe to abandon the original A-B-C chain of commits, as you've copied everything you care about, while making the changes needed in order to make the new A'-B'-C' chain look the way you want it to.
The way to do this all the way back to the root commit is with git rebase -i --root. But as with all rebasing, or any other way of "editing history", this will cause a big headache for anyone who obtained an earlier published copy of your work and believes you still have commits A-B-C rather than your new A'-B'-C' sequence. So if your repository is cloned by, or pushed to, other people, don't do this—or at least don't do it without discussing it with them first.