今日推荐英文原文：《5 Git Practices for Effective Work》
今日推荐英文原文：《5 Git Practices for Effective Work》作者：W3dcos
5 Git Practices for Effective Work
Commit early, commit often
(Photo by Aziz Acharki on Unsplash)One of Git’s key advantages is flexibility. Git makes it possible to handle small and large projects, and enables a smoother workflow for team members. Tracking changes, maintaining the source code backup, enabling the collaboration of the team, and deploying the source code on the server with a single command are considered to be the power of Git performance.
Yet, there is lots of stuff you don’t even guess that’s essential for productive work. To increase your work efficiency and achieve better results, we suggest the following points to consider while working.
1. Make Clean CommitsMany of you, while fixing a specific bug, may spot another one and try to solve all the things in just a single
git commit. Of course, this is not a good habit and can be problematic for you and your team members. Always keep in mind that fixing two different bugs should produce two separate commits. Keep the commits clean and contribute one commit for a single purpose. It will help others check your changes efficiently and make code reviews.
Also, don’t forget to write good commit messages (we have contributed a whole article on this topic). For that purpose, Git provides tools like the staging area (index) and the ability to stage only some parts of your file.
2. Commit Early and OftenGit is responsible for your data when you commit. Do not wait until you get a perfect commit. Split the feature implementation into small chunks and commit often. If you need a little bit more time to complete your work, just keep it updated with the latest changes so as not to get stuck in conflicts. If you commit early and often, you don’t lose work or revert changes, and you can trace your work while using the
git reflogcommand. However, don’t commit a half-done code. If you want to commit just because you need a clean working copy, just use the
git stashcommand instead!
3. Don’t Change Published HistoryAltering an already published history is a bad practice. Why? Let’s explain. Once a commit has been merged into an upstream branch and is visible to other programmers, it’s not recommended to change the history, to avoid conflicts. If you later find out that you litter your commit up, you can create new commits that fix the problem, for example by
git revert. However, there are times when altering a published history is perhaps a normal thing, if managed well.
4. Use Aliases
Git aliasis a shortcut that creates short commands mapping the longer ones. It demands a few keystrokes to run a command simplifying the programmer’s work. For example, we run:
git commit -m “commit message”
Instead, we can type:
git c -m “commit message”
Cutting a few letters every time will definitely save lots of time! Check out our article 7 Git Tricks to Save You Time.
5. Use Git Rebase in an Interactive ModeThere are lots of scenarios when your feature branch is ready to be integrated into the master, but a mess of commits doesn’t let you merge it.
You may have a bunch of commits in your branch, but once you want to merge it into the master, you should squash those commits into a clean commit. Git rebase interactive comes to help you with that:
git rebase — interactive <base>
Thanks to this magic mode, you can clean the history by filtering and modifying the existing sequence of commits.
To sum up, we undoubtedly can say that Git is a flexible tool that provides almost everything to make your workflow easier. It’s something like a magic world that we’re trying to dive deeper into and explore for you.