π 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>
Example:
git clone https://github.com/username/project-name.git
π git status
Shows the current state of your working directory.
git status
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>
Add all files:
git add .
β
git commit
Saves a snapshot of your staged changes.
git commit -m "Your commit message"
π git push
Uploads your local commits to the remote repository.
git push
π git init
Initializes a new Git repository in your local directory.
git init
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>
View your remote URLs:
git remote -v
πΏ 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:
- Create a new folder and add your project files.
- Initialize Git:
git init
- Create a new repository on GitHub.
- Link your local repo to GitHub:
git remote add origin https://github.com/your-username/your-repo.git
- Rename the default branch to
main
:
git branch -M main
- Push your first commit:
git add .
git commit -m "Initial commit"
git push -u origin main
From now on, just use:
git push
π± 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
π Switch between branches:
git checkout main
π§Ή Delete a branch (from another branch):
git branch -d feature-branch
π See Differences Between Branches
Compare your current branch with main
:
git diff main
π Merge a Branch
First, switch to the main
branch:
git checkout main
Then merge the feature branch:
git merge feature-branch
π© Pull Requests (PR)
Instead of merging directly from your local machine:
- Push your feature branch to GitHub.
- Create a Pull Request (PR) on GitHub.
- Have someone review and approve it.
- 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
βοΈ 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
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>
Unstage all files:
git reset
Undo the last commit (keep changes):
git reset HEAD~1
Reset to a specific commit:
git log # Find the commit hash
git reset <commit-hash>
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>
β
Safer alternatives:
If you just want to move the branch without losing code:
git reset --soft <commit-has>
Keeps your changes in the staging area
OR,
git reset --mixed <commit-hash>
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.