-1

I'm working on a Python/Git project for a Udacity program, and I've been told that my commit messages need to be fixed to obey the Udacity format (was indicated to be "encouraged" and not "required", but okay). However, I'm having trouble with replacing the existing commit messages using the git rebase command.

I tried using the suggestion from gitHub docs, using git rebase -i HEAD~13 (to modify all my commits), and then in the initial rebase prompt, I change each of the commit lines from pick to reword and then go through the process of placing in the new commit messages. However, at the end, what happens is I have 13 new commits added on to my branch, each of which have the new commit messages. The existing commits remain the same.

I then tried using this solution from StackOverflow which involves using git rebase and setting each commit to edit instead, and using git commit --amend to modify the last commit and then using git rebase --continue to advance to the next, slowly modifying each commit as being "most recent", one by one with --amend. In this case, it also added a series of new commits to the branch, without actually editing the old ones.

Could this maybe be because I've already pushed my branches to origin on gitHub? Or because my branches are merged already? Apologies if this is a stupid question, but I couldn't find any results on Google for this particular problem besides more instructions to do what I've already done.
My repository: https://github.com/WJTownsend/pdsnd_github

WJTownsend
  • 123
  • 5
  • Are you askign how to not have the old commits ? That would involve rewriting the git history. – leoOrion Jul 23 '21 at 03:55
  • My initial thought is you simply haven't forced push your branch out yet after the rebase. But, what do you mean by "Or because my branches are merged already?" – TTT Jul 23 '21 at 05:12
  • It is literally impossible to *change* any part of any commit. The commit's hash ID is a cryptographic checksum of all the data stored inside that commit, so the hash ID *is* the commit, and vice versa. That's why everything Git makes lasts forever (or until people stop using it), and that's why "changing a commit" really means "making a new commit and using that instead of the old one, which still exists". – torek Jul 23 '21 at 05:18
  • I had apparently misunderstood exactly what you could do with `rebase` or with `git commit --amend`. I thought it was possible to amend the messages, especially with the verbiage in `reword` within rebase. Maybe a bad practice, sure, but possible. Like I said up top, I'm learning, and no where did it actually say "that's not possible at all" that I found. Not sure why that got downvoted, but I appreciate the answers here. – WJTownsend Jul 24 '21 at 07:38

1 Answers1

0

However, at the end, what happens is I have 13 new commits added on to my branch, each of which have the new commit messages.

When you rebase and change anything in a given commit, Git writes a new commit. Changing the commit message falls into this category, so when you rebase a chain of 13 commits, you are actually instructing Git to make 13 new commits, with different commit messages.

The existing commits remain the same.

The functionality of the commits may remain the same after the rebase, but the commits themselves are in fact new. To verify this, run git log on your branch before and after the rebase. You will notice that the SHA-1 hash of every commit is now different.

Keep in mind that in general rewriting the history of a published branch which also might be shared by others besides yourself is not a desirable thing. You should only attempt this if you are the only one working with this branch.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I did notice that the new commits also have new SHA-1 hashes. And I understand that going back and changing commits on a published branch isn't a good practice, but that's basically what I'm looking at having to do meet the requirements and complete my project because I didn't take "encouraged" to mean "required". FWIW, no one is using this repository but myself. Is there a way to do this, or am I starting over? – WJTownsend Jul 23 '21 at 03:27
  • You already have done this. There is _no_ way to do this without rewriting the commits, unfortunately. – Tim Biegeleisen Jul 23 '21 at 03:30