📜 ⬆️ ⬇️

What we need to configure Git!


Darova, Habr! (I did not invent anything more original)

I doubt that this note draws on a full post, but I still leave it here. What are we talking about?

We all heard about git. We all know that he is good. But only a few are trying to do something with it, somehow to push it through. Immediately I say, there will be nothing paranormal here, just a little work with the .gitconfig file. Yes, yes, it is with the file that is so reverently gathering dust in your home directory.
')
So, I'm already a bit tired of writing this, in fact, meaningless introductory text, so let's start doing something already.

What is .gitconfig and why is it needed?
The standard Git version control system has a great file - .gitconfig. Its purpose is obvious from the title. Strictly speaking, this is a user config. It allows you to customize aliases, add-ins, etc.

I do not know about other systems, but on Linux it lies in ~. If he is not there - feel free to create!

We open .gitconfig in a text editor convenient for you (for me it is nano).

Output light

Reading Git's dry output is hard enough. I have seen quite a few people who tolerated this. After one great command, they feel better. To enable color output, add lines to .gitconfig:

[color] ui = true 

We are recording ourselves

Why do you need it? If you run git log for some repository, you will see that for each commit, the author is characterized by Name and Email. To find us correctly, it is time to ask all this correctly. Attention! Changing this information during development is highly undesirable. She can break the story (multiple author)! It is advisable to select one name and a specific Email once.

 [user] name = Anonymous Doody email = anonymous.doody@anonymous.com 

Commit pattern

I do not know how other habrovchane work, but mostly my commits go to KDE Edu projects. There they clearly dictate the rules of form and type of commit, since it is parsed by different bots, etc. So God forbid that you make a mistake or do something wrong, there are templates. This template is very easy to make (and you can take it somewhere). To make it appear during git commit, add the following:

 [commit] template = ~/.commit-template 

In this case, ~ / .commit-template can of course be any file in your file system.

Credential helper

It happens that you need to perform several operations with a remote repository at a time. Each time you enter a name and password, name and password, name and ... Strains, no? Annoying me. Starting from version 1.7.10, Git supports Credential Helper.

 [credential] helper = cache --timeout=<> 

We set the right time (I have it at 3600 - one hour) and rejoice!

Aliases for frequent commands

I often use the commands checkout and branch , for example. To write three or four times the same thing - annoying. Let's replace them with a more concise version: cd and dir , for example.

 [alias] cd = checkout dir = branch mersq = merge --squash free = branch -D 

And now let's see what we did:
No modificationsWith modifications
 git pull --rebase git branch git checkout temp git add -u git commit git merge master git checkout master git merge --squash temp git commit git push git branch -D temp 

 git pull --rebase git dir git cd temp git add -u git commit git merge master git cd master git mersq temp git commit git push git free temp 


Remote prefixes

There is one trick that is often used by developers. These are prefixes for remote. They allow you to reduce the length of the address to the remote repository. You can set such for read-only and push. What for? This is logical for open-source projects. To reduce server load and speed, it is better to pull from anongit (read-only) without using SSH. What do I have for KDE?

 [url "http://anongit.kde.org/"] insteadOf = kde: [url "git@git.kde.org:"] pushInsteadOf = kde: 

Let's figure it out. Here we set up two URLs for pull and push. Set the kde prefix. What does this give us? Let's look at an example (the gh prefix is ​​not specified in the article for GitHub):
No modificationsWith modifications
 git clone http://anongit.kde.org/marble git clone https://github.com/user/repository 

 git clone kde:marble git clone gh:user/repository 



It is better, is not it?

Conclusion

The article omitted all sorts of "exotic" aliases and add. prefixes for different services. I can safely say that after Git motivated, it became more pleasant to work. All friends who tried it - agreed with me.

PS If I do something wrong or I’m already completely miserable, please write to me in PM and inform / advise (if it’s not difficult for you, of course).

UPD : More interesting and fresh can be found here .

Happy Coding!

Source: https://habr.com/ru/post/164297/


All Articles