Lesson 4 of 5 in Power Tools

Config and aliases

Story: Every agent keeps their tools where their hands expect them. Time to arrange yours.

git config holds git's settings. You've set a few already: your name and email, pull.rebase and core.hooksPath. This Lesson adds aliases, short names for the commands you type most, a way to see every setting at once, and a way to remove one.

Aliases

An alias is a setting whose name starts with alias.. Its value is the command it stands for, without git in front:

$ git config --global alias.st status
$ git st
On branch main
nothing to commit, working tree clean

Now git st runs git status. With --global, the alias works in every repository on this computer.

An alias can hold flags too: put the value in quotes when it has spaces in it. Anything you type after the alias goes on the end of it:

$ git config --global alias.new "switch -c"
$ git new contact
Switched to a new branch 'contact'

git new contact runs git switch -c contact. Read an alias like any other setting to see what it stands for:

$ git config alias.new
switch -c

To change an alias, set it again. Some aliases many people make:

Alias Stands for
git st git status
git new <branch> git switch -c <branch>
git unstage <file> git restore --staged <file>
git graph git log --oneline --graph --all

git's own commands always win. An alias named log or commit is ignored, so an alias can't change what a git command does.

Aliases for one repository

Leave out --global, and an alias, like any setting, is for the repository you're in. A repository can even have its own alias with the same name as a global one:

$ git config --global alias.undo "reset --soft HEAD~1"
$ git config alias.undo "reset --hard HEAD~1"

So what does git undo do here?

Every setting at once: git config --list

$ git config --list
user.name=You
user.email=you@example.com
alias.st=status
alias.new=switch -c
alias.undo=reset --soft HEAD~1
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
alias.undo=reset --hard HEAD~1

Each line is one setting, name=value. The global settings come first, then this repository's own. The core. lines are settings git init wrote when it made the repository; you can leave them alone. Add --global to list only the global settings.

When a name is listed twice, the later line wins, so in this repository, its own setting wins over the global one. git config alias.undo prints the value that counts here:

$ git config alias.undo
reset --hard HEAD~1

In every other repository, git undo takes back the last commit and keeps its changes staged. In this one, it throws them away. Before you trust an alias in a repository, check what it does there.

The same goes for every setting: a user.email, pull.rebase or core.hooksPath set in a repository wins there over the global one. When git behaves differently in one repository, git config --list is the first place to look.

Removing a setting: git config --unset

To change a setting, you set it again. To remove one, use --unset. Without --global, it removes only this repository's own setting, so here the global one counts again:

$ git config --unset alias.undo
$ git config alias.undo
reset --soft HEAD~1

With --global, it removes a global setting instead: git config --global --unset alias.st removes your global alias st.

On your own computer

Global settings live in a file in your home folder, .gitconfig, and a repository's in .git/config. git config --list --show-origin says which file each line comes from. An alias that starts with !, like !echo hello, runs a shell command instead of a git command; this Terminal doesn't run those.

Command What it does
git config --global alias.<name> "<command>" Makes git <name> run git <command>, in every repository.
git config alias.<name> "<command>" The same, in this repository only. Here, it wins over a global alias with that name.
git config alias.<name> Prints what the alias stands for here.
git config --list Lists every setting: the global ones first, then this repository's.
git config --global --list Lists only the global settings.
git config --unset <name> Removes this repository's own setting.
git config --global --unset <name> Removes the global setting.

New commands

  • git config alias.*
  • git config --list
  • git config --unset

Explainer: Aliases are your own short names

project: your repo, you are here37d7cf7 Add a README37d7cf7ab89288 Add the home pageab89288a4b8d97 Style the headinga4b8d976d91e67 Make the heading navy (unreachable)6d91e67HEAD → contactmain
The repository at the end of the Explainer.
Graph as text

project: your repo, you are here

  • HEAD → contact at a4b8d97 "Style the heading"

Branches and tags

  • contact at a4b8d97 "Style the heading"
  • main at a4b8d97 "Style the heading"

Commits, newest first

  1. a4b8d97 "Style the heading"; parent ab89288; refs: HEAD → contact, main
  2. ab89288 "Add the home page"; parent 37d7cf7
  3. 37d7cf7 "Add a README"; no parent

Unreachable

  • 6d91e67 "Make the heading navy"; parent a4b8d97

Challenge 1 of 3

Story: Your own shorthand, Agent, from a case you've half forgotten. Look it up before you use it.

You staged everything, including debug.log, a log file that doesn't belong in the project. A while ago you made a few aliases in this repository, and one of them takes a file back out of the staging area. Find it, and use it on debug.log. Then commit the change to index.html on its own. Keep debug.log itself.

Unlocks after Rewriting History.

Terminal

Unlocks after Rewriting History.

This Lesson can be played once Rewriting History is complete. Its text and Explainer are open to everyone.