Git Setup & Configuration
One-time setup that pays off every day: identity, remote access, SSH keys, ignore rules, and aliases.
First-time Setup
Before you can make your first commit, Git needs to know your name and email. This is recorded on every change you make so your teammates know who did what.
Run these two commands once — you never need to do this again on the same machine:
git config --global user.name "Your Name"
git config --global user.email "you@company.com"
Use your real name and the same email address you used to sign up to GitLab or GitHub. To check it worked:
git config --list
You should see your name and email in the output.
One more recommended default — make git pull rebase instead of merge, so pulling doesn't create noisy merge commits on your feature branches:
git config --global pull.rebase true
Connect to GitLab or GitHub (HTTPS)
To push and pull, you need to prove who you are. We use a Personal Access Token (PAT) — think of it as a password that only works for Git.
Step 1 — Create your token on GitLab:
- Log in to GitLab
- Click your avatar (top-right) → Edit profile
- In the left menu click Access Tokens
- Click Add new token
- Give it a name like
my-laptop-git - Set an expiry date (1 year is common)
- Tick the write_repository scope
- Click Create personal access token
- Copy the token shown — you will never see it again after you close this page
Step 2 — Store it so Git remembers it:
When you clone or push for the first time, Git will ask for your username and password. Enter:
- Username: your GitLab or GitHub username
- Password: the Personal Access Token (not your login password)
Windows saves this in Credential Manager automatically — you will not be asked again on this machine. macOS uses the Keychain; Linux uses libsecret or a configured credential helper.
SSH Key Setup (Alternative to HTTPS + PAT)
SSH keys are the more common setup for day-to-day development — once added, there's no token to renew every year. Either method works; pick one.
Step 1 — Generate a key (skip if ~/.ssh/id_ed25519 already exists):
ssh-keygen -t ed25519 -C "you@example.com"
Accept the default file location. A passphrase is optional but recommended.
Step 2 — Start the agent and add your key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Step 3 — Copy your public key:
cat ~/.ssh/id_ed25519.pub
Step 4 — Add it to your account:
Edit profile → SSH Keys → paste the key → Add key.
Step 5 — Test it:
ssh -T git@gitlab.com # or: ssh -T git@github.com
Then clone using the SSH URL instead of HTTPS — git clone git@gitlab.com:team/repo.git or git clone git@github.com:user/repo.git.
git remote set-url origin git@gitlab.com:team/repo.git — no need to re-clone..gitignore — Patterns and Debugging
The .gitignore file tells Git which files to never track. Pattern syntax:
| Pattern | What it matches |
|---|---|
*.log | Any file ending in .log |
bin/ | The entire bin folder (trailing slash = folder) |
/config.js | Only config.js at the root, not in subfolders |
**/logs | Any folder named logs anywhere in the tree |
!important.log | Exception — don't ignore this file even if a rule above matches it |
Debug why a file is being ignored:
git check-ignore -v filename.js
# Output tells you exactly which rule matched and from which .gitignore file
Already committed a file that should be ignored:
git rm --cached .env # stop tracking it (file stays on disk)
echo ".env" >> .gitignore
git commit -m "chore: stop tracking .env"
Global .gitignore — rules that apply to every repo on your machine (great for IDE and OS files):
git config --global core.excludesfile ~/.gitignore_global
# Then add entries like .DS_Store, Thumbs.db, .idea/, .vs/ to that file
.gitattributes — a sibling file that controls how Git treats tracked files, not whether it tracks them. Most common use: pinning line endings per file type so Windows and CI checkouts don't fight over CRLF vs LF:
# .gitattributes
* text=auto
*.csv text eol=crlf
*.sh text eol=lf
*.png binary
*.dll binary
Git Aliases — Shortcuts for Daily Commands
Aliases let you define short names for long commands. Set them once in your global config and they work in every repo.
# Pretty log with branch graph
git config --global alias.lg "log --oneline --graph --all --decorate"
# Undo last commit, keep changes staged
git config --global alias.undo "reset --soft HEAD~1"
# Unstage a file
git config --global alias.unstage "restore --staged"
# Short status
git config --global alias.st "status -sb"
After setting up: git lg, git undo, git st — less typing every day.