|
| 1 | +--- |
| 2 | + |
| 3 | +title: "How git stores your configs" |
| 4 | +description: "Have you ever wondered what's inside the .git directory?" |
| 5 | +date: 17-06-2022 |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | +### How commits work? |
| 11 | +A commit contains information related not only to changes in files, but also to the committers in general. Github, for example, uses emails to link committers to their respective accounts in the platform. |
| 12 | + |
| 13 | +Therefore, working with git configs means directly changing the information associated with your commits. |
| 14 | + |
| 15 | +### Viewing your configs |
| 16 | + |
| 17 | +If you have ever had to change or view the name or email displayed in your commits, you probably know the `git config user.name` command, or its variation `git config user.email`. These commands are useful to check the information associated with your commits. |
| 18 | + |
| 19 | +But how git stores these configs _under the hood?_ |
| 20 | + |
| 21 | +## Local and global configs |
| 22 | +Git stores configs in two different ways: globally or locally. Just like npm modules, the difference is where the files will be stored - inside of your current directory or in a system directory, being accessible from your whole system. **This differentiation is useful when, for example, working with multiple git accounts on the same computer.** You will be able to configure which account you will be using for each repository. |
| 23 | + |
| 24 | +### Locally: the `.git` directory |
| 25 | +Every time you start a git repository, what you are essentially doing is creating a .git directory inside it. You can open this file in VSCode by navigating to /.git (`cd .git`) and typing `code .` |
| 26 | +An empty repository will usually have a .git structure like this: |
| 27 | + |
| 28 | + |
| 29 | +Every interactive git change you do in your repository - that opens a text editor, by default Vim on windows - is going to edit git files in this folder. For example, let's say we forgot to use the `-m` flag when committing changes and it triggered a text editor to open: |
| 30 | + |
| 31 | + or, in Vim: |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +In both cases, what you are doing is editing a file called _COMMIT_EDITMSG_ inside of your .git directory. |
| 36 | + |
| 37 | + |
| 38 | +It also happens when you use the `rebase -i` command; in this case, you create a directory called _rebase-merge_, that contains the file `git-rebase-todo`, which is where you are going to make the changes. |
| 39 | +Git is also able to see if there's an ongoing rebase by checking if this directory exists or not: |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +### Okay, but.. What does it have to do with configs? |
| 45 | +All of these stuff happens locally, inside of your repository. Naturally, the config file is also there, in a file named `config` as well. |
| 46 | + |
| 47 | +If you open it, you'll be able to see a file like this: |
| 48 | + |
| 49 | + |
| 50 | +These settings include the url of the remote repository, and some information about branches and merges. |
| 51 | +See, for example, what happens if we change the "origin" - the remote url - to something different, by changing its URL: |
| 52 | +`git remote set-url origin google.com` |
| 53 | + |
| 54 | + |
| 55 | +Now, our remote URL points to `google.com` in the current repository. |
| 56 | +Commands such as `git remote set-url origin` are shortcuts for editing the config file directly in your CLI. But these properties are not the only ones you can change; let's say that we have multiple github accounts and want to set which one we are going to use for commits in a particular repository. |
| 57 | +We can do so by adding, to our config file, the following properties: |
| 58 | + |
| 59 | + |
| 60 | +**By doing so, we are overriding, for this repository, the _global git configs_.** |
| 61 | + |
| 62 | +_Side note: of course, using two github accounts on the same computer requires some additional steps, such as adding credentials to the Windows Credentials Manager, etc. But for customizing the author of commits, the method described here works._ |
| 63 | + |
| 64 | +###Okay, but.. Where are the global git settings stored? |
| 65 | +Even though you want to change the committer's information for a repository, git has some default settings what will be shared with every new file tracked with git in your system. These are the global configs, and you can see them by typing `git config --list --local`. ~~yeah, it also works with the --local flag~~ |
| 66 | +Unlike the local configs, the global ones are stored inside of a file called `.gitconfig`, that is inside of your system user directory. The changes applied to this file will have the same effect of setting things in the git CLI with a `--global` flag. |
| 67 | + |
| 68 | +### In conclusion... |
| 69 | +Even though it's not very common to make changes in .git files directly (instead, you are probably going to use git bash), it may be useful to understand a bit of how things work under the hood. After all, git deals with your files by creating also some other files to track information. I encourage you to take a look not only at the `config` file but also at other files and folders inside `.git`, you may find interesting things there! |
0 commit comments