A little guide to using git as a ninja.
The big problem to use git commit -m
is the idea to write a good commit message. Some IDEs than VSCode offer a textarea to write a draft message while writing your code but if you change the branch? or change takes time in another project?
My solution to this dispersion is to write the commit message by branch. Is a good practice to write your changes in a branch distinct of development|develop
and main|master
and is a best good practice to write the commit with "Commits convention".
I use some git directory to write my commits. For example, if work inside of feature/my-feature
branch writes my message on .git/commits-draf/feature/my-feature/COMMIT_EDITMSG
. This allows the IDE indentify the highlight "Git Commit Message" and manteint the order in my workflow.
Well, if I'm already satisfied with the changes I can make the commit with the next command git commit -F .git/commits-draf/feature/my-feature/COMMIT_EDITMSG
and done ๐
Obviously, not finished here. The bellow code is a command to open the editor with the message on draft.
#!/bin/sh
EDITOR=$(git config core.editor)
CURRENT_BRANCH=$(git branch --show-current --no-color)
COMMIT_EDITMSG_FILENAME=".git/$CURRENT_BRANCH/COMMIT_EDITMSG"
DIRNAME=$(dirname $COMMIT_EDITMSG_FILENAME)
mkdir -p $DIRNAME
touch $COMMIT_EDITMSG_FILENAME
$EDITOR $COMMIT_EDITMSG_FILENAME
Soo long? This is the version for git alias.
git config alias.edit-draft-commit '!EDITOR=$(git config core.editor);CURRENT_BRANCH=$(git branch --show-current --no-color);COMMIT_EDITMSG_FILENAME=".git/$CURRENT_BRANCH/COMMIT_EDITMSG";DIRNAME=$(dirname $COMMIT_EDITMSG_FILENAME);mkdir -p $DIRNAME;touch $COMMIT_EDITMSG_FILENAME;$EDITOR $COMMIT_EDITMSG_FILENAME'
๐ Now only run git edit-draft-commit
to edit your code.
This is the command to commit the draft commit.
#!/bin/sh
CURRENT_BRANCH=$(git branch --show-current --no-color)
COMMIT_EDITMSG_FILENAME=".git/$CURRENT_BRANCH/COMMIT_EDITMSG"
git commit -F $COMMIT_EDITMSG_FILENAME
And this is the version for git alias.
git config alias.commit-draft-commit '!CURRENT_BRANCH=$(git branch --show-current --no-color);COMMIT_EDITMSG_FILENAME=".git/$CURRENT_BRANCH/COMMIT_EDITMSG";git commit -F $COMMIT_EDITMSG_FILENAME'
๐๐ Now only run git commit-draft-commit
to commit your code.