Back
Git9 min read
0

Is Git Confusing? A Git Crash Course to Get You Started!

Git might seem confusing to some and people find it hard to understand. Here is a beginner's tutorial to just get you started with Git and GitHub.

Is Git Confusing? A Git Crash Course to Get You Started!

Git might seem confusing to some and people find it hard to understand. Here is a beginner's tutorial to just get you started.

What is Git?

Git is an open-source distributed version control system for tracking changes in source code during software development. Think of it like a time machine for your code - you can save snapshots of your work and go back to any point in time if something goes wrong!

Imagine you're writing a story. Every time you make progress, you take a photo of the page. If you don't like the latest changes, you can look at an older photo and continue from there. That's exactly what Git does for your code!

Git's 5 Staging Areas

Git has 5 important places where your code can live:

  1. Working Directory - This is where you're actively writing and editing your code
  2. Staging - A waiting area where you prepare files before saving them permanently
  3. Commit Tree - The history of all your saved snapshots (commits)
  4. Stash - A temporary storage box for code you're not ready to save yet
  5. Remote Repository - The cloud storage (like GitHub) where your code lives online

Think of it like packing for a trip:

  • Working Directory = Your messy room with clothes everywhere
  • Staging = The suitcase where you put clothes you want to pack
  • Commit Tree = Your travel journal with photos of each trip
  • Stash = A drawer where you temporarily put things
  • Remote Repository = The cloud backup of all your travel photos

Basic Git Commands

git add - Moving from Working Directory to Staging

The git add command adds changes in your working directory to the staging area. It's like putting items in your shopping cart before checkout.

bash
# Add a specific file

git add filename.js

# Add all files in the current directory

git add .

# Add all JavaScript files

git add *.js

What happens? Your changes move from the working directory (where you edited them) to the staging area (ready to be saved).

git commit - Moving from Staging to Commit Tree

The git commit command creates a snapshot of your code. It's like taking a photo and writing a caption about what you changed.

bash
# Create a commit with a message

git commit -m "Add login functionality"

# Create a commit with a detailed message

git commit -m "Add login functionality

- Created login form component
- Added authentication logic
- Fixed styling issues"

What happens? Your staged changes are saved permanently in Git's history. Each commit has a unique ID, like a photo timestamp!

git push - Moving from Commit Tree to Remote Repository

The git push command uploads your local commits to a remote repository (like GitHub). It's like uploading photos to the cloud so others can see them.

bash
# Push to the main branch

git push origin main

# Push to a specific branch

git push origin feature-branch

What happens? Your local commits are sent to GitHub, making your code available to others and backed up in the cloud.

git stash - Temporarily Saving Your Work

The git stash command temporarily saves your changes so you can work on something else. It's like putting your current work in a drawer while you handle an urgent task.

bash
# Save your current work temporarily

git stash

# See what's in your stash

git stash list

# Get your work back

git stash pop

# Get your work back and keep it in stash

git stash apply

What happens? Your uncommitted changes are saved temporarily, your working directory becomes clean, and you can switch to other work. When you're ready, you can bring your stashed changes back!

git checkout - Switching Between Versions

The git checkout command lets you switch between different versions of your code or different branches. It's like time traveling to different points in your project's history.

bash
# Switch to a different branch

git checkout feature-branch

# Create a new branch and switch to it

git checkout -b new-feature

# Go back to a previous commit (just to look, not to edit)

git checkout commit-hash

What happens? Git changes your files to match the branch or commit you checked out. It's like opening a different version of your project!

Working with Branches

Branches are like parallel universes for your code. You can work on different features without affecting the main code.

bash
# See all branches

git branch

# Create a new branch

git branch feature-login

# Switch to a branch

git checkout feature-login

# Create and switch in one command

git checkout -b feature-login

# Delete a branch

git branch -d feature-login

Why use branches? Imagine you're building a house. The main branch is the foundation. You can create a branch for "adding a kitchen" and another for "adding a bathroom". You work on each separately, then combine them when ready!

Merging - Combining Your Work

Merging is like combining two different versions of your story into one. When you're done with a feature branch, you merge it back into the main branch.

bash
# Switch to the branch you want to merge INTO (usually main)

git checkout main

# Merge the feature branch into main

git merge feature-login

# If there are conflicts, Git will tell you

# You'll need to resolve them manually

