my-notes

Configuring_Git

2 min read

Configuring Git

Set up Git with your identity, preferences, and useful aliases.


πŸ‘€ User Identity (Required)

Set Your Name

git config --global user.name "Your Name"

Sets your name for all commits. This appears in commit history.


Set Your Email

git config --global user.email "you@example.com"

Sets your email for all commits. Use the same email as your GitHub account.


View Current Identity

git config user.name

Shows your configured name.

git config user.email

Shows your configured email.


βš™οΈ Configuration Levels

graph TD
    A[System Config] -->|/etc/gitconfig| B[Applies to all users]
    C[Global Config] -->|~/.gitconfig| D[Applies to your user]
    E[Local Config] -->|.git/config| F[Applies to this repo only]
    F -->|Highest Priority| F

System Level (All Users)

git config --system user.name "Name"

Applies to all users on the system.


Global Level (Your User)

git config --global user.name "Name"

Applies to all your repositories.


Local Level (This Repo)

git config --local user.name "Name"

Applies only to current repository. Overrides global settings.


πŸ“‹ View Configuration

View All Settings

git config --list

Shows all Git configuration settings.


View Settings with Origin

git config --list --show-origin

Shows settings along with which file they come from.


View Specific Setting

git config user.email

Shows the value of a specific setting.


πŸ”§ Common Settings

Set Default Branch Name

git config --global init.defaultBranch main

New repositories will use main as default branch instead of master.


Set Default Editor

git config --global core.editor "code --wait"

Sets VS Code as the default editor for commits and rebases.


Enable Auto-color

git config --global color.ui auto

Enables colored output in terminal.


Set Pull to Rebase

git config --global pull.rebase true

git pull will rebase instead of merge by default.


Set Push Default

git config --global push.default current

Push will automatically push current branch to same-named remote branch.


Enable Credential Caching

git config --global credential.helper cache

Caches credentials in memory for 15 minutes.


Cache Credentials Longer

git config --global credential.helper 'cache --timeout=3600'

Caches credentials for 1 hour (3600 seconds).


⌨️ Useful Aliases

Create Alias for Status

git config --global alias.st status

Now you can use git st instead of git status.


Create Alias for Checkout

git config --global alias.co checkout

Now you can use git co instead of git checkout.


Create Alias for Branch

git config --global alias.br branch

Now you can use git br instead of git branch.


Create Alias for Commit

git config --global alias.ci commit

Now you can use git ci instead of git commit.


Create Pretty Log Alias

git config --global alias.lg "log --oneline --graph --all --decorate"

Now you can use git lg for a beautiful log view.


View All Aliases

git config --get-regexp alias

Shows all configured aliases.


πŸ” SSH Setup

Generate SSH Key

ssh-keygen -t ed25519 -C "you@example.com"

Generates a new SSH key using Ed25519 algorithm.


Start SSH Agent

eval "$(ssh-agent -s)"

Starts the SSH agent in the background.


Add SSH Key to Agent

ssh-add ~/.ssh/id_ed25519

Adds your SSH key to the agent.


Copy SSH Public Key

cat ~/.ssh/id_ed25519.pub

Displays your public key. Copy this to GitHub.


Test SSH Connection

ssh -T git@github.com

Tests if SSH connection to GitHub works.


πŸ“Š Configuration File Example

Your ~/.gitconfig might look like:

[user]
    name = Your Name
    email = you@example.com

[init]
    defaultBranch = main

[core]
    editor = code --wait

[alias]
    st = status
    co = checkout
    br = branch
    lg = log --oneline --graph --all --decorate

[pull]
    rebase = true


#git #config #setup #aliases #ssh

Built with mdgarden