Skip to content

Commit

Permalink
docs: add shell expansion and Git aliases (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
HonkingGoose authored Oct 15, 2021
1 parent 1692795 commit 2982e4f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
62 changes: 62 additions & 0 deletions docs/best_practices/git_aliases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
id: git_aliases
title: Git aliases
description: Learn about Git aliases
---

You'll use commands like `git status`, `git log`, `git commit` and `git push` often.
Normally you have to type out Git commands in full before you can use them.

You can save some typing by setting up Git aliases, think of them as "shortcuts" to the full command.

:::tip
Aliases do not replace the original Git commands.

The full length Git commands are _always_ available.
:::

## Local vs global aliases

Aliases can be "local" or "global".

A local alias only works in the repository where you initially setup the alias.
A global alias can be used anywhere on your workstation.

### Setting a local Git alias

Use `git config` to set a local alias:

```git
$ git config alias.ci commit
```

Now you can choose whether you want to type `git commit` or `git ci` when making a new commit in the repository where you set the alias.

### Setting a global Git alias

Use `git config --global` to set a global alias:

```git
$ git config --global alias.ci commit
```

Now you can choose whether you want to type `git commit` or `git ci` when making a new commit in any repository on your workstation.

## Listing current aliases

You can use the `git config --list --local` command to see the local Git configuration.
Conversely, if you want to see the global configuration, use `git config --list --global`.

### Listing local aliases

```git
$ git config --list --local
alias.ci=commit
```

### Listing global aliases

```git
$ git config --list --global
alias.ci=commit
```
32 changes: 32 additions & 0 deletions docs/git_sandbox/shell_expansion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
id: shell_expansion
title: Shell expansion
description: Learn about shell expansion
---

A handy feature of the Bash shell - and most other shells - is that it comes with shell expansion.

You can type a partial Git command name, and Bash will try to expand it to the full command whenever you press <kbd>tab</kbd>.
Shell expansion will also suggest the correct filenames/locations when you use commands like `ls` or `cd`.

:::tip
If the partial command does not uniquely match a command, add some extra letters to make the match unique and press <kbd>tab</kbd> again.
:::

## Shell expansion with Git

Start typing part of the Git command, and use the <kbd>tab</kbd> key to let the shell suggest the rest of the command.
If you don't see any suggestions from the shell, try pressing <kbd>tab</kbd> a second time.

For example:

1. Type `git st`
1. Press <kbd>tab</kbd>
1. Shell expands to `git sta`, but nothing more happens.
1. Press <kbd>tab</kbd> again, you'll see a list of valid options:
```bash
stage stash status
```
1. Type in `t` to make a unique match for `status` and press <kbd>tab</kbd>
1. Now the shell expands to the full command: `git status`
1. Press <kbd>Enter</kbd> to execute the command
2 changes: 2 additions & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
label: "Git sandbox",
items: [
"git_sandbox/setup_your_git_sandbox",
"git_sandbox/shell_expansion",
"git_sandbox/explore_your_git_sandbox",
"git_sandbox/introduction_to_git_status",
"git_sandbox/create_a_new_file_and_track_it",
Expand All @@ -58,6 +59,7 @@ module.exports = {
"best_practices/use_a_gitignore_file",
"best_practices/split_up_your_commits",
"best_practices/write_good_commit_messages",
"best_practices/git_aliases",
`best_practices/use_a_linter`,
"best_practices/dealing_with_merges",
"best_practices/use_a_dependency_bot",
Expand Down

0 comments on commit 2982e4f

Please sign in to comment.