What happens? Git takes all the changes from your feature branch and adds them to the main branch. It's like combining two puzzle pieces!

Example scenario:

  1. You're on main branch with a file that says "Hello"
  2. You create feature-branch and change it to "Hello World"
  3. Meanwhile, someone else changes main to "Hello Friend"
  4. When you merge, Git tries to combine both changes
  5. If Git can't figure it out automatically, you get a merge conflict

Handling Merge Conflicts

When Git can't automatically combine changes, you get a conflict. Don't panic! It's like two people editing the same sentence - you just need to decide which version to keep.

bash
# After a merge conflict, Git marks the conflicts like this:

<<<<<<< HEAD
Hello Friend
=======
Hello World

> > > > > > > feature-branch
>>>>>>> feature-branch

How to fix:

  1. Open the file with the conflict
  2. Look for the <<<<<<<, =======, and >>>>>>> markers
  3. Decide which version you want (or combine both)
  4. Remove the markers
  5. Save the file
  6. Run git add filename.js
  7. Run git commit to complete the merge

Rebasing - A Cleaner History

Rebasing is like rewriting history to make it look like you worked on the latest version all along. Instead of merging, rebasing replays your commits on top of the latest code.

bash
# Switch to your feature branch

git checkout feature-branch

# Rebase onto main (get the latest changes from main)

git rebase main

# If there are conflicts, fix them, then:

git add .
git rebase --continue

What happens? Git takes your commits, temporarily removes them, updates your branch with the latest changes from main, then replays your commits on top. It's like moving your work to sit on top of the latest foundation!

Merge vs Rebase - Which Should You Use?

Use Merge when:

  • You want to preserve the exact history of when things happened
  • You're working with a team and want to see all branches clearly
  • You're merging a feature that's been worked on by multiple people

Use Rebase when:

  • You want a cleaner, linear history
  • You're working on a personal feature branch
  • You want your commits to appear as if they were made on the latest code

Think of it this way:

  • Merge = "I'll add my changes alongside the existing ones" (creates a merge commit)
  • Rebase = "Let me move my work to sit on top of the latest changes" (rewrites history)

Complete Git Workflow Example

Let's walk through a complete example of working with Git:

bash
# 1. Start a new project or clone an existing one

git clone https://github.com/username/project.git
cd project

# 2. Create a new branch for your feature

git checkout -b add-user-profile

# 3. Make some changes to files

# (edit files in your code editor)

# 4. Check what you've changed

git status

# 5. See the actual changes

git diff

# 6. Add your changes to staging

git add .

# 7. Commit your changes

git commit -m "Add user profile page"

# 8. Push your branch to GitHub

git push origin add-user-profile

# 9. Switch back to main

git checkout main

# 10. Get the latest changes from GitHub

git pull origin main

# 11. Merge your feature branch

git merge add-user-profile

# 12. Push the merged changes

git push origin main

# 13. Delete the feature branch (it's merged now)

git branch -d add-user-profile

Common Git Commands Cheat Sheet

Here's a quick reference for the most common Git commands:

bash
# See what files have changed

git status

# See the actual changes in files

git diff

# See commit history

git log

# See a simpler commit history

git log --oneline

# Undo changes in a file (before staging)

git checkout -- filename.js

# Unstage a file (remove from staging)

git reset HEAD filename.js

# Get latest changes from remote

git pull origin main

# See all remote repositories

git remote -v

# Clone a repository

git clone https://github.com/username/repo.git

Tips for Git Beginners

  1. Commit often - Small, frequent commits are better than huge ones
  2. Write clear commit messages - Future you will thank present you!
  3. Use branches - Don't work directly on main/master
  4. Pull before you push - Always get the latest changes first
  5. Don't panic about conflicts - They're normal and fixable
  6. Use .gitignore - Tell Git which files to ignore (like node_modules)

Conclusion

Git might seem scary at first, but it's really just a way to:

  • Save your work (git add and git commit)
  • Share your work (git push)
  • Work on different things (branches)
  • Combine your work (merge or rebase)
  • Go back in time (checkout previous commits)

Start with the basics: add, commit, and push. As you get comfortable, explore branches, merging, and rebasing. Remember, every expert was once a beginner. Happy coding! 🚀

Stay Updated

Get the latest posts and insights delivered straight to your inbox.