DEV Community

Istiak Islam
Istiak Islam

Posted on

Git & GitHub for Beginners: A Hands-On Guide

πŸš€ Git & GitHub Made Simple

Version control is a superpower for developers. Git and GitHub is the most popular combo to make it happen. In this tutorial, you’ll learn the essential Git commands, how GitHub fits into the picture, and how to manage your code like a pro (even if you're just starting out!).

Let’s dive in! 🌊


πŸ”„ What is Git?

Git is a version control system that helps you track changes in your code over time. It allows you to:

  • Save your progress (commits)
  • Go back to previous versions
  • Work with others on the same project (branches, merges)
  • Resolve code conflicts and maintain a clean history

πŸ™ What is GitHub?

GitHub is a cloud-based platform that hosts your Git repositories. With GitHub, you can:

  • Store your code remotely
  • Share it with collaborators
  • Review, comment, and merge changes
  • Manage projects using issues and pull requests

βœ… Basic Git Commands

Here’s a quick overview of the most essential Git commands:

πŸ”— git clone

Copies a remote repository (e.g., from GitHub) to your local machine.

git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode

Example:

git clone https://github.com/username/project-name.git
Enter fullscreen mode Exit fullscreen mode

πŸ“‹ git status

Shows the current state of your working directory.

git status
Enter fullscreen mode Exit fullscreen mode

You’ll see files that are:

  • untracked – new files not yet staged
  • modified – files that changed but are not staged
  • staged – ready to be committed
  • unmodified – clean and unchanged

βž• git add

Adds files to the staging area (preparing them for commit).

git add <file>
Enter fullscreen mode Exit fullscreen mode

Add all files:

git add .
Enter fullscreen mode Exit fullscreen mode

βœ… git commit

Saves a snapshot of your staged changes.

git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

πŸš€ git push

Uploads your local commits to the remote repository.

git push
Enter fullscreen mode Exit fullscreen mode

πŸ“ git init

Initializes a new Git repository in your local directory.

git init
Enter fullscreen mode Exit fullscreen mode

This sets up a .git folder to track your changes.


🌐 git remote

Links your local repository to a remote one (like GitHub).

git remote add origin <remote-url>
Enter fullscreen mode Exit fullscreen mode

View your remote URLs:

git remote -v
Enter fullscreen mode Exit fullscreen mode

🌿 git branch

Manages branches in your repository. You can create, list, delete, or rename branches.


πŸ› οΈ Create and Connect a Local Repository

Let’s set up a new Git project step-by-step:

  1. Create a new folder and add your project files.
  2. Initialize Git:
git init
Enter fullscreen mode Exit fullscreen mode
  1. Create a new repository on GitHub.
  2. Link your local repo to GitHub:
git remote add origin https://github.com/your-username/your-repo.git
Enter fullscreen mode Exit fullscreen mode
  1. Rename the default branch to main:
git branch -M main
Enter fullscreen mode Exit fullscreen mode
  1. Push your first commit:
git add .
git commit -m "Initial commit"
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

From now on, just use:

git push
Enter fullscreen mode Exit fullscreen mode

🌱 Branching and Merging

Branching allows you (or your team) to work on different features without affecting the main codebase.

πŸ“Œ Create a new branch:

git checkout -b feature-branch
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Switch between branches:

git checkout main
Enter fullscreen mode Exit fullscreen mode

🧹 Delete a branch (from another branch):

git branch -d feature-branch
Enter fullscreen mode Exit fullscreen mode

πŸ” See Differences Between Branches

Compare your current branch with main:

git diff main
Enter fullscreen mode Exit fullscreen mode

πŸ”€ Merge a Branch

First, switch to the main branch:

git checkout main
Enter fullscreen mode Exit fullscreen mode

Then merge the feature branch:

git merge feature-branch
Enter fullscreen mode Exit fullscreen mode

🚩 Pull Requests (PR)

Instead of merging directly from your local machine:

  1. Push your feature branch to GitHub.
  2. Create a Pull Request (PR) on GitHub.
  3. Have someone review and approve it.
  4. Merge the branch into main on GitHub.

πŸ”„ Sync with GitHub

After merging on GitHub, your local code may be outdated. To fetch and merge the latest changes:

git pull origin main
Enter fullscreen mode Exit fullscreen mode

βš”οΈ Merge Conflicts

Conflicts happen when different branches change the same line of code. Git will show it like this:

<<<<<<< HEAD
your code
=======
someone else's code
>>>>>>> feature-branch
Enter fullscreen mode Exit fullscreen mode

You’ll need to manually choose the correct version, fix the file, then commit the change.


πŸ”™ Undoing Changes

Undo a staged file (before committing):

git reset <file>
Enter fullscreen mode Exit fullscreen mode

Unstage all files:

git reset
Enter fullscreen mode Exit fullscreen mode

Undo the last commit (keep changes):

git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode

Reset to a specific commit:

git log  # Find the commit hash
git reset <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Wipe everything and reset (dangerous!)

Including the code in the working tree. The project will look exactly like it did at that commit, as if later changes never happened This is destructive.
⚠️ Once you run it, you cannot recover the deleted commits or changes (unless you've backed them up or they exist in the reflog).

git reset --hard <commit-hash>
Enter fullscreen mode Exit fullscreen mode

βœ… Safer alternatives:
If you just want to move the branch without losing code:

git reset --soft <commit-has>
Enter fullscreen mode Exit fullscreen mode

Keeps your changes in the staging area

OR,

git reset --mixed <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Keeps your changes in the working directory, but unstages them

πŸ” Git Reset Modes β€” Comparison Table

Mode Changes to Commit History Staging Area Cleared Working Directory Cleared What Happens
--soft βœ… Yes ❌ No ❌ No Moves HEAD to the given commit. Keeps all changes staged (ready to commit again).
--mixed βœ… Yes βœ… Yes ❌ No Unstages your changes but keeps them in your working directory. This is the default mode.
--hard βœ… Yes βœ… Yes βœ… Yes Completely deletes all changes after the given commit, including uncommitted work.

πŸ’‘ Pro Tips

  • Use meaningful commit messages.
  • Commit frequently, but in logical chunks.
  • Always pull before pushing.
  • Use .gitignore to exclude unnecessary files.
  • Practice branching, it’s a powerful workflow tool.

πŸŽ“ My Final Thoughts to you

Learning Git and GitHub is like learning a new language. It might seem tricky at first, but with practice, you'll manage your projects confidently and collaborate smoothly.

Start small, explore branches, and don't be afraid to experiment. πŸ’»βœ¨

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

OSZAR »