GitLearnHub
Practice Flow
15px

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:

bash
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:

bash
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:

bash
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:

  1. Log in to GitLab
  2. Click your avatar (top-right) → Edit profile
  3. In the left menu click Access Tokens
  4. Click Add new token
  5. Give it a name like my-laptop-git
  6. Set an expiry date (1 year is common)
  7. Tick the write_repository scope
  8. Click Create personal access token
  9. Copy the token shown — you will never see it again after you close this page
⚠️
Copy and save your token somewhere safe (like a password manager) before closing the page — neither GitLab nor GitHub will show it again.

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:

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.

💡
If your token expires, remove the saved entry so Git prompts for a new one — on Windows, open Control Panel → Credential Manager → Windows Credentials; on macOS, use Keychain Access; on Linux, check your configured credential helper. The next Git push will ask you for a new token.

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):

bash
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:

bash
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.

💡
Already have an HTTPS clone and want to switch it to SSH? 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
*.logAny file ending in .log
bin/The entire bin folder (trailing slash = folder)
/config.jsOnly config.js at the root, not in subfolders
**/logsAny folder named logs anywhere in the tree
!important.logException — don't ignore this file even if a rule above matches it

Debug why a file is being ignored:

bash
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:

bash
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):

bash
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:

bash
# .gitattributes
* text=auto
*.csv     text eol=crlf
*.sh      text eol=lf
*.png     binary
*.dll     binary

Common mistake here →

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.

bash
# 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.