Course Tonight

Course Tonight

Did You Know?

Docy turns out that context is a key part of learning.

Chapter 11: Aliases

Estimated reading: 5 minutes 7 views

Section 11.1: Simple aliases

There are two ways of creating aliases in Git:

With the ~/.gitconfig file:

[alias]
ci = commit
st = status
co = checkout

With the command line:

git config --global alias.ci "commit"
git config --global alias.st "status"
git config --global alias.co "checkout"

After the alias is created, you can use:

git ci instead of git commit, git st instead of git status, git co instead of git checkout.

As with regular Git commands, aliases can be used with arguments. For example:

git ci -m "Commit message..."
git co -b feature-42

Section 11.2: List / search existing aliases

You can list existing Git aliases using --get-regexp:

$ git config --get-regexp '^alias\.'

To search aliases, add the following to your .gitconfig under [alias]:

aliases = !git config --list | grep ^alias\\. | cut -c 7- | grep -Ei --color \"$1\" "#"

Then you can use:

git aliases – show ALL aliases git aliases commit – only aliases containing “commit”

Section 11.3: Advanced Aliases

Git lets you use non-Git commands and full shell syntax in your aliases if you prefix them with !.

In your ~/.gitconfig file:

[alias]
temp = !git add -A && git commit -m "Temp"

The fact that full shell syntax is available in these prefixed aliases also means you can use shell functions to construct more complex aliases, such as ones which utilize command line arguments:

[alias]
ignore = "!f() { echo $1 >> .gitignore; }; f"

The above alias defines the f function, then runs it with any arguments you pass to the alias. So running git ignore .tmp/ would add .tmp/ to your .gitignore file.

Note that aliases prefixed with ! in this way are run from the root directory of your Git checkout, even if your current directory is deeper in the tree. This can be a useful way to run a command from the root without having to cd there explicitly.

[alias]
ignore = "! echo $1 >> .gitignore"

Section 11.4: Temporarily ignore tracked files

To temporarily mark a file as ignored (pass file as parameter to alias), type:

unwatch = update-index --assume-unchanged

To start tracking the file again, type:

watch = update-index --no-assume-unchanged

To list all files that have been temporarily ignored, type:

unwatched = "!git ls-files -v | grep '^[[:lower:]]'"

To clear the unwatched list, type:

watchall = "!git unwatched | xargs -L 1 -I % sh -c 'git watch `echo % | cut -c 2-`'"

Example of using the aliases:

git unwatch my_file.txt
git watch my_file.txt
git unwatched
git watchall

Section 11.5: Show pretty log with branch graph

[alias

] logp = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short lg = log --graph --date-order --first-parent --pretty=format:'%C(auto)%h%Creset %C(auto)%d%Creset %s %C(green)(%ad) %C(boldcyan)<%an>%Creset' lgb = log --graph --date-order --branches --first-parent --pretty=format:'%C(auto)%h%Creset %C(auto)%d%Creset %s %C(green)(%ad) %C(boldcyan)<%an>%Creset' lga = log --graph --date-order --all --pretty=format:'%C(auto)%h%Creset %C(auto)%d%Creset %s %C(green)(%ad) %C(boldcyan)<%an>%Creset'

Here’s an explanation of the options and placeholders used in the --pretty format:

    • --graph: Draw the commit tree
    • --date-order: Use commit timestamp order when possible
    • --first-parent: Follow only the first parent on merge nodes
    • --branches: Show all local branches (by default, only the current branch is shown)
    • --all: Show all local and remote branches
    • %h: Hash value for commit (abbreviated)
    • %ad: Date stamp (author)
  • %an: Author username
  • %an: Commit username
  • %C(auto): Use colors defined in the [color] section
  • %Creset: Reset color
  • %d: –decorate (branch & tag names)
  • %s: Commit message
  • %ad: Author date (will follow –date directive) (and not committer date)
  • %an: Author name (can be %cn for committer name)

Section 11.6: See which files are being ignored by your .gitignore configuration

[alias

] ignored = !git ls-files --others --ignored --exclude-standard --directory && git ls-files --others -i --exclude-standard

Shows one line per file, so you can grep (only directories):

$ git ignored | grep '/$'
.yardoc/
doc/

Or count:

$ git ignored | wc -l
199811 # oops, my home directory is getting crowded

Section 11.7: Updating code while keeping a linear history

Sometimes you need to keep a linear (non-branching) history of your code commits. If you are working on a branch for a while, this can be tricky if you have to do a regular git pull since that will record a merge with upstream.

[alias]
up = pull --rebase

This will update with your upstream source, then reapply any work you have not pushed on top of whatever you pulled down.

To use: git up

Section 11.8: Unstage staged files

Normally, to remove files that are staged to be committed using git reset, reset has a lot of functions depending on the arguments provided to it. To completely unstage all files staged, we can make use of Git aliases to create a new alias that uses reset but now we do not need to remember to provide the correct arguments to reset.

git config --global alias.unstage "reset --"

Now, any time you want to unstage stages files, type git unstage and you are good to go.

Leave a Comment

Share this Doc

Chapter 11: Aliases

Or copy link

CONTENTS