Git Tutorial for Beginners: Clone, Commit, Branch, Merge, and Push
Learn the everyday Git workflow: track changes, branch safely, sync with a remote, and pair with SSH authentication.
Getting started Beginner 7 min read
·
Git commands each solve one problem in version control: clone copies history so you can work offline; status compares your tree to the index; commit freezes a snapshot; push publishes commits. You run them in that order because each step depends on the previous state being correct.
For password-free Git over SSH, set up SSH keys for GitHub and GitLab.
Clone a repository
Why git clone: You need the full object database and default remote (origin) to pull updates and push branches; downloading a ZIP loses history and remotes.
git clone https://github.com/org/repo.git
cd repo
Check: git remote -v shows origin with fetch/push URLs.
Check status and stage changes
Why git status first: It tells you which files are modified, staged, or untracked—so you do not accidentally commit build artifacts or secrets.
git status
git add .
git commit -m "Describe the change"
Why git add: The index (staging area) is what gets committed; working-tree edits are not in a commit until staged.
Use git add -p to stage hunks interactively for cleaner commits.
Check: git status should show “nothing to commit, working tree clean” after a successful commit.
Branches
Why branch before editing: Keeps main releasable and isolates experiments; merging or PR review becomes possible without blocking others.
git branch feature-login
git switch feature-login
# or: git checkout -b feature-login
Finish with a merge on the main branch or open a pull request on the hosting site.
Pull, fetch, and push
Why fetch before assuming you are up to date: Remotes move when teammates push; fetch only downloads objects—safe to run anytime.
git fetchdownloads remote updates without changing your working tree.git pullis typicallyfetch+ merge (or rebase, depending on configuration)—use when you intend to integrate remote commits into your current branch.git push -u origin branch-namepublishes your branch and sets upstream tracking so futuregit push/git pullknow the default remote branch.
Check after push: The hosting site shows your branch; git status reports “up to date with origin/…”.
.gitignore
Why maintain a ignore file: Git would otherwise offer to commit generated files, huge binaries, and .env secrets—noise in diffs and real security risk.
List files Git should never track: build artifacts, .env with secrets (see environment variables guide), virtualenvs, and editor junk. GitHub offers starter templates by language.
Related guides
Automate deployments often pair Git with Docker Compose for consistent environments.
Frequently asked questions
What is the difference between git merge and git rebase?
Merge preserves branch history with a merge commit. Rebase replays your commits on top of another branch for a linear history; use carefully on shared branches.
I committed secrets—what now?
Rotate the credential immediately, remove it from history or invalidate the repo exposure, and follow our secrets guide.
HTTPS or SSH for clone URLs?
HTTPS is simple; SSH avoids typing passwords when keys are loaded. Many teams standardize on SSH for developers.
What does detached HEAD mean?
You checked out a specific commit instead of a branch. Create a branch with git switch -c name to keep work safely.