Learn Git and GitHub the Easy Way: A Beginner's Guide

Learn Git and GitHub the Easy Way: A Beginner's Guide

If you're new to coding or just starting to work on real projects, Git can seem confusing. But once you understand the basics, it becomes one of the most powerful tools you'll use every day. This guide will walk you through Git step by step, using simple examples and clear explanations.


๐Ÿš€ What Is Git?

Git is a free tool that helps you:

  • Save versions of your code and keep track of every change.
  • Go back in time if something breaks or you make a mistake.
  • Work with others on the same project without overwriting each other's changes.

Think of Git as a smart "undo" and collaboration system for your code.


๐Ÿ› ๏ธ How to Set Up Git (First-Time Only)

Before you can start using Git, you need to tell Git who you are. This info gets saved with every change you make, so it's important to set it up properly.

Step 1: Open the Terminal

You need to use the terminal to enter Git commands.

If you're using VSCode:

  • Go to the top menu and click:
    View > Terminal

Or use the keyboard shortcut:

  • Ctrl + ` (on Windows or Linux)
  • Cmd + ` (on Mac)

This opens the terminal panel at the bottom of the editor.

If you're using macOS and not VSCode:

  • Open the built-in Terminal app.
  • Press Cmd + Space, type Terminal, and hit Enter.

The terminal is where you'll type and run Git commands.


Step 2: Set Your Name and Email

Once the terminal is open, run the following commands:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Replace "Your Name" with your actual name and use the email address you plan to use on GitHub.


What These Commands Do

  • user.name and user.email tell Git who is making each change.
  • This information shows up next to each commit.
  • The --global flag means these settings apply to all Git projects on your computer (you only need to do this once).

Check Your Git Settings

To make sure your info was saved correctly, run:

git config --global --list

This will show your name and email.

To see all Git settings and where they were set from:

git config --list --show-origin

Now you're ready to start using Git in any project!


๐Ÿงพ Most Used Git Commands (And What They Do)

Start a New Git Project

git init

This command tells Git to start tracking your project. It creates a hidden .git folder inside your project. You won't usually touch this folder, but it's where Git saves all the history and settings.


Check What's Going On

git status

This shows the current state of your files โ€” which ones are new, which ones have been changed, and which ones are ready to be saved (committed). It's a command you'll use a lot.


Add Files to Be Saved

Before you save your changes permanently, you need to stage them using:

git add file.txt

This stages the file file.txt.

To stage everything in the current folder:

git add .

To stage everything in the entire project (no matter where you are):

git add -A

Save the Changes (Make a Commit)

git commit -m "your message here"

This saves all the staged changes and adds a message so you know what the change was.

Good messages are short and clear, like:

git commit -m "add login form"
git commit -m "fix footer layout"

Avoid unclear messages like:

git commit -m "stuff"
git commit -m "things"

Commit messages help you remember what you did and help others understand your work too.


See Your Project History

git log

This shows all the past commits โ€” including who made them, the message, and when.

If you want a shorter version:

git log --oneline

This shows one line per commit, so it's easier to scan quickly.


๐Ÿ” How Git Tracks Files (File States Explained)

Each file in Git can be in one of several states:

  • Untracked โ€“ Git doesn't know about the file yet.
  • Staged โ€“ You've told Git to include the file in the next save (git add).
  • Committed โ€“ The file has been saved in the project history.
  • Modified โ€“ You've changed a file that was already saved.

Understanding these states is helpful when you're figuring out what will be saved and what hasn't yet.


๐Ÿงช Try It Yourself: Your First Git Project

