Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set commit message for future git commit

Tags:

git

Is there a way to set a comment for the next future commit using git? Imagine something like this:

git next-commit "Implement client-side validation"

# implement the feature ...

# commit changes
# equivalent to git commit -m "Implement client-side validation"
git commit -m from-next-commit

The motivation behind this is that I often have a particular feature in mind when I'm programming, but along the way I end up developing some other features or fixing related things and I lose track of the main task I was working on.

At that point I have modified the source code with useful changes, but I don't even remember what the main feature I added is, since it's all just a bunch of changes and the only commit message I can think of is git commit -m "Update stuff". Setting the message for the next commit could also help me keep working on what I was supposed to do. At any point if I feel like I've forgotten the main task and have derailed to other feature, I would ideally ask git with something like git next-commit, which could print Implement client-side validation.

Does a feature like this exist?

EDIT: after seeing some answers, I think I should clarify another thing. Ideally, this command would also help you keep track of when the future commit has already been used. For example, if you use git commit -m from next-commit twice without setting a new future commit message before, it should fail.

$ git next-commit "Implement client-side validation"
ok
$ git commit -m from-next-commit
ok
# git commit -m from-next-commit
error : already used
$ git next-commit "Optimize get_request"
ok
$ git commit -m from-next-commit
ok
like image 321
Mei Zhang Avatar asked Feb 18 '19 06:02

Mei Zhang


People also ask

How do you specify a commit message to a commit?

The easiest way to create a Git commit with a message is to execute “git commit” with the “-m” option followed by your commit message. When using the Git CLI, note that you should restrict your commit message in order for it not to be wrapped.

Can I change commit message?

You can change the most recent commit message using the git commit --amend command. In Git, the text of the commit message is part of the commit. Changing the commit message will change the commit ID--i.e., the SHA1 checksum that names the commit.

Can I change commit message before push?

Conclusion. To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N . Don't amend pushed commits as it may potentially cause a lot of problems to your colleagues.


1 Answers

Personally I would do what Tim Biegeleisen suggested, but it wouldn't be terribly hard to do what you want if you're willing to use a custom command instead of git commit.

That is, you could make a custom git-next-commit script, put it in PATH, and have it write your desired commit message to some file in the root of your git repo (or maybe in .git/).

Then make another custom script (e.g. git-commit-from-next, or just reuse git-next-commit, possibly with a command-line option) that invokes git commit with the previously stored message and deletes the file. If the file doesn't exist, then print an appropriate error message.

You also might be able to get the git prepare-commit-msg hook to do what you want if it sees a special "from-next-commit" message.

like image 103
jamesdlin Avatar answered Sep 29 '22 02:09

jamesdlin