G
GuideDevOps
Lesson 3 of 18

Git Basics

Part of the Git & Version Control tutorial series.

The Basic Workflow

Four core operations repeated daily:

Edit files → Stage changes → Commit → Push to remote

Checking Status

Action:

git status

Result:

On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  modified:   README.md
 
Untracked files:
  (use "git add <file>..." to include in what will be committed)
  new-feature.js
 
no changes added to commit (use "git add" and/or "git commit -a")

Staging Changes

Action:

git add README.md
git status -s

Result:

M  README.md
?? new-feature.js

Creating Commits

Action:

git commit -m "docs: update README with installation steps"

Result:

[main a1b2c3d] docs: update README with installation steps
 1 file changed, 5 insertions(+), 2 deletions(-)

Viewing History

Action:

git log --oneline -3

Result:

a1b2c3d docs: update README with installation steps
f5e6d7c feat: add login validation
9a8b7c6 initial commit

Pushing & Pulling

Push to Remote

# Push current branch
git push
 
# First push of new branch
git push -u origin feature/new-api
 
# Push all branches
git push --all

Pull from Remote

# Fetch and merge changes
git pull
 
# Just fetch (don't merge yet)
git fetch
 
# Update after fetch
git merge origin/main

Undoing Changes

Discard Local Changes

# Discard changes in working directory
git checkout file.txt
 
# Discard ALL local changes
git checkout .

Unstage Files

# Remove from staging area
git reset file.txt
 
# Unstage everything
git reset

Remove Commits

# Keep changes, undo commit
git reset --soft HEAD~1
 
# Discard changes and commit
git reset --hard HEAD~1
 
# View specific commit
git show <commit-hash>

Remote Repositories

View Remotes

git remote -v
# Shows fetch and push URLs
 
# Add remote
git remote add origin https://github.com/user/repo.git
 
# Remove remote
git remote remove origin
 
# Change remote URL
git remote set-url origin new-url

Common Workflows

Feature Development

# Create and switch to feature branch
git checkout -b feature/search
 
# Make multiple commits
git add search.js
git commit -m "Implement search algorithm"
 
git add search.css
git commit -m "Style search results"
 
# Push feature branch
git push -u origin feature/search

Fixing a Mistake

# Oops, committed to wrong branch
git reset --soft HEAD~1  # Undo commit, keep changes
git stash                 # Save changes temporarily
git checkout main         # Switch to correct branch
git stash pop             # Apply changes
git commit -m "Right commit message"
git push

Tips

✓ Commit frequently with clear messages
✓ Pull before working to avoid conflicts
✓ Use branches for features and fixes
✓ Push to backup your work
✓ Keep commits focused on one change
✓ Write "why" in commit messages, not just "what"