A practical, copy-pasteable introduction to Git covering installation, configuration, core commands, repository management, remote connections, and branching basics for beginners.
By Aaron O'Toole
This guide is a practical, copy-pasteable intro to Git.
status, diff, add, commit.gitignore to keep secrets and temporary files out of version controlInstall Git (Debian/Ubuntu):
sudo apt update
sudo apt install git
Check your version:
git --version
Expected output (version may vary):
git version 2.43.0
Set your name and email once (used in commit metadata):
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Set default branch name to main for new repos (standard for most projects):
git config --global init.defaultBranch main
Verify your settings:
git config --list
user.email=you@example.com
user.name=Your Name
init.defaultbranch=main
mkdir git-project && cd git-project
git init
cd /path/to/existing-project
git init
Initialized empty Git repository in /path/to/git-project/.git/
add, commitAdd files to your project (create a sample file):
touch example-file.md
Stage all changes (new files, modified files):
git add .
Commit staged changes with a message:
git commit -m "Added xyz improvements"
status, diffCheck the current state:
git status
See what changed since the last commit (output will be empty if nothing changed since last commit):
git diff
.gitignoreCreate a .env file for secrets (this won’t get commited)
touch .env
Create a .gitignore file to prevent committing secrets:
touch .gitignore
In your preferred text editor, open .gitignore to add the .env file and any other patterns you want to ignore.
Apply it:
git add .gitignore
git commit -m "Added .gitignore to keep secrets out of Git"
If you accidentally committed a secret, rotate it immediately and remove it from history. For simple cases:
git rm --cached secret.file
echo "secret.file" >> .gitignore
git commit -m "Remove secret.file from repo"
At this point, git ls-files won’t show ignored files, but ls -a will still list them on disk.
git-project)HTTPS (easiest to start):
git remote add origin https://github.com/<your-username>/<your-repo>.git
SSH (recommended once set up):
git remote add origin git@github.com:<your-username>/<your-repo>.git
Verify:
git remote -v
Make sure you’re on main (or your chosen branch):
git branch
First push (sets upstream so future git push knows where to go):
git push -u origin main
If you would like to clone an existing remote repository (e.g., from GitHub):
git clone https://github.com/<org>/<repo>.git
# or
git clone git@github.com:<org>/<repo>.git
Create a feature branch and switch to it:
git switch -c feature/awesome
Switch back to main:
git switch main
Merge feature/awesome into main:
git merge feature/awesome
Delete the merged branch (if no longer needed):
git branch -d feature/awesome
Fetch updates from the remote repository:
git fetch origin
View branches:
git branch -a
Switch to an existing remote feature branch:
git switch -b feature/awesome origin/feature/awesome
status, add, commit, and diff..gitignore to keep secrets and temporary files out of version control.With these commands, you now have the complete beginner’s workflow:
.gitignore