good practices #5
Pinned
Abhinav-Kumar012
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Git Commit Best Practices
Basic Rules
Commit Related Changes
A commit should be a wrapper for related changes. For example, fixing two different bugs should produce two separate commits. Small commits make it easier for other developers to understand the changes and roll them back if something went wrong.
With tools like the staging area and the ability to stage only parts of a file, Git makes it easy to create very granular commits.
Commit Often
Committing often keeps your commits small and, again, helps you commit only related changes. Moreover, it allows you to share your code more frequently with others. That way it‘s easier for everyone to integrate changes regularly and avoid having merge conflicts. Having large commits and sharing them infrequently, in contrast, makes it hard to solve conflicts.
Don't Commit Half-Done Work
You should only commit code when a logical component is completed.
Split a feature‘s implementation into logical chunks that can be completed quickly so that you can commit often. If you‘re tempted to commit just because you need a clean working copy (to check out a branch, pull in changes, etc.) consider using Git‘s «Stash» feature instead.
Test Your Code Before You Commit
Resist the temptation to commit something that you «think» is completed. Test it thoroughly to make sure it really is completed and has no side effects (as far as one can tell). While committing half-baked things in your local repository only requires you to forgive yourself, having your code tested is even more important when it comes to pushing/sharing your code with others.
Write Good Commit Messages
Begin your message with a short summary of your changes (up to 50 characters as a guideline). Separate it from
the following body by including a blank line. The body of your message should provide detailed answers to the following questions:
– What was the motivation for the change? – How does it differ from the previous
implementation?
Use the imperative, present tense («change», not «changed» or «changes») to be consistent with generated messages from commands like git merge.
Having your files backed up on a remote server is a nice side effect of having a version control system. But you should not use your VCS like it was a backup system. When doing version control, you should pay attention to committing semantically (see «related changes») - you shouldn‘t just cram in files.
Use Branches
Branching is one of Git‘s most powerful features - and this is not by accident: quick and easy branching was a central requirement from day one. Branches are the perfect tool to help you avoid mixing up different lines of development. You should use branches extensively in your development workflows: for new features, bug fixes, ideas...
Agree on A Workflow
Git lets you pick from a lot of different workflows: long-running branches, topic branches, merge or rebase, git-flow... Which one you choose depends on a couple of factors: your project, your overall development and deployment workflows and (maybe most importantly) on your and your teammates‘ personal preferences. However you choose to work, just make sure to agree on a common workflow that everyone follows.
The following document is based on experience doing code development, bug troubleshooting and code review across a number of projects using GIT, including libvirt, QEMU and OpenStack Nova. Examination of other open source projects such as the Kernel, CoreUtils, GNULIB and more suggested they all follow a fairly common practice. It is motivated by a desire to improve the quality of the Nova GIT history. Quality is a hard term to define in computing; one man's "Thing of Beauty" is another man's "Evil Hack". We can, however, come up with some general guidelines for what to do, or conversely what not to do, when publishing GIT commits for merge with a project, in this case, OpenStack.
This topic can be split into two areas of concern
Formatting Rules
Capitalized, short (50 chars or less) summary
More detailed explanatory text, if necessary. Wrap it to about 72 characters. In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank line separating the summary from the body is critical (unless you omit the body entirely); tools like rebase can get confused if you run the two together.
Always leave the second line blank.
Write your commit message in the imperative: "Fix bug" and not "Fixed bug" or "Fixes bug." This convention matches up with commit messages generated by commands like git merge and git revert.
Further paragraphs come after blank lines.
Examples of good practice
Example 1 (no description, only summary)
Example 2 (description as bullet points)
Example 3 (description as plain text)
Git Tips
Properly Configure your
~/.gitconfigGitHub tracks your changes by using the information provided by your
~/.gitconfig. If you work on more than one machine and your~/.gitconfigis not properly configured, you will probably end up with duplicated commits and disorganized history. Here are the lines you will have to modify according to your GitHub credentials.The difftool and mergetool are the software Git will execute during diff or conflict resolution operations respectively.
Cheat Sheet
Create
$ git clone ssh://user@domain.com/repo.git$ git initLocal Changes
$ git status$ git diff$ git add .$ git add -p <file>$ git commit -a$ git commit$ git commit --amendCommit History
$ git log$ git log -p <file>$ git blame <file>Branches and Tags
$ git branch$ git checkout <branch>$ git branch <new-branch>$ git checkout --track <remote/branch>$ git branch -d <branch>$ git tag <tag-name>Update and Publish
$ git remote -v$ git remote show <remote>$ git remote add <remote> <url>$ git fetch <remote>$ git pull <remote> <branch>$ git push <remote> <branch>$ git branch -dr <remote/branch>$ git push --tagsMerge and Rebase
$ git merge <branch>$ git rebase <branch>$ git rebase --abort$ git rebase --continue$ git mergetool$ git add <resolved-file> $ git rm <resolved-file>Undo
$ git reset --hard HEAD$ git checkout HEAD <file>$ git revert <commit>$ git reset --hard <commit>$ git reset <commit>$ git reset --keep <commit>obtained from
Beta Was this translation helpful? Give feedback.
All reactions