Check out a free preview of the full Everything You'll Need to Know About Git course
The "Config & Locations" Lesson is part of the full, Everything You'll Need to Know About Git course featured in this preview video. Here's what you'd learn in this lesson:
ThePrimeagen demonstrates how to use the `git config` command to add, list, and remove configuration values in Git. He shows how to add values to the config file using different scopes (global and local), how to list and retrieve specific values, and how to remove values and sections from the config file. How to rename the default branch from "master" to "trunk" using a global setting is also demonstrated.
Transcript from the "Config & Locations" Lesson
[00:00:00]
>> We're gonna go over some config now, it's good to know this. This is kind of like the last of the intro into get area. I remember the first time I use anything with config, it felt strange, and I did it that one time and then I never thought about it again.
[00:00:12]
And then when I had to alter it, I was just like, crap, I have to go look this up. And then you just always forget things, totally reasonable. So, --add adds a key to your git config. So if I do this, I just add a key. This is really important to remember.
[00:00:28]
And remember, a key is made up of two parts, a section and a key name. Very important things to remember. All right, problem, I want you to create three values with the section name of fem, frontend masters. I don't care what you do, I will use these three right here, fem.dev is great, marc is, he's ok, semi decent, and fem.git would, right?
[00:00:51]
I'll do it too. I'll go git config --add, and then I'll do the section name, remember, fem, then I'll do the key name, dev, and say "is great". There we go, and then I'll do mark is ok, and then git, I put little quotes around it just because, I don't know, I want to send it as a single argument there.
[00:01:15]
You might not even need quotes, I've actually not tried quotes, I just always quote everything. Old habits, die hard, and they get formed at some point in your head, and that's just that. So there we go, I have those three commits. All right, solution, there we go, hopefully everyone effectively did this.
[00:01:30]
There's some ways of listing out values, you can use --list, or you can use --get- regex expression. Problem, can you list out all the fem values in your config with a single command? Me personally, cuz of just how I'm not the world's greatest Linux person, I would probably do something like git config --list, and then probably grep out fem, that's what I would do.
[00:01:53]
And you can see I must have, looks like I actually have some old values in here. So I should probably take those out. [LAUGH] There we go. I knew it, got bamboozled myself, right there. All right, so hopefully everyone did that, absolutely fantastic. So the way to be able to list everything out is that I always just do this, I know it's not the most efficient way to use your Linux system, it's just how my brain always does things.
[00:02:25]
But you can use things like git config --get a regex expression, and then just go fetch. And this will actually just list out your three keys that match that. So that means I could also do something like M., and it would list out anything that kind of falls within that format, dot being a wildcard.
[00:02:43]
So, you kinda have your nice little regular expression, as you can see, we overmatched by accident cuz again, regular expressions, very dangerous to use. Don't put them in your code base, kids. All right, anyways, so problem, can you change one of the values of your config? Let's take fem.dev, I want you to change its value with all the commands you currently know that I've showed you, which is just add, list and get, so that's all you have.
[00:03:08]
I'll join you and I'll go git config, and I'll say, is amazing. Fem.dev is amazing. There we go, I've changed it. And then I want you to list it out with --get regex expression. Well, what happens when I do it? I have two fem.devs. It's one of the odd parts of git, is that you can actually accidentally add multiple of the same keys.
[00:03:36]
Which is also good, cuz that means you can have local settings and you can have global settings. You don't have to worry about collisions or anything like that, but you will add the same key by accident sometimes. It has happened, I might have two or three usernames at this point inside of my git configs, it's okay.
[00:03:52]
So, what happened here? It's a little bit of a surprise, right? You can use list or you can use .get key and see what comes out. So for fun, let's use --get, and I want you to get out fem.dev. So long as you've been following along and you're using the same keys as me, we have two with fem.dev.
[00:04:07]
When I do that, I get the later of the two values, the last one added. This should intrinsically make sense, always use the latest value, don't use the previous value, okay. If you use --get all, you'll get all of the values at once. All of this, by the way, is in the man page, so always review the man page if you forget or just Google it.
[00:04:25]
You can unset, which is a great name. I mean, me personally, I would have used remove, but unset is the opposite of adding. There's no set option. You know how much that bothers me that there's no set, but there's unset and add? It gets me every, I think at one point in here, I actually had a typo where I said set instead of add, because that's naturally how I feel about things.
[00:04:47]
Anyways, okay, so let's try to remove the duplicate key. Go ahead, try to remove the duplicate key. You have unset. When you try to do it, it says, hey, you can't do it, right? Fem.dev has multiple values. Did we unset one of them, did we unset all of them?
[00:05:09]
Well, you can always list it out, right? You can list out, we'll do this one, right? Look at that, both of them are still there. So you cannot unset a multiple value key, you have to unset all. Just as a little fun warning for you. So, you can do it all, let's repeat it again with --unset -all.
[00:05:29]
So I'm gonna go in here and unset all for fem.dev. And there we go, if I look at it, notice that, fem.dev completely gone, we removed all the keys for it. So you have to always remove all the keys, it's just kind of a thing. Fun fact, by the way, configuration is just a file.
[00:05:48]
Again, I've tried to express this upon you, how important it is to remember that git isn't magical. So I want you to just take a little quick look in the git folder, find that config file. Click look, see if you can find it. By the way, there are a lot of options, so you can unset via index, there's like 900 different things you can do, I'm just giving you the basics, okay?
[00:06:13]
The real talk, you're probably gonna use config like three times in your life and you're never gonna look at it again. But it's just good to know how to use it, then it's never confusing and it feels easy. All right, if you go like this, cat.getconfig, you'll see it's right there.
[00:06:26]
Mh-hm, does that look familiar? Yeah, those are the ones we added. There's nothing magical about it, it looks kind of like Tamil, which Tamil is another encoding sweet format for all of us to be able to use in a file. And so that's what it kinda looks like to me, there we go.
[00:06:41]
So, awesome, we have those things there. The magic is so gone, hopefully at this point, it's almost disappointing how simple that actually was, that the config just exists. You can actually remove everything by using --remove-section. So go ahead, give it a little try, let's remove the entire fem namespace.
[00:06:57]
So, [LAUGH] git config just ruining everything for me. There we go, remove the section. If I go and I cat out my config, look what's gone. We removed the section, the section was deleted. There you go, your commands are actually updating a file. All right, there's also locations.
[00:07:16]
Locations are system, global, local, worktree, you can provide a file path to a config. You can do all this, but reality is, is when you're using git you likely are only using global or local. Local is just repository level settings and global is what you want for everything.
[00:07:33]
All right, so let's try adding the same key fem.dev, to both local and global. I want you to start with global first. So I'll go git config, add --and I'll go --global. So this should add it to all of git. There we go, and then I'll do the exact same thing and I'll do it with local.
[00:07:52]
You don't have to specify the local command or the local location, but for this I will. I'm gonna do this really quickly, I just realized that I didn't give it a nice local. There we go. So now we added those two things, and now I want to list out my local items.
[00:08:13]
So if I go get config and I go --list --local, providing a location, you can see, I have fem.dev right there. If I just simply list everything out, instead of, again, I'm not using the grep option, which I could use the grep option. You can see right here, I have the global one and I have the local one.
[00:08:35]
Okay, so you can access them both. But which one should show up when I do --get fem.dev, which one do you think should show up?
>> Local.
>> Local, exactly, you can see that. All right, so let's do a quick thing, let's actually alter our git experience. Let's rename master to trunk.
[00:08:52]
So I want to do a global setting to init.defaultBranch. You can choose whatever name you want, for here I'm actually a laughing internally because I used to have to use SVN tortoise. And so we're gonna name it trunk, just remember how awful life can actually be. For all of my tortoises out there.
[00:09:12]
So I'm going to do a little global and go trunk. I just want everyone to feel the pain. Yeah, exactly, I want you to feel it. So there we go, we've now named our initial branch. Obviously, should this be local or global, should be global if you want it to take place for all repos for all time on your system.
[00:09:33]
Remember, we just got done naming your default branch to trunk. So if you forget that you did this class and you create a new repo, it will have trunk as its default name, and then you'll be triggered due to remembering SVN. Real fact, by the way. So let's try it out.
[00:09:50]
Let's just go and create a project right now, call it hello-git. This is important, call it hello-git, and do a git init inside that folder. So I'm gonna go back to back, I will make my own daughter hello git, and then I'll go to hello git, and I'll go get init.
[00:10:07]
Look at that, it's now trunk. There we go. So git has global settings, you can change your experience, there's actually quite a few experiential changes you can make to git, this is just the easiest one to demonstrate with no git knowledge.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops