Aliases and Useful Configuration
Git is very configurable. A handful of aliases saves a lot of typing, a few settings change defaults that most people find better, and a .gitattributes file makes line endings and binary files behave the same for everyone on a team.
Aliases
An alias is a short name for a longer command:
git config --global alias.st "status -sb" git config --global alias.lg "log --oneline --graph --all --decorate" git config --global alias.last "log -1 HEAD --stat" git st
## main
git lg
* 529acae (soups) Add tomato soup * 90c0089 (HEAD -> main) Add omelette recipe * c4f5e47 Add pancake recipe
Aliases are stored in the [alias] section of your global config and accept extra arguments, so git lg -5 works.
A few popular ones:
[alias]
st = status -sb
co = checkout
sw = switch
br = branch -vv
lg = log --oneline --graph --all --decorate
last = log -1 HEAD --stat
unstage = restore --staged
undo = reset --soft HEAD~1
amend = commit --amend --no-edit
wip = !git add -A && git commit -m "WIP"
An alias starting with ! runs as a shell command, so it can chain several commands. Keep aliases you understand — when you're on someone else's machine, you'll need the full commands.
Settings worth knowing
| Setting | Effect |
|---|---|
init.defaultBranch main | Name of the first branch in new repositories |
pull.rebase true | Rebase instead of merge when pulling (lesson 34) |
push.autoSetupRemote true | The first git push on a new branch sets its upstream automatically |
fetch.prune true | Remove remote-tracking branches deleted on the server |
rebase.autoSquash true | Apply fixup! commits automatically (lesson 27) |
merge.conflictStyle zdiff3 | Show the original text in conflicts (lesson 25) |
rerere.enabled true | "Reuse recorded resolution": remember how you resolved a conflict and repeat it if the same conflict reappears |
diff.colorMoved default | Show moved lines in a different colour from added/removed ones |
branch.sort -committerdate | List recently used branches first |
commit.verbose true | Show the diff in the editor while writing a commit message |
help.autocorrect prompt | Offer to run the command you probably meant after a typo |
git config --global branch.sort -committerdate git branch
soups * main
Different settings for different folders
With includeIf, Git loads an extra config file only for repositories inside a particular folder — perfect for using a work email for work projects automatically:
[user]
name = Sam Rivera
email = sam@example.com
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
[user]
email = sam.rivera@company.example
.gitattributes
A .gitattributes file, committed to the repository, tells Git how to treat files by pattern. Its most important job is line endings: unlike each person's core.autocrlf setting, it applies to everyone who clones the project.
# Let Git normalise line endings of text files to LF in the repository * text=auto # Always use LF for shell scripts, even on Windows *.sh text eol=lf # Windows batch files need CRLF *.bat text eol=crlf # Never touch these: binary files *.png binary *.jpg binary *.pdf binary
| Attribute | Meaning |
|---|---|
text=auto | Git decides whether a file is text; text files are stored with LF and converted on checkout as configured |
eol=lf / eol=crlf | Always check out with these line endings |
binary | No line-ending conversion and no text diff |
-diff | Don't show diffs for these files (e.g. lock files) |
linguist-generated | (GitHub) collapse these files in pull request diffs |
After adding .gitattributes to an existing project, run git add --renormalize . and commit, so files already in the repository follow the new rules.
printf '* text=auto\n*.sh text eol=lf\n*.png binary\n' > .gitattributes
git check-attr -a run.sh logo.png
run.sh: text: set run.sh: eol: lf logo.png: binary: set logo.png: diff: unset logo.png: merge: unset logo.png: text: unset
Seeing everything
git config --global --list
alias.st=status -sb alias.lg=log --oneline --graph --all --decorate alias.last=log -1 HEAD --stat branch.sort=-committerdate
git config --list --show-origin shows every setting and the file it comes from — handy when a setting isn't doing what you expect.
Try it yourself
Add three aliases you'd actually use, set push.autoSetupRemote and fetch.prune, and check them with git config --global --list. In a project, add a .gitattributes with * text=auto and use git check-attr to see how Git treats a few files.