-1

In this particular situation, I am using 1 commit = 1 file.

I know that using the --amend option to a commit will update the LAST commit, but how can I update the last commit message for a specific file ?

This of course does not work:

git commit -o somefile.js -m "new message" --amend

I need to be able to do this non-interactively so that it can be called in a single line command.

Is there a solution ?

crankshaft
  • 2,361
  • 4
  • 32
  • 69

2 Answers2

2

git rebase -i <commit hash before the one you want to change>

You will be presented with a list of commits. Find the commit you want to change. After that, change the first letter or word on that line (will probably be p or pick if I remember correctly) to r (for reword).

Save and exit how you would when doing a git commit and you will be presented with your default text editor and your commit message to be changed.

Make the changes you want before saving and exiting from that. You will have to do a git push -f after this since you changed the commit message via interactive rebase.

LeGEC
  • 29,595
  • 2
  • 37
  • 78
yes-siz
  • 158
  • 8
  • Thanks for the suggestion, but I need to be able to do this non-interactively – crankshaft Jun 20 '20 at 09:04
  • No matter what you’re doing, you’ll be [modifying the commit](https://www.atlassian.com/git/tutorials/rewriting-history). What I suggested has the same results as other suggestions not using an interactive rebase. The only situation I could think of where interactive rebasing would be off the table is if your system didn’t have a text editor. – yes-siz Jun 20 '20 at 12:18
0

I think it is not a good practice for committing for every file because every commit in git most of the time should define specific change or provide some functionality or bugfix, in your repository but if you want to change specific message commit in your repository you could do this:

$git checkout <your hash commit>
$git commit --amend -m "New message"
$git push --force repository-name branch-name

A safer alternative is to use:

$git push --force-with-lease repository-name branch-name

Unlike --force, which will destroy any changes someone else has pushed to the branch, --force-with-lease will abort if there was an upstream change to the repository. If the message needs to be amended for an older commit, then the interactive rebase tool can be used: