Git & GitHub

 It seems like you're working on a coding project and you've made a mistake that breaks everything. Your boss would most likely fire you if you were even able to get a job in the first place. Without Git, you'd have no easy way to go back and undo the changes—you'd be toast. Git is the industry standard; most companies, teams, and open-source projects use Git, so naturally, every job description mentions it. Learning Git isn't just a nice-to-have; it's your "get good or get out" moment. It's a must for any serious developer wanting to land the job.


Welcome to a quick, no-nonsense Git and GitHub Info. Unlike a typical Git Info that only scratches the surface and leaves you pushing straight to production like many other interns, we go beyond the basics and dive into the real stuff no one really talks about, like how to fix a broken production on a Friday.  You'll not only know how to track code changes and collaborate with your team but also professionally resolve merge conflicts, fix real-life production issues, write clean commits so your team doesn't have to question your life choices, recover from major mistakes with reset, revert, and checkout, and even master advanced Git tricks like cherry-picking and stashing.


What is Git and Why is it So Popular?


Git is a distributed version control system. The "version control" part helps you track and manage code changes over time, while "distributed" means that every developer's computer has a complete copy of the codebase, including its entire history of changes and information about who changed what and when. This allows you to blame someone (hopefully, people won't blame you).


Can You Code Without Git?


Of course, you can, but your workflow would look something like this: You start coding your project in a folder named "my_project," and as you make progress, you worry about losing your work, so you create copies like "my_project_v1," "my_project_v2," "my_project_v3," and so on. Then your colleague asks you for the latest version, so you zip up "my_project_v3" and email it over. They make some changes and send it back as "my_project_v3_johns_changes.zip." Meanwhile, you've continued to work, so now you have "my_project_v4." You then need to manually compare John's changes with your V4 and create a V5 incorporating everyone's work. A week later, you realize you accidentally removed a crucial feature in V2, so you dig through your old folders trying to figure out what changed between versions. Now imagine doing this with 10 developers, each working on different features. It's a recipe for chaos, lost work, and countless hours wasted on version management instead of actual coding.


Git solves all of these problems and more. It tracks every change automatically, allows multiple people to work on the same project seamlessly, and lets you easily navigate through your project's history. No more "final_version_v2_final_really_final.zip" files. Git does all of this for you in a much more powerful and organized way.


Getting Started with Git


To get started, you need Git installed on your system. Whether you're on Windows, Mac, or Linux, it's just a few clicks away. Once Git is installed, open up your terminal and run git --version to check if it's installed properly. Next, configure Git to work with your name and email using the following commands:


bash

Copy

git config --global user.name "Your Name"

git config --global user.email "your.email@example.com"

Creating a Repository

A repository (or repo) is where Git tracks everything in your project. Think of it like a folder that stores all the versions of your code. To create a new repository, navigate to your project folder in the terminal and run:


bash

Copy

git init

This initializes an empty Git repository in your folder. You won't see any changes yet, but a hidden .git folder has been created inside your directory. This folder contains all the Git-related data, such as commit history, branches, and more.


Making Your First Commit


Now that you have a repository, let's add some files and track changes. Create a new file called index.html and add some code to it. Then, run git status to see the status of your repository. Git will tell you that there are untracked files. To track a file, use the git add command:


bash

Copy

git add index.html

After adding a file, you need to commit it. Committing in Git is like taking a snapshot of your project at a certain point. To commit, use the git commit command:


bash

Copy

git commit -m "Add index.html file"

Congratulations! You just created your first commit.


Branching and Merging


Branches in Git allow you to create different versions of your project. Think of it as making a copy of your project at a specific moment in time. Whatever changes you make in this copied version won't affect the original project. The main project or branch stays untouched while you experiment, modify, or add new features in the copied branch. If everything works out, you can later merge your changes back into the original project. If not, no worries—the original remains safe and unchanged.


To create a new branch, use the git branch command:


bash

Copy

git branch new-feature

To switch to this new branch, use the git checkout command:


bash

Copy

git checkout new-feature

You can also create and switch to a new branch in one command:


bash

Copy

git checkout -b new-feature

Resolving Merge Conflicts

Sometimes, when two or more developers edit the same lines of code, Git gets confused. This is called a merge conflict. When this happens, Git will ask you to manually choose which changes to keep. To resolve a merge conflict, you need to:

Check out to the main branch and pull the latest changes.

Check out to your branch and merge the main branch into your branch.

Manually resolve the conflicts in the affected files.

Add the resolved files and commit the changes.


Advanced Git Commands


Git Reset: This command allows you to reset your repository to a previous commit. There are three types of resets: soft, mixed, and hard. A soft reset moves the specified commit in history but keeps changes staged. A mixed reset (the default) moves to the specified commit and unstages changes but keeps them in the working directory. A hard reset moves to the specified commit and discards all changes in the working directory and staging area.


Git Revert: This command allows you to undo the effects of a previous commit without losing the commit history. It creates a new commit that reverses the changes made by the specified commit.


Git Stash: This command allows you to temporarily save your uncommitted changes so you can switch branches or work on something else. You can later apply the stashed changes back to your working directory.


Using Git with a GUI


While the command line is powerful, using a graphical user interface (GUI) can make Git more intuitive and user-friendly. Many IDEs, like WebStorm, offer built-in Git integration that allows you to perform Git operations through simple buttons, panels, and menus. This can save you time and make it easier to manage your repositories.


Conclusion


Git is an essential tool for any developer. It allows you to track changes, collaborate with your team, and recover from mistakes. By mastering Git, you'll become your company's go-to Git person—the one everyone turns to when things go south. With this info, you've learned the basics and some advanced Git techniques that will help you in your day-to-day work. Keep practicing, and soon Git will become second nature.



0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post