Like most new things we learn, Git has some hidden concepts and features we may only discover after years of use. In my case this is definitely true, I could have learned a lot more about Git if I spent some time reading the manual in full. But my lazy habits prevented me from doing so. Here is a list of tips that I think will be useful for developers new to Git.
At first, it might not be obvious why it's a good idea to make use of a global ignore file. The first advantage is that you get to specify a set of rules for all common files and folders that you would like to ignore across all your repositories. This also helps you avoid defining the same rules again and again for each new repository.
Don’t repeat yourself
I like to argue that it should be the responsibility of every developer to have there own global ignore file. The ignore file of a repository should cater only to itself and not to every developer that works on it. You can imagine, if a dozen developers were working on the same repository, all using there own unique tools and operations systems. The ignore file will become populated with hundreds of rules, non-related to the repository itself. So always make use of a global ignore file and ask your contributors to do the same.
A global ignore file can be created anywhere on your system. It should be placed in a location free from disturbances, you don't want to accidentally delete it or anything. You can also name the file as you please but stick to the standard convention. After you created the file you need to set the config property.
cd ~/touch .gitignore_globalgit config --global core.excludesfile ~/.gitignore_global
Here is a handy list of git ignore files you might find useful.
Git allows you to use any text editor to write your commit messages. To get this working, you have to be sure that you can open the text editor that you are planning to use from within your terminal, it needs to be available in your path. Once you are able to open your editor from the terminal, all that's left is to set the config property core.editor. Below is an example using Visual Studio Code.
git config --global core.editor 'code -w'
After running the commit command your editor will automatically open. You can then start to write your commit message, once you are done simply save and close the buffer.
If you are actively working on a remote repository being asked for your username and password every time, can quickly become an annoyance. Git offers you the option of storing your username and password in memory for a certain amount of time. When you enable this option without specifying the amount of time, the default will be 15 minutes.
git config --global credential.helper cache
You can extend the amount of time by including the timeout flag. The value of this flag is in seconds.
git config --global credential.helper 'cache --timeout=3600'
Git gives you the option to define your own aliases. They can be used to shorten standard commands or to create new ones. Having a good set of aliases can make you much more productive by eliminating the need to write out repetitive commands.
git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.st status
The above aliases will shorten all the standard git commands. You can now check out a branch writing only git co. It is also possible to create aliases for longer commands, by surrounding them in single or double quotes.
git config --global alias.last 'log -1 HEAD'
The above alias will show details about the last made commit. These aliases don't have to be defined on a global scale. Repository specific aliases can also be created by replacing the global flag with local.
To change the message or staged files of your last commit, you only have to run a single command.
git commit --amend
All file changes you want to make should be made before you call the above command. In a sense, your commit just gets updated and replaced. Here is an example, let's imagine you made a commit but forgot to include a file. To alter the commit and include the file, you first want to stage the file and then call the above command. The same principle applies to other operations.
git commit --amend git add sample-file.js