This guide is a practical, copy-pasteable intro to Git.
What You’ll Learn
- How to install Git and configure your identity
- How to create a repository and make your first commit
- Core commands:
status
,diff
,add
,commit
- How to use
.gitignore
to keep secrets and temporary files out of version control - How to connect a local repo to GitHub (HTTPS or SSH) and push your code
- How to clone a repository and pull updates from a remote repository
- How to create and merge branches for feature development
Prerequisites
- Terminal access (Ubuntu/Debian shown here; Windows users can use WSL with Ubuntu)
- Git installed locally
- A GitHub account (or another remote host, if you want to push code)
- A text editor like nano, vim, or your favorite IDE
1) Install and Verify Git
Install 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
2) Configure Your Identity (Required), Set Default Branch Name
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
Expected output (name, email, default branch may vary):
user.email=you@example.com
user.name=Your Name
init.defaultbranch=main
3) Create or Initialize a Repository
Option A: Start from scratch in a new folder
mkdir git-project && cd git-project
git init
Option B: Initialize Git in an existing folder
cd /path/to/existing-project
git init
Expected output:
Initialized empty Git repository in /path/to/git-project/.git/
4) Adding and Committing Changes: add
, commit
Add 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"
5) See Changes: status
, diff
Check the current state:
git status
See what changed since the last commit (output will be empty if nothing changed since last commit):
git diff
6) Keeping Secrets Out: .gitignore
Create 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.
7) Connect to a Remote (GitHub)
Create a repo on GitHub
- Go to GitHub → New repository
- Name it (e.g.,
git-project
) - Choose Public or Private visibility
- Leave it empty (don’t add README/.gitignore/license) to avoid conflicts
- Copy the repo URL (HTTPS or SSH)
Add the remote to your local repo
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
8) Push Your Code
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
9) Cloning a Remote Repository
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
10) Branching Basics
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
Working with remote branches
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
Summary
- In this guide you learned how to set up Git, create a repository, and track changes with
status
,add
,commit
, anddiff
. - You set up a
.gitignore
to keep secrets and temporary files out of version control. - You connected your local repository to GitHub (or another remote), pushed your work, and saw how to clone a repo back down.
- Finally, you learned the basics of branching and merging so you can develop features safely.
With these commands, you now have the complete beginner’s workflow:
- Initialize a repo
- Make changes and commit them
- Keep your history clean with
.gitignore
- Push to GitHub
- Clone and branch when collaborating