Without a version control system, it’s next to impossible to manage the source code. You can be a software developer of C, C#, Java, or any other language, but you need version control. The best and one of the most popular version control systems is Git.
Git is a free open-source distributed version control system (VCS) used by many small and large companies. Its functionality is impressive, and many thousands of Git supporters gladly add more practices and tips to make the source control management more productive.
Currently, you can use more than 35 Git best practices and many tips. However, this article will focus on the best 11 Git tips and practices that are optimal for beginners.
I will start with my all-time favorite – the AFTER technique. AFTER stands for:
- Atomic Commits
- Frequent Commits
- Test Changes Before Push
- Enforce Standards
An atomic commit is a single commit based solely on one context. By this, I mean a single subject in terms of context: function, bug fix, refactor, update. If all your project changes go as a single commit, it is the monolithic way or the spaghetti commit.
The Git best practices claim that commits should not be based on a time basis (hourly, daily, etc.). Instead, you should commit based on a function basis. Therefore, you should commit whenever you add a commit-worthy change.
Did you incorporate a working technique? Commit.
Did you correct the typo? Commit.
Did you patch an incorrect indentation file? Commit.
As soon as the dedication is essential, there is nothing wrong with making minor adjustments.
Test Your Changes Before You Push
In Git, you can work both alone and with a team in collaboration. When you commit your code, you are working with your local repository, and when you push your changes, you are working with the remote repository and sharing your code changes with the team.
Testing your changes before you push them is a must. If you push the broken code, your team may get in a deadlock position because of the error. Therefore, always test your changes locally before you push them to the remote.
In any project, standards are crucial. They speed up the overall efficiency and minimize errors and delays. Note the two simple principles you can always enforce in your team:
- Use a gitignore file – it lets Git know that certain files should be ignored, as they aren’t a part of your code. It also helps to keep your repository healthy.
- Add an appropriate commentary with every commit. It is helpful for developers and serves as documentation.
Refactoring is not a Feature
For developers, code refactoring is common. It is also beneficial for many reasons, such as:
- Make the team’s code readable.
- Reduce sophistication.
- Make your source code more maintainable, etc.
The worst thing, however, is to refactor and add a new feature in the same commit.
If you want to do refactoring, it should be a separate commit. And if you are working on a new feature, it should go in a particular commit.
These practices compile the AFTER technique. Now, let’s proceed to other useful tips for working with Git.
Clone a Branch
Sometimes you need to clone a particular branch from the remote repository, not the entire repository. In this case, Git Remote Add is the solution:
git remote add -t <remoteBranchName> -f origin <remoteRepoUrlPath>
Set and Reset Author
To use Git, you have to set your name and email address after downloading it. The data will be attached to each commit you make. Hence, you won’t be able to build commits without it.
git config –global user.name “Your name”
git config –global user.email firstname.lastname@example.org
If you need for some reason to reset your name or email, here is the way:
git commit –amend –reset-author –no-edit
If a repository includes many hundreds of objects, there might be many unnecessary objects. It is possible to delete them with a specific command – a garbage collection command that will clean up your local repository.
Here is the syntax: git gc
Get your Git Guide
It’s always better to know the git commands. Git offers a separate command for this purpose, the git help command:
git help -g
By executing it, you can access and read all of the available git commands. The command is particularly important when you are learning Git.
The Git Archive command is a Git command-line utility that generates an archive file like commits, branches, or trees from listed Git Refs. Additional arguments that will change the archive output are approved by the git archive.
git archive –format=tar HEAD
This command constructs an archive from the current HEAD ref of the repository when executed. It is a valuable utility for building distributed git repository packages. Note: It allows several output formats in Git’s archive that can use added compression.
Know the .git folder
The .git folder contains all the version control information needed for your project and all the commit information, remote repository address, etc. It also provides a log that stores your history of committing so that you can go back to history.
The .git folder is the directory created when you do the git init or git clone. This “thing” makes a “git” repository for your project. Without it, the project is local – not a git project, and you can’t apply any git operations.
Deleting the .git folder does not remove any other files that are part of the git repository. However, it won’t be under version control anymore. Therefore, you should not delete this folder.
Git, being a vast system with many hundreds of thousand supporters, is constantly improving. Developers add new options and make the users’ cooperation much more effective, and the performance of the tasks smoother and faster.
Lots of software solutions for committing changes and working with remote repositories. Among them, it’s worth mentioning dbForge Source Control, a multi-functional SSMS add-in. Ifs functionality allows you to work with Git, and also such systems as SVN, TFS, and Microsoft Azure DevOps.
I hope that this article equipped you with essential knowledge on git tips and best practices, so you can boost your productivity. Thanks for reading, and I hope that this post will help you in your work.