Introduction
Imagine You’ve just finished a project for a client. After seeing it, she suggests some changes, and you spend a week tweaking the design and functionality. Excitedly, you present the updated version, only for her to say, “The previous one was more appealing. Can we go back to that?”
Here’s the problem:
You didn’t save the old version! All your changes overwrote the original, and now you’re stuck trying to recreate it from scratch, wasting time and effort. If you had used Git—your life (and sanity) could have been saved with just a click! 😅
What is Git?
Git is a distributed version control system. In simpler terms, it allows you to track changes in your files over time. Imagine you're working on a painting, and you want to be able to go back to previous versions if you don't like the changes you made. That's basically what Git does for your files!
- Track Changes: With Git, every time you make a change and save it, you create a new version of your file. You can easily go back in time to see the evolution of your project.
- Collaborate with Others: Git makes it easy for multiple people to work on the same project simultaneously. Each person can work on their own copy, and when they’re ready, they can share their adjustments.
What is GitHub?
It’s an online platform where you can store your Git-managed code, collaborate with others, and showcase your projects to the world. Together, Git and GitHub make development seamless, organized, and accessible from anywhere, saving you from coding disasters while helping you work smarter and more collaboratively.
- Remote Hosting: Think of GitHub as a cloud storages for all your project files. You can store your code there and access it from anywhere.
- Collaboration Tools: With features like issue tracking and pull requests, GitHub allows teams to communicate and integrate changes smoothly. This means if someone fixes a bug or adds a feature, you can review and decide whether to incorporate those changes into your project or not.
Simple Git Workflow
Here’s how a straightforward Git workflow might look:
-
Write the code on your PC: Start developing your application or project.
⇓
-
Stage your changes using
git add <file name>
: Select the files you want to include in your next commit.⇓
-
Commit your changes using
git commit -m "text"
: Save your changes with a descriptive message so you remember what you did.⇓
-
Push your changes to the remote repo using
git push origin <branch name>
: Upload your changes to the GitHub repository for others to see.⇓
Make a pull request: This is your way of asking others to review your changes and integrate them into the main project.
Simple GitHub Workflow
Here is a streamlined process for managing projects on GitHub:
-
Write the code on GitHub: You can edit files directly in your web browser.
⇓
-
Commit your changes: Each time you make changes, commit them to log what you’ve done.
⇓
Make a pull request: After making changes, propose those updates for review.
Basic Git Commands
Navigating Git is all about knowing a few essential commands:
-
git init
: Initializes a new Git repository in your current directory. Imagine turning a folder into a Git project. -
git add <file name>
: Adds files to the staging area for your next commit. It’s like picking which items to pack for a trip. -
git commit -m "message"
: Captures a snapshot of what you've done, complete with a message that indicates the changes. -
git push origin <branch name>
: Uploads your local commits to a remote repository on GitHub, making your changes accessible to others. -
git diff
: Shows the differences between the current state and the previous commit, helping you review changes. -
git status origin <branch name>
: Displays the current state of your repository, letting you know what’s been staged, what’s not, and if there are any changes. -
git pull origin <branch name>
: Fetches changes from a remote repository and merges them into your current branch, updating your local project. -
git clone <url of repo>
: Creates a local copy of a remote Git repository on your computer, allowing you to work offline. -
git reset
: Moves your current branch to a specific commit or resets your staging area. Perfect for reversing changes.
Uploading a Local Project to GitHub
If you have a project you want to upload, follow these steps:
-
Create a new repository on GitHub: Set up a space to store your project.
⇓
-
Initialize Git in your local project directory using
git init
: Begin tracking your local files.⇓
-
Add files to the staging area using
git add .
: Choose the files you want to include in your upload.⇓
-
Commit the changes using
git commit -m "first project"
: Save your changes locally with a clear message.⇓
-
Link your local repository to the remote repository on GitHub using
git remote add origin main
: Connect your local project to its online counterpart.⇓
Push your local commits to GitHub using
git push origin main
: Upload your changes to your new GitHub repository.
Git Branches
Imagine you have a big drawing that you want to work on, but you're not sure if you want to make changes directly on the main drawing or try out different ideas first. That's where Git branches come in!
- Branching: Think of branches like paths you can take in your project. You create a separate track and experiment without affecting the main project. Once you're happy with your changes, you can merge them back into the main project.
Branch-related Commands
Here are some commands to manage branches:
-
git branch -d <name>
: Deletes the specified branch. -
git branch
: Lists all branches in the repository. -
git checkout -b <name>
: Creates a new branch and switches to it. -
git checkout <name>
: Switches to an existing branch. -
git merge <feature branch name>
: Incorporates changes from one branch into another.
Merge Conflicts
Sometimes, things can get a bit confusing – especially when two collaborators modify the same part of a file. This is known as a merge conflict.
- Resolving Conflicts: Imagine you and a friend both editing the same sentence in a story. Git doesn’t know which version to keep, so it asks you to sort it out. You’ll have to manually choose which changes to maintain. It's a collaborative moment that requires a bit of negotiation and creativity.
Remember, resolving conflicts is a natural part of collaborating and can lead to even better results!
Final Thoughts
While Git and GitHub have many more advanced features to explore, the concepts and commands we've covered here are what you'll use 90% of the time. These fundamentals will serve as a strong foundation for your development journey.
I hope this guide helps you get started with version control and collaborative coding! If you found this helpful, please consider sharing it on social media and following me for more tech content.
👋 This is my first tech blog, and I'm excited to improve it further! If you have any feedback or suggestions, please share them in the comments. Your input will help me create better content going forward.
Connect with me on:
Top comments (0)