Version control is non-negotiable in modern software development. **Git** is the industry-standard tool for tracking changes in your code, and **GitHub** is the most popular platform for hosting your Git repositories and collaborating with others. Understanding how to use them effectively is a fundamental skill that separates junior developers from seasoned professionals.
This guide goes beyond a simple list of commands. It teaches you the **workflow**—the complete cycle of creating features, sharing your work, and integrating it into a larger project. We'll cover everything from your first local commit to creating a collaborative Pull Request.
Prerequisites
All you need to follow along is Git installed on your machine. If you don't have it, download it from the official Git website.
Part 1: The Local Workflow (Working Solo)
Step 1: Configuration and Initialization
The very first time you use Git, you should tell it who you are. This information is attached to every commit you make.
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
Now, navigate to your project folder and initialize a Git repository:
cd my-awesome-project
git init
This creates a hidden `.git` folder where Git will store all its tracking information.
Step 2: The Core Cycle - Add, Commit, and Check Status
This is the fundamental loop of working with Git locally.
- Make changes to your files (write code, add images, etc.).
- Stage your changes using `git add`. The "staging area" is an intermediate step where you prepare the changes you want to include in your next snapshot (commit).
- Commit your changes using `git commit`. A commit is a permanent snapshot of your staged changes. Writing clear, descriptive commit messages is a critical skill.
# Stage a specific file
git add filename.js
# Or, stage all modified files in the current directory
git add .
git commit -m "feat: Add user login functionality"
At any point, you can use `git status` to see the current state of your files—what's modified, what's staged, and what's not being tracked. It's your best friend for understanding what's happening.
To see the history of your commits, use `git log`.
Part 2: The Collaborative Workflow (with GitHub)
Step 3: Connecting to a Remote Repository
First, go to GitHub and create a new, empty repository. Once created, GitHub will provide you with a URL. Now, connect your local repository to this remote one.
# The 'origin' is a conventional alias for your main remote URL
git remote add origin https://github.com/your-username/your-repo.git
Now, "push" your local commits to the remote repository on GitHub.
# The -u flag sets the upstream branch for future pushes
git push -u origin main
Step 4: The Golden Rule - Branching
To work on a new feature or bug fix, you should **never** work directly on the `main` branch. Always create a new branch. This keeps the `main` branch clean and stable while you work in an isolated environment.
# Create a new branch and switch to it in one command
git checkout -b feature/add-user-profile
Now you are on the `feature/add-user-profile` branch. You can make changes, add, and commit here without affecting `main`.
Step 5: The Pull Request (PR) Workflow
Once your feature is complete on its branch, it's time to propose merging it into `main`. This is done via a **Pull Request** on GitHub.
- Push your feature branch to the remote repository:
- Go to your repository on GitHub. You will see a notification prompting you to create a Pull Request for the branch you just pushed. Click it.
- Write a clear title and description for your PR, explaining what your changes do. This is where you ask teammates for a **code review**.
- Your team can now review your code, leave comments, and suggest changes. This collaborative review process is one of the most powerful features of GitHub.
git push origin feature/add-user-profile
Step 6: Merging and Syncing
Once your Pull Request is approved, a repository maintainer (or you) will click the **"Merge pull request"** button on GitHub. This integrates your feature branch's changes into the `main` branch.
Now, your local `main` branch is out of date. You need to sync it with the remote changes.
# Switch back to your main branch
git checkout main
# "Pull" the latest changes from the remote 'main' branch
git pull origin main
`git pull` is a combination of two commands: `git fetch` (which downloads remote changes but doesn't apply them) and `git merge` (which merges the downloaded changes into your current branch).
Finally, it's good practice to delete your now-merged feature branch, both locally and remotely.
# Delete the local branch
git branch -d feature/add-user-profile
# Delete the remote branch
git push origin --delete feature/add-user-profile
Conclusion
You now understand the full development lifecycle with Git and GitHub! You can work on features in isolated branches, propose changes for review through Pull Requests, and keep your local project in sync with a collaborative remote repository. This workflow prevents chaos, improves code quality, and is the standard for development teams everywhere. The key is practice, so start using this workflow for all your projects, big and small.