Skip to content

Instantly share code, notes, and snippets.

@JonDotsoy
Last active November 30, 2022 03:58
Show Gist options
  • Save JonDotsoy/6dd1f826f12f27428970c18fc1db04ed to your computer and use it in GitHub Desktop.
Save JonDotsoy/6dd1f826f12f27428970c18fc1db04ed to your computer and use it in GitHub Desktop.
🦺 A few ideas to optimize your workflow

Git Ninja 🥷

A little guide to using git as a ninja.

🙏 No more git commit -m

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment