Git

Setup & Config

Git · 9 entries

Initialize a Repository

syntax
git init [directory]
example
git init my-project
cd my-project
output
Initialized empty Git repository in /home/user/my-project/.git/

Note Creates a hidden .git folder. Running this in an existing repo is safe and will not overwrite anything.

Clone a Repository

syntax
git clone <url> [directory]
example
git clone https://github.com/user/project.git
git clone git@github.com:user/project.git my-local-name
output
Cloning into 'project'...

Note HTTPS asks for credentials each time unless you configure a credential helper. SSH uses your key pair and is generally preferred for frequent pushes.

Set User Name & Email

syntax
git config [--global] user.name "Name"
git config [--global] user.email "email"
example
git config --global user.name "Samira Khan"
git config --global user.email "samira@example.com"

# Per-repo override:
git config user.email "samira@work.com"

Note Without --global, the setting only applies to the current repository. Your commits will be attributed to whatever name/email is configured, so double-check with git config user.email before committing to a work repo.

Set Default Editor

syntax
git config --global core.editor "<editor>"
example
git config --global core.editor "code --wait"
# Or for vim:
git config --global core.editor "vim"

Note The --wait flag for VS Code is essential. Without it, Git thinks the editor closed immediately and aborts the commit message.

Create Aliases

syntax
git config --global alias.<shortcut> '<command>'
example
git config --global alias.co 'checkout'
git config --global alias.br 'branch'
git config --global alias.lg 'log --oneline --graph --all'

# Now use:
git co main
git lg

Note Aliases are stored in ~/.gitconfig. You can also edit that file directly under the [alias] section for complex aliases.

Ignoring Files with .gitignore

syntax
# .gitignore file patterns:
pattern
dir/
*.ext
!exception
example
# .gitignore
node_modules/
*.log
.env
.DS_Store
build/

# Track this specific log despite the wildcard:
!important.log

Note If a file is already tracked, adding it to .gitignore will NOT stop tracking it. You must first run: git rm --cached <file> to untrack it.

View All Configuration

syntax
git config --list [--global|--local|--system]
example
git config --list --global
git config --list --local
output
user.name=Samira Khan
user.email=samira@example.com
core.editor=code --wait
alias.co=checkout

Note Local config overrides global, which overrides system. Use --show-origin to see which file each setting comes from.

Credential Storage

syntax
git config --global credential.helper <store|cache|osxkeychain>
example
# macOS — use the system keychain:
git config --global credential.helper osxkeychain

# Linux — cache for 1 hour:
git config --global credential.helper 'cache --timeout=3600'

Note The 'store' helper saves passwords in plain text on disk. Prefer 'osxkeychain' on Mac or a credential manager on Windows. For SSH repos, credentials are handled by your SSH key instead.

Global .gitignore

syntax
git config --global core.excludesFile <path>
example
git config --global core.excludesFile ~/.gitignore_global

# Then in ~/.gitignore_global:
.DS_Store
Thumbs.db
*.swp
.idea/
.vscode/

Note Use this for OS and editor-specific files. Project-specific ignores still belong in the repo's own .gitignore so all contributors benefit.