Let's go step by step and practice everything.

  1. Create a folder called git-tutorial and open it in VSCode.
  2. Open the terminal (Ctrl + `).
  3. Run this to start Git in your project:
git init
  1. Create a file:
echo "Hello Git" > main.txt
  1. Add the file to the staging area:
git add main.txt
  1. Save it with a message:
git commit -m "add main.txt"
  1. Add more files:
echo "New file" > second.txt
git add .
git commit -m "add second.txt"
  1. See your history:
git log --oneline

Now you've made multiple commits and can review your project's history.


โœ๏ธ Making Changes and Adding New Files

Let's try editing and adding at the same time:

  1. Edit main.txt and change the text.
  2. Create a new folder called notes.
  3. Inside that folder, create a file: todo.txt.
  4. Add and commit both changes:
git add .
git commit -m "update main.txt and add todo.txt"

This shows how Git handles both modified and new files at the same time.


๐Ÿงฉ Using Git Inside VSCode

Instead of using the terminal, you can use the Git GUI built into VSCode.

  • Click the Source Control icon in the sidebar (it looks like a branch).
  • You'll see file changes, and you can:
    • Stage files by clicking the "+" button
    • Write commit messages in the input box
    • Click the checkmark to commit

This is helpful if you're more comfortable with a visual interface instead of using the terminal.


๐Ÿงญ Going Back in Time with Git

Every time you commit, Git saves a unique ID (called a hash). You can use it to go back to that version.

  1. See your history:
git log --oneline
  1. Copy the hash from a commit.
  2. Move to that version:
git checkout <commit_hash>

While in this "detached" state, don't make changes. When you're ready to go back to the main version:

git checkout main

โš ๏ธ Important Git Tips

git add . vs git add -A

  • git add . adds changes from the current folder only.
  • git add -A adds changes from the entire project, even from other folders.

To make sure you don't miss anything, use git add -A.


Never Touch the .git Folder

This folder stores Git's internal data. Do not:

  • Create folders inside .git
  • Run git init inside it
  • Edit or delete files in it

Doing this can break your project and delete your commit history.


๐ŸŽฏ Practice Challenge

  1. Create a new folder: my-git-project.
  2. Open it in VSCode and open the terminal.
  3. Start Git:
git init
  1. Create a file and write something inside.
  2. Add and commit it:
git add .
git commit -m "initial commit"
  1. Edit the file, create another one, and repeat the process.
  2. Use git log --oneline to see what you've done.

Repeat the process to practice. The more you use Git, the more natural it becomes.


โœ… What's Next?

Once you're comfortable with these basics, the next step is to learn:

  • Branches โ€“ Work on features without touching the main code.
  • Merging โ€“ Combine different branches together.
  • Rebasing โ€“ Clean up your history.
  • Fixing conflicts โ€“ Handle situations when changes overlap.

That's all coming in the next guide.


You're doing great โ€” Git is your friend now! ๐Ÿ™Œ

Git Branches, Merging, and Rebasing โ€” A Simple Guide

Now that you know the basics of Git, it's time to learn how to work on different tasks or features without breaking your main project. That's where branches come in. You'll also learn how to merge and rebase changes, which are two ways of bringing branches back together.


๐ŸŒฑ What Are Branches?

A Git branch is like a separate copy of your project where you can work without affecting the original. This is helpful when you're adding new features, fixing bugs, or testing ideas.

Common Branch Commands

git branch               # List all branches
git branch new-feature  # Create a new branch
git checkout main       # Switch to another branch
git switch main         # Same as checkout (Git 2.23+)
git checkout -b new     # Create and switch in one step
git switch -c new       # Same as above (Git 2.23+)

๐Ÿงช Example: Creating and Switching Branches

Let's walk through it.

  1. Open VSCode and create a folder called git-branches.

  2. Open the terminal:

    • VSCode: Click View > Terminal or use Ctrl + `` (Windows/Linux) or Cmd + `` (Mac).
    • macOS Terminal: Press Cmd + Space, type Terminal, and hit Enter.
  3. Start Git:

git init
  1. Create a file and commit:
echo "main file" > main-01.txt
git add .
git commit -m "add main-01.txt"
  1. Do the same for another file:
echo "another main file" > main-02.txt
git add .
git commit -m "add main-02.txt"
  1. Create and switch to a new branch:
git checkout -b feature
  1. Check current branch:
git branch
  1. Add a new file in this branch:
mkdir features
echo "feature work" > features/feature-01.txt
git add .
git commit -m "add feature-01.txt"
  1. Switch back to main:
git checkout main

Notice that feature-01.txt is gone โ€” that's because each branch has its own files and changes.


โฉ Quick Practice: Create a Bugfix Branch

  1. Run this to create and switch to a bugfix branch:
git checkout -b bugfix
  1. Add and commit a new file:
mkdir bugfix
echo "bugfix work" > bugfix/file-01.txt
git add .
git commit -m "add bugfix file"
  1. Switch back to main:
git checkout main

Again, the new file won't appear here. Each branch is its own world.


๐Ÿ”€ Merging Branches

Merging brings changes from one branch into another.

1. Fast-Forward Merge

Happens when the target branch (like main) has no changes of its own. Git just moves its pointer forward.

Example:

  • You are on the feature branch and want to merge main:
git merge main

If main hasn't changed, this merge is fast and clean.

2. Three-Way Merge

Used when both branches have made changes. Git creates a new merge commit to combine both histories.

Steps:

  1. Switch to the branch you want to merge into:
git checkout main
  1. Make sure you're on the right branch:
git branch
  1. Merge the feature branch into it:
git merge feature
  1. See the result:
git log --oneline

Look for something like:

(HEAD -> main, feature)

That means both branches point to the same commit.


๐Ÿงช Practice: Merge main into bugfix

  1. Switch to bugfix:
git checkout bugfix
  1. Merge main (this will create a merge commit):
git merge main

Git may open a text editor (like Vim or Nano) to confirm the commit message.

In VSCode GUI:

  • You'll see the message box in the Source Control panel. Just click Commit.

In Vim:

  • Press i to edit.
  • Press Esc, type :wq, and hit Enter to save and quit.

In Nano:

  • Type your message.
  • Press Ctrl+O then Enter to save.
  • Press Ctrl+X to exit.
  1. Now go back to main and merge bugfix:
git checkout main
git merge bugfix
  1. Check history:
git log --oneline
  1. Check current status:
git status

๐Ÿงน Deleting Branches

Once a branch is merged, you can safely delete it.

git branch -d branch-name

If it's not merged yet, Git will stop you from deleting it by accident.

To force-delete:

git branch -D branch-name

Example:

git checkout main
git branch -d feature
git branch -d bugfix

Create a test branch:

git checkout -b test
echo "test" > test.txt
git add .
git commit -m "add test file"

Now delete it from main:

git checkout main
git branch -d test  # Git warns it's not merged
git branch -D test  # Force delete

๐Ÿ” Why Merge main into Feature?

Sometimes your teammates update main while you're working on a feature. Merging main into your branch helps you:

  • Get the latest updates
  • Solve conflicts early
  • Avoid surprises later when merging back

๐Ÿ“ What Is Rebase?

Rebase is another way to bring changes from one branch into another. But instead of merging, it moves your commits and creates a clean, straight timeline.

When to Use It?

  • To make history cleaner.
  • Avoid on shared/public branches like main โ€” it changes history and can confuse others.

Rebase Example (Instead of Merging)

  1. Create a new project:
mkdir git-rebase
cd git-rebase
git init
  1. Repeat the same steps as before โ€” make a main, feature, and bugfix branch.

  2. In feature or bugfix, instead of merging:

git rebase main

Now your branch will be updated with main's changes, and your commits will be placed on top.

Compare this to the git-branches project. The commit history is cleaner, but the result is the same.

Some teams prefer rebase, others prefer merge. At work, you usually follow team rules.


๐Ÿง‘โ€๐Ÿ’ป Using VSCode for Branching & Merging

You can do all of this in VSCode without typing commands.

  • Use the Source Control panel.
  • Click the "..." menu to create branches, switch, and merge.
  • When merging, VSCode will often auto-commit for you.
  • It's quicker and easier, especially for beginners.

๐Ÿง  Git Challenge: Branches & Merging

Try this on your own:

  1. Create a folder: my-branch-project
  2. Open it in VSCode and start Git:
git init
  1. Add and commit a couple of files in main.
  2. Create feature and bugfix branches:
git checkout -b feature
git checkout -b bugfix
  1. Add different files in each branch and commit them.
  2. Merge main into feature, then merge feature into main.
  3. Do the same with bugfix, but try rebase instead of merge.
  4. Try deleting the branches.
  5. Create a test branch, commit something, and practice forced deletion.

โœ… Summary

  • Use branches to safely work on features.
  • Use merge to combine branches.
  • Use rebase to clean up history (only when it's safe).
  • Use VSCode to manage Git without the terminal if you prefer visuals.

With practice, all of this becomes second nature!


Keep practicing โ€” you're one branch away from mastering Git! ๐ŸŒฟ

Git Deep Dive: Config, Ignore, Stash, Reset, and Recovery

Now that you're comfortable with the basics of Git and branches, let's explore some important Git tools and tricks that will make your work easier and safer. In this guide, you'll learn how to:

  • Use global and local Git settings
  • Ignore files with .gitignore
  • Stash temporary changes
  • Understand HEAD and detached HEAD
  • Use reflog to recover lost commits
  • Undo and reset changes safely

๐Ÿ”ง Git Config (Global vs Local)

When using Git, it's important to tell Git who you are. You can do this globally (for all projects) or locally (just for one project).

Global Git Config

To set your name and email globally, open the terminal and run:

git config --global user.name "Your Name"
git config --global user.email "your@email.com"

This will apply to all Git projects on your computer.

Local Git Config

If you want to use a different name or email in just one project, you can set it locally:

git config --local user.name "Another Name"
git config --local user.email "another@email.com"

These settings are stored inside the .git/config file in your project folder.

You can view local settings with:

git config --local --list

Or open the file .git/config in any text editor.


Extra: Customize Default Branch Name

By default, Git uses main or master as the first branch name. But you can set your own:

git config --global init.defaultBranch shakeAndBake

Now all new Git projects will start with a branch named shakeAndBake. It's great for teams who follow specific naming rules.


๐Ÿšซ Ignoring Files with .gitignore

Some files don't belong in your Git history โ€” like passwords, log files, or generated files.

Create a .gitignore file in your project and add filenames or patterns you want Git to ignore.

Example:

.env
node_modules/
*.log

Git will not track these files, and they won't show up in git status.


Practice: Try It Yourself

  1. Create a project and run:
git init
  1. Create a file and commit it:
echo "hello" > main-01.txt
git add .
git commit -m "initial commit"
  1. Create a .gitignore file and add:
.env
  1. Create a .env file with:
SECRET_KEY=supersecret
  1. Run:
git status

You'll see .env is ignored โ€” it won't be staged or committed.


๐Ÿ‘œ Git Stash: Save Work for Later

Sometimes you need to switch tasks but don't want to commit unfinished work. git stash helps with that.

  • Save changes:
git stash
  • Save with a message:
git stash push -m "WIP: fixing button style"
  • See stash list:
git stash list
  • Reapply the last stash and remove it:
git stash pop
  • Reapply without removing:
git stash apply
  • Apply a specific stash:
git stash pop stash@{0}

Stash only works if your project has at least one commit. Also, it only saves tracked files.


๐Ÿ“ Understanding HEAD and Detached HEAD

HEAD

HEAD is a pointer that shows where you are in your Git history. Usually, it points to the latest commit on your current branch.

When you make a new commit, HEAD moves forward.

Detached HEAD

If you checkout a specific commit (instead of a branch), HEAD gets "detached."

Example:

git checkout abc123

Now you're not on any branch. Any commits made now will not belong to a branch โ€” they're "orphaned."

To save your work:

git checkout -b new-branch

That creates a new branch and attaches HEAD to it again.

๐Ÿ’ก Use git log or git show <hash> to view a commit instead of checking it out, so you avoid detached HEAD mode.


๐Ÿงญ Git Reflog: Recover Lost Commits

Made a mistake? Deleted a branch? Don't panic โ€” git reflog can help.

git reflog

This shows everything Git has done โ€” every branch switch, commit, reset, etc. You can use it to recover lost commits:

  1. Copy the commit hash from git reflog.
  2. Checkout that commit or branch:
git checkout <hash>
git checkout -b recovered-branch

Think of git log as your official history, and git reflog as your full activity log.


โ™ป๏ธ Git Reset: Undoing Commits

Git reset helps you undo commits or unstage files.

Undo the Last Commit (keep changes staged):

git reset --soft HEAD~1

Undo and discard changes completely:

git reset --hard HEAD~1

Reset to a specific commit:

git reset --hard <commit-hash>

Unstage a file:

git reset filename.txt

Unstage all files:

git reset

๐Ÿ’ก HEAD1 means "one commit before the current one". HEAD2 would mean "two commits back".


๐Ÿ”™ Git Restore and Checkout

To undo changes you haven't staged yet, use:

git restore filename.txt

To discard all unstaged changes:

git restore .

Or use the older version:

git checkout -- filename.txt
git checkout .

Both do the same, but git restore is newer and easier to read.


๐Ÿงน Cleaning Untracked Files

Untracked files are files Git doesn't know about (you haven't added them).

Preview what will be deleted:

git clean -dn

Actually delete untracked files:

git clean -df

Be careful โ€” this cannot be undone.


๐Ÿ”ฅ Deleting Unmerged Branches

If you try to delete a branch that hasn't been merged:

git branch -d branch-name

Git will stop you.

To force delete:

git branch -D branch-name

The commits are still saved (for a while) and can be recovered using git reflog.

Tips:

  • Merge important changes before deleting
  • Create a backup branch if unsure
  • Only use -D when you're 100% ready

๐Ÿง  Git Challenge: Practice Config, Ignore, Stash & Recovery

Try these steps to put everything together:

  1. Create a folder my-git-tools
  2. Open in VSCode, open terminal
  3. Run:
git init
  1. Set local name and email:
git config user.name "Your Local Name"
git config user.email "your@local.email"
  1. Add and commit 3 files (main-01.txt, main-02.txt, main-03.txt)
  2. Create .gitignore with:
temp\*.md
  1. Create .temp-1.md and .temp-99.md
  2. Run:
git status
  1. Modify main-01.txt but don't commit
  2. Stash your changes:
git stash push -m "work in progress"
  1. Check stashes:
git stash list
  1. Apply the stash:
git stash pop
  1. View commit log:
git log --oneline
  1. Copy a hash and check it out:
git checkout <hash>
  1. Make a change and commit
  2. Realize you're in detached HEAD, save it:
git checkout -b recovery-branch
  1. Switch back to main

  2. View reflog:

git reflog
  1. Make 2 commits

  2. Run:

git reset --soft HEAD~1
  1. Edit and re-commit

  2. Run:

git reset --hard HEAD~1
  1. Confirm changes are gone

  2. Stage a change, then unstage it:

git restore --staged filename.txt
  1. Make more changes, discard them:
git restore .
  1. Create temp.txt and preview clean:
git clean -dn
  1. Delete it:
git clean -df

โœ… Summary

  • Use local config when working in different teams/projects
  • Use .gitignore to keep sensitive and messy files out of Git
  • Use stash to save work temporarily without committing
  • Understand HEAD and reflog to recover lost work
  • Use reset, restore, and clean to undo, fix, or clean things up

These are the everyday tools real developers use to avoid mistakes and recover fast when something goes wrong.


You're now a Git troubleshooting ninja โ€” stash it, reset it, or reflog your way to safety! ๐Ÿง™

Git Merge Conflicts, Rebase, Squash, and Cherry-Pick: Explained Simply

As your Git projects grow, you'll start working with multiple branches โ€” and sooner or later, you'll run into merge conflicts. Don't panic. Conflicts are normal and fixable. In this guide, we'll explain merge conflicts, how to fix them, and how to work with powerful tools like rebase, squash, and cherry-pick.


๐Ÿ’ฅ What Is a Merge Conflict?

A merge conflict happens when Git can't figure out how to combine changes from two branches. This usually occurs when:

  • Two branches changed the same line in a file
  • One branch deleted a file while the other edited it
  • A file is renamed differently in two branches
  • One branch creates a file, the other creates a folder with the same name
  • Binary files (like images) are edited differently in each branch

When this happens, Git pauses the merge and marks the file as conflicted โ€” waiting for you to fix it.


๐Ÿ”„ Rebase vs Merge: What's the Difference?

Both git merge and git rebase are ways to combine branches, but they behave differently.

๐Ÿ‘‰ Merge (git merge main from feature branch)

  • Keeps the full branch history
  • Creates a new merge commit
  • Keeps the context of both branches

How it works:

  1. Finds the common base commit
  2. Combines the new changes from both branches
  3. Creates a merge commit

๐Ÿ‘‰ Rebase (git rebase main from feature branch)

  • Rewrites history
  • No merge commit โ€” creates a clean, linear history
  • Moves your changes on top of the other branch

How it works:

  1. Temporarily removes your changes
  2. Updates your branch to match main
  3. Re-applies your changes one by one

๐Ÿ’ก Use rebase when you want a cleaner history. Use merge when you want to preserve the full story.


๐Ÿงจ Merge Conflicts During Rebase

Rebase also runs into conflicts โ€” especially if the changes in the base branch touch the same lines you've changed.

What happens:

  • Git stops and tells you which files are in conflict
  • You must fix the file(s)
  • Then run:
git add filename.txt
git rebase --continue

You can also cancel a rebase:

git rebase --abort

๐Ÿงบ Squashing Commits

Sometimes you make lots of small commits while working on a feature. Instead of merging all of them into main, you can squash them into one commit.

1. Squash with merge:

git merge --squash feature
git commit -m "add full feature in one commit"

2. Squash with interactive rebase:

git rebase -i HEAD~n

This opens a list of your last n commits in a text editor. Change pick to squash or s for the commits you want to combine.

Follow the prompts to write the new, single commit message.


๐Ÿ’ Cherry-Pick

Cherry-pick means copying a single commit from one branch and adding it to another.

This is useful when:

  • You want one small change from another branch
  • You don't want to merge the whole branch

Steps:

  1. Find the commit hash:
git log --oneline
  1. Switch to the target branch:
git checkout cherry-target
  1. Apply the commit:
git cherry-pick <commit-hash>
  1. If there are conflicts, resolve them, then run:
git add .
git cherry-pick --continue

You can cherry-pick multiple commits too:

git cherry-pick a1b2c3..f6g7h8

๐Ÿง  Git Challenge: Practice Merge Conflicts, Rebase & Cherry-Pick

Try this exercise to put everything together:

๐Ÿ”ง Setup

  1. Create a new folder: git-merge-rebase-practice
  2. Open it in VSCode
  3. Open the terminal:
View > Terminal
or Ctrl + ` (Windows/Linux)
    Cmd + ` (Mac)
  1. Initialize Git:
git init
  1. Create and commit a file:
echo "Hello from main" > main.txt
git add .
git commit -m "main.txt initial commit"

๐Ÿ”€ Create Conflict

  1. Create a branch and switch to it:
git checkout -b feature
  1. Modify main.txt (same line), commit:
echo "Feature change" > main.txt
git add .
git commit -m "update main.txt in feature"
  1. Create another file:
echo "New feature file" > feature-1.txt
git add .
git commit -m "add feature-1.txt"
  1. Switch back to main:
git checkout main
  1. Modify the same line in main.txt differently, commit:
echo "Main branch change" > main.txt
git add .
git commit -m "conflicting change in main"

โš”๏ธ Trigger a Merge Conflict

  1. Merge main into feature:
git checkout feature
git merge main

Git will stop and show a conflict in main.txt.

  1. Open main.txt, you'll see:
<<<<<<< HEAD
Feature change
=======
Main branch change
>>>>>>> main
  1. Pick the correct version or combine both. Then save and exit.

  2. Stage the file:

git add main.txt
  1. Finish the merge:
git commit

๐Ÿงผ Squash the Commits

  1. Switch to main:
git checkout main
  1. Squash merge feature:
git merge --squash feature
git commit -m "squashed feature changes"

๐Ÿ” Practice Rebase

  1. Reset to the commit before merge:
git reset --hard HEAD~1
  1. Switch to feature, then rebase:
git checkout feature
git rebase main

Resolve the conflict as before. Then run:

git add main.txt
git rebase --continue

โœ‚๏ธ Interactive Rebase

Make a few small commits:

echo "line1" >> notes.txt
git add . && git commit -m "add line1"
echo "line2" >> notes.txt
git add . && git commit -m "add line2"

Now run:

git rebase -i HEAD~2

Change pick to squash for the second commit. Save and confirm the new message.


๐Ÿ’ Cherry-Pick a Commit

  1. Switch to feature and make a new change:
echo "unique update" > cherry.txt
git add .
git commit -m "special change to cherry-pick"
  1. Copy the commit hash:
git log --oneline
  1. Switch to cherry-target:
git checkout -b cherry-target
  1. Cherry-pick the commit:
git cherry-pick <hash>

โœ… Summary

  • Conflicts happen when two branches change the same thing.
  • Rebase creates a cleaner history, but merge preserves the original timeline.
  • Squash helps you combine messy commits into one.
  • Cherry-pick lets you copy just one change without merging everything.

With these tools, you're ready to work like a real Git pro โ€” even when things get messy.


Next time you see conflict markers, don't panic โ€” you're ready to fix them like a boss. ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ‘ฉโ€๐Ÿ’ป

GitHub for Beginners: Remotes, SSH, Pull Requests & Collaboration

You've learned how to use Git locally. Now let's take things to the next level โ€” working with GitHub! GitHub is a platform that helps you host your code online, collaborate with others, and track changes as a team.


๐ŸŒ What is GitHub?

GitHub is the world's largest development platform where:

  • You can host your Git repositories
  • Collaborate with teammates or open-source communities
  • Share code, track issues, and submit pull requests (PRs)

๐Ÿ”— Working with Remotes

When you connect your local Git project to GitHub, you set up a remote. GitHub becomes the online version of your project.

Here are the essential Git remote commands:

1. Add a Remote

git remote add origin https://github.com/username/repo.git
  • origin is a nickname for the remote GitHub repo.
  • The URL is the repo's SSH or HTTPS link.

2. Push Your Changes

git push -u origin main
  • Sends your local commits to GitHub.
  • Use -u to set tracking so future pushes don't need branch names.

For feature branches:

git push -u origin feature-name

3. Fetch Updates (No Merge)

git fetch
  • Downloads new data from GitHub but does not change your files.
  • Use when you want to review changes before applying them.

4. Pull Changes (Fetch + Merge)

git pull
  • Updates your branch with the latest changes from GitHub.
  • Combines fetch + merge.

5. Clone a Repo

git clone https://github.com/username/repo.git
  • Makes a local copy of a GitHub repo.
  • Sets up remotes automatically.

๐Ÿ” Setting Up SSH with GitHub

Using SSH is a secure and convenient way to connect to GitHub without typing your password every time.

Steps:

  1. Check for existing SSH keys:
ls -al ~/.ssh
  1. Generate a new SSH key:
ssh-keygen -t ed25519 -C "you@example.com"
  1. Copy the public key:
pbcopy < ~/.ssh/id_ed25519.pub
  1. Go to GitHub:

    • Settings > SSH and GPG keys > New SSH key
    • Paste and save
  2. Test connection:

ssh -T git@github.com

If you get a success message, you're all set!


Optional: SSH Config File

If needed, add a config file:

touch ~/.ssh/config

Example config:

Host github.com
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519

Then run:

ssh-add ~/.ssh/id_ed25519

๐Ÿš€ First GitHub Project

  1. Create a new folder locally and initialize Git:
git init
  1. Add a file and commit:
echo "console.log('hello')" > app.js
git add .
git commit -m "initial commit"
  1. On GitHub, create a new repository.

  2. Connect and push:

git remote add origin git@github.com:username/repo.git
git branch -M main
git push -u origin main
  1. Make more changes locally and push again:
git add .
git commit -m "second commit"
git push

๐Ÿง  Pull Requests (PRs)

Pull Requests let you share changes with others and ask for review.

  1. Create a new branch:
git checkout -b feature-xyz
  1. Make changes and commit.

  2. Push the branch to GitHub:

git push -u origin feature-xyz
  1. Go to GitHub, click "Compare & pull request".

  2. Add a description and create the PR.

Now collaborators can review, suggest changes, or approve it for merging.


๐Ÿงช Practice PR with TICKET-22 Branch

  1. Create a new branch:
git checkout -b TICKET-22
  1. Edit files:
echo "hello people" >> main-01.txt
echo "hello from ticket-22" > hello.js
echo "app is running..." > app.js
  1. Make 2 commits:
git add .
git commit -m "TICKET-22 make changes"
git commit -am "TICKET-22 update app.js"
  1. Merge main into TICKET-22:
git merge main
  1. Push to GitHub:
git push -u origin TICKET-22
  1. Open a pull request.

๐Ÿ’ฌ What If You See "Already up to date"?

That means the branches don't have new changes to merge. Just continue working or open a PR if your branch is ready to merge.


๐Ÿ”„ Understanding Remotes and Remote-Tracking Branches

What is a Remote?

A remote is a version of your project stored online (like GitHub).

What is a Remote-Tracking Branch?

When you fetch, Git updates your local copy of the remote branch (like origin/main). This lets you see what changed without modifying your current branch.


Check All Branches (Local + Remote)

git branch -a

You'll see something like:

main
remotes/origin/main
remotes/origin/TICKET-22

โšก Common Remote Actions

Fetch:

git fetch origin
  • Downloads updates but doesn't merge.

Pull:

git pull origin main
  • Downloads and merges changes into your current branch.

Push:

git push origin feature-name
  • Sends your local commits to GitHub.

โœ๏ธ Interactive Rebase and Force Push

Let's say you want to squash commits in your branch:

git rebase -i HEAD~3

Mark some commits as squash, save, and follow the prompts.

Then push:

git push -f

โš ๏ธ Use force push carefully โ€” it rewrites history and can overwrite others' work if you're sharing the branch.


๐Ÿ’ Cherry-Picking Commits from Another Branch

Cherry-pick lets you copy a single commit from one branch to another.

Steps:

  1. Copy commit hash from git log
  2. Switch to your target branch:
git checkout checkout
  1. Cherry-pick the commit:
git cherry-pick <commit-hash>

If Git can't find the commit:

  • Run:
git fetch
  • Try again

Cherry-pick is useful when you want one feature or fix from another branch without merging the whole thing.


๐Ÿง  GitHub Collaboration Challenge

  1. Create a GitHub repo and clone it in 3 folders: dev1, dev2, dev3

In dev1:

  • Create branch TICKET-11
  • Change app.js, commit
  • Merge main into it
  • Push to GitHub
  • Create PR โ†’ Merge using merge

In dev2:

  • Create branch TICKET-22
  • Make 2 commits to app.js
  • Push to GitHub
  • Create PR โ†’ Merge using merge

In dev3:

  • Create branch TICKET-33
  • Make 3 commits
  • Use interactive rebase to squash
  • Rebase onto main
  • Force push to GitHub
  • Create PR โ†’ Merge using rebase

๐Ÿ’ Advanced Cherry-Pick Practice

  1. Clone the repo into dev4 and dev5

In dev4:

  • Create branch feature
  • Add feature.txt
  • Commit and push

In dev5:

  • Create branch checkout
  • Add checkout.txt
  • Commit

Try to cherry-pick feature.txt commit โ†’ you'll get an error

Fix it with:

git fetch

Then:

git cherry-pick <commit-hash>

Now the commit can be applied.


โœ… Summary

  • Use git remote and push/pull/fetch/clone to connect local and remote repos
  • Use SSH for secure communication with GitHub
  • Use branches + PRs for team collaboration
  • Use rebase to clean up commit history
  • Use cherry-pick to bring over specific changes
  • Use force-push only when you know what you're doing

You're now ready to work with teams, contribute to open source, and push your projects to the world with GitHub! ๐Ÿš€