11 Expert Tips for Efficient Committing
Explore 11 essential tips for mastering code commits, including effective messaging, managing file counts, and using shortcuts to streamline your workflow. Here’s a guide to enhance your commit practices for a better development experience.
Local and Remote Commits
Understanding the difference between local and remote commits is crucial:
- Local Commits: Changes saved on your computer.
- Remote Commits: Changes pushed to the remote repository.
Keep your local and remote repositories in sync:
- Push Changes:
git push
to update the remote repository. - Pull Changes:
git pull
to update your local repository.
Conflicts may arise during merges if there are discrepancies in commit history. 🔄
How Many Files to Include in a Commit
- Small Commits: Prefer smaller, focused commits that are easier to manage and revert.
- Separate Commits: For changes to different parts of a project (e.g., header, footer), create separate commits to keep history clean and relevant.
Example of Few Files Split into More Commits
Smaller, well-defined commits make it easier to track and revert specific changes:
Git Commit Command Tips
Basic Git Commit
- Command:
git commit
- Opens a text editor for the commit message.
Commit with Message Inline
- Command:
git commit -m "Your message"
- Provides a one-line commit message without opening a text editor.
Git Add and Commit Simultaneously
- Command:
git commit -a -m "Your message"
- Adds all changes and commits in one step (excludes untracked files).
Commit Amendment
- Command:
git commit --amend
- Modify the last commit to include additional changes or fix mistakes.
Tips for Writing Good Commit Messages
- Use Action-Oriented Verbs:
- E.g.,
Improve
,Optimize
,Update
,Implement
- E.g.,
- Be Clear and Specific:
- E.g.,
Improve navigation bar responsiveness
,Optimize database query performance
- E.g.,
- Avoid Ambiguity:
- Specify what was changed and why.
- Format Consistently:
- Title (50 characters max) and description (72 characters per line).
- Provide Additional Context:
- Use comments or descriptions if needed.
What Not to Write in a Commit Message
- Avoid Vague Messages: E.g.,
Update feature
- Avoid Repetitive Messages: E.g., multiple commits with
create about page
- Avoid Single-Letter or Generic Messages: E.g.,
m
,test
,add
Using VS Code Shortcuts for Commits
- Configure Shortcuts:
- Go to File > Preferences > Keyboard Shortcuts (
CTRL + K + S
).
- Go to File > Preferences > Keyboard Shortcuts (
- Assign Shortcuts:
- Double-click on Git: Commit and set your preferred shortcut.
- Commit from VS Code:
- Use the shortcut to open the commit message box.
Editing Git Messages in VS Code
- Enable Editing: Go to File > Preferences > Settings (
CTRL + ,
), search for “Git: Commit Message”, and enable editing.
References
Callback
- Prefer Smaller Commits: Keep commits small and focused.
- Use
git commit --amend
: To add missing files to the last commit.
Ref: Adarsh Rai - Medium