0

I have a a local, unpushed branch of a git repo. I am blocked from pushing my branch to origin cause I forgot to add in a ticket number in one of my git commit messages, so I can push to our master branch due to a restraint from our ticket integration.

The commit missing the ticket number is 3 commits pass my local branch head.

I've tried purposefully detaching at the commit to use --amend but it did not work.

I've also tried to cherry-pick and --amend but that also did not work.

I've tried to do:

git reset [missing ticket # commit hash] 
git commit --amend

and tried:

git cherry-pick [missing ticket # commit hash]
git commit --amend

Neither work.

Kyle Calica-St
  • 2,629
  • 4
  • 26
  • 56
  • 2
    Try an [interactive rebase](https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt--i). – jonrsharpe Mar 11 '19 at 20:30
  • eg `git rebase -i HEAD~10` (pick a number > 3). Change the first column of the appropriate commit to `r` – William Pursell Mar 11 '19 at 20:33
  • Possible duplicate of [How to modify existing, unpushed commits?](https://stackoverflow.com/questions/179123/how-to-modify-existing-unpushed-commits) – phd Mar 11 '19 at 21:12
  • https://stackoverflow.com/search?q=%5Bgit%5D+change+commit+message – phd Mar 11 '19 at 21:13

3 Answers3

3

You have do a rebase with interactive option. I.e:

git rebase --interactive HEAD~2

Then for the commit you want to change the message, choose the reword option.

Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58
2

Just read a blog online to help. Was pretty dense and seemed outdated so I was very nervous of it.

But it worked.

git rebase --interactive [hash of parent commit]^ 

the carrot means to use as a parent and for every commit after it

then you'll be in some editor.

Change the word pick to one of the commands, in my case it was edit

Very important to do the instructions above. I was confused on how to edit. It's a simple text change.

Then you can use

git commit --amend

git rebase --continue

and now you should have the proper commit message.

Helpful link

TLDR:

git rebase --interactive [hash of parent commit]^ 

Edit pick on commit you want to change to edit

git commit --amend

git rebase --continue
Kyle Calica-St
  • 2,629
  • 4
  • 26
  • 56
  • 1
    Note that "reword", if available, is much simpler. (It's not in some truly ancient Git versions, where you have to use the longer method that you used.) – torek Mar 11 '19 at 20:51
  • so `git reword [commit hash]` ? I have `2.17` and it was not there, unless that's not the proper way to use `reword` – Kyle Calica-St Mar 11 '19 at 21:18
  • 1
    It's an option during `git rebase -i`: change `pick` to `reword` instead of `edit`, and your next step is to fix the message—there's no faffing about with `git commit --amend` and `git rebase --continue`. – torek Mar 11 '19 at 21:43
1

With --amend flag you can change last commit message, e.g. git commit --amend -m "new commit message". If you need to edit multiple commit massages use git rebase -i.

Jovo Skorupan
  • 237
  • 2
  • 5