Git Intermediate Guide
Remote repositories are versions of your project hosted on the internet or network. The most common platforms are GitHub, GitLab, and Bitbucket.
Adding a Remote Repository
# Add a remote repositorygit remote add origin https://github.com/username/repository.git
# View remote repositoriesgit remote -v
# Output:# origin https://github.com/username/repository.git (fetch)# origin https://github.com/username/repository.git (push)Pushing Changes
Upload your local commits to a remote repository.
# Push to remote repositorygit push origin main
# Push and set upstream (first time)git push -u origin main
# Push all branchesgit push --all origin
# Force push (use with caution!)git push --force origin mainFetching and Pulling Changes
Download changes from a remote repository.
# Fetch changes (doesn't merge)git fetch origin
# Pull changes (fetch + merge)git pull origin main
# Pull with rebasegit pull --rebase origin mainDifference between Fetch and Pull:
git fetch: Downloads changes but doesn’t merge themgit pull: Downloads and automatically merges changes
Branching Strategies
Branches allow you to develop features in isolation without affecting the main codebase.
Creating and Switching Branches
# Create a new branchgit branch feature/user-authentication
# Switch to a branchgit checkout feature/user-authentication
# Create and switch (shorthand)git checkout -b feature/user-authentication
# Modern way (Git 2.23+)git switch -c feature/user-authentication
# List all branchesgit branch
# List all branches including remotegit branch -aBranch Naming Conventions
Follow these best practices for naming branches:
# Feature branchesgit checkout -b feature/add-login-pagegit checkout -b feature/payment-integration
# Bug fix branchesgit checkout -b fix/header-alignmentgit checkout -b bugfix/memory-leak
# Hotfix branchesgit checkout -b hotfix/security-patch
# Release branchesgit checkout -b release/v1.2.0Working with Branches
# Create feature branchgit checkout -b feature/shopping-cart
# Make changes and commitgit add .git commit -m "Add shopping cart functionality"
# Push branch to remotegit push -u origin feature/shopping-cart
# Switch back to maingit checkout main
# Delete local branchgit branch -d feature/shopping-cart
# Force delete (if not merged)git branch -D feature/shopping-cart
# Delete remote branchgit push origin --delete feature/shopping-cartMerging Branches
Combine changes from different branches.
Fast-Forward Merge
When there are no diverging commits:
# Switch to main branchgit checkout main
# Merge feature branchgit merge feature/shopping-cart
# Output: Fast-forwardThree-Way Merge
When branches have diverged:
# Switch to main branchgit checkout main
# Merge feature branchgit merge feature/shopping-cart
# Git will create a merge commitMerge with No Fast-Forward
Always create a merge commit (preserves branch history):
git merge --no-ff feature/shopping-cartResolving Merge Conflicts
Conflicts occur when the same lines are changed in different branches.
Example Conflict
# Attempt to mergegit merge feature/new-feature
# Output:# Auto-merging index.html# CONFLICT (content): Merge conflict in index.html# Automatic merge failed; fix conflicts and then commit the result.Resolving Conflicts
# 1. Check statusgit status
# 2. Open conflicted file(s)# You'll see conflict markers:Conflicted File:
<!DOCTYPE html><html><body><<<<<<< HEAD <h1>Welcome to My Site</h1>======= <h1>Welcome to Our Website</h1>>>>>>>> feature/new-feature</body></html>Resolved File:
<!DOCTYPE html><html><body> <h1>Welcome to Our Website</h1></body></html># 3. Mark as resolvedgit add index.html
# 4. Complete the mergegit commit -m "Merge feature/new-feature and resolve conflicts"
# Abort merge if neededgit merge --abortStashing Changes
Temporarily save uncommitted changes.
Basic Stashing
# Stash current changesgit stash
# Stash with a messagegit stash save "Work in progress on login feature"
# List all stashesgit stash list
# Output:# stash@{0}: On main: Work in progress on login feature# stash@{1}: WIP on main: 1234567 Previous work
# Apply most recent stashgit stash apply
# Apply and remove stashgit stash pop
# Apply specific stashgit stash apply stash@{1}
# Remove a stashgit stash drop stash@{0}
# Clear all stashesgit stash clearStash Untracked Files
# Stash including untracked filesgit stash -u
# Stash including ignored filesgit stash -aPractical Stashing Example
# Working on a featuregit checkout feature/user-profile# ... make changes ...
# Emergency bug fix needed!git stash save "User profile work in progress"
# Switch to main and fix buggit checkout maingit checkout -b hotfix/critical-bug# ... fix bug, commit, merge ...
# Back to feature workgit checkout feature/user-profilegit stash popViewing History and Diffs
Advanced Log Commands
# One line per commitgit log --oneline
# Graph view of branchesgit log --oneline --graph --all
# Last 5 commits with statsgit log -5 --stat
# Commits by authorgit log --author="John Doe"
# Commits in date rangegit log --since="2 weeks ago" --until="yesterday"
# Commits affecting specific filegit log -- path/to/file.js
# Pretty formatgit log --pretty=format:"%h - %an, %ar : %s"Viewing Differences
# Differences between working directory and staginggit diff
# Differences between staging and last commitgit diff --staged
# Differences between branchesgit diff main feature/new-feature
# Differences for specific filegit diff main feature/new-feature -- index.html
# Word-level differencesgit diff --word-diff
# Statistics onlygit diff --statTagging Releases
Tags mark specific points in history, typically for releases.
Creating Tags
# Lightweight taggit tag v1.0.0
# Annotated tag (recommended)git tag -a v1.0.0 -m "Release version 1.0.0"
# Tag a specific commitgit tag -a v0.9.0 abc1234 -m "Beta release"
# List all tagsgit tag
# Show tag informationgit show v1.0.0Pushing Tags
# Push specific taggit push origin v1.0.0
# Push all tagsgit push origin --tagsDeleting Tags
# Delete local taggit tag -d v1.0.0
# Delete remote taggit push origin --delete v1.0.0Undoing Changes
Unstage Files
# Unstage a filegit reset HEAD index.html
# Unstage all filesgit reset HEADDiscard Working Directory Changes
# Discard changes in a filegit checkout -- index.html
# Discard all changesgit checkout -- .
# Modern way (Git 2.23+)git restore index.htmlAmend Last Commit
# Change last commit messagegit commit --amend -m "New commit message"
# Add forgotten files to last commitgit add forgotten_file.jsgit commit --amend --no-editReset Commits
# Soft reset (keep changes staged)git reset --soft HEAD~1
# Mixed reset (keep changes unstaged) - defaultgit reset HEAD~1
# Hard reset (discard all changes)git reset --hard HEAD~1
# Reset to specific commitgit reset --hard abc1234Collaboration Workflow
Feature Branch Workflow
# 1. Update main branchgit checkout maingit pull origin main
# 2. Create feature branchgit checkout -b feature/add-comments
# 3. Work on featuregit add .git commit -m "Add comment system"
# 4. Push to remotegit push -u origin feature/add-comments
# 5. Create pull request on GitHub/GitLab# (Done through web interface)
# 6. After approval, merge via web interface or:git checkout maingit merge feature/add-commentsgit push origin main
# 7. Delete feature branchgit branch -d feature/add-commentsgit push origin --delete feature/add-commentsKeeping Feature Branch Updated
# Update feature branch with latest maingit checkout feature/my-featuregit fetch origingit rebase origin/main
# Or merge approachgit merge origin/mainQuick Reference
| Command | Description |
|---|---|
git remote add origin <url> | Add remote repository |
git push origin <branch> | Push branch to remote |
git pull origin <branch> | Pull changes from remote |
git checkout -b <branch> | Create and switch to branch |
git merge <branch> | Merge branch into current |
git stash | Save uncommitted changes |
git stash pop | Apply and remove stash |
git tag -a <tag> -m "<msg>" | Create annotated tag |
git log --oneline --graph | View branch graph |
git reset --hard HEAD~1 | Undo last commit |
Best Practices
- Commit Often: Make small, focused commits
- Write Clear Messages: Explain what and why, not how
- Pull Before Push: Always sync before pushing
- Use Branches: Don’t work directly on main
- Review Before Merge: Always review changes
- Tag Releases: Mark important milestones
- Keep History Clean: Use rebase when appropriate
Next Steps
Ready for advanced Git techniques? Check out the Git Advanced Guide to learn about:
- Git submodules
- Interactive rebase
- Cherry-picking
- Git hooks
- Advanced workflows