Skip to main content

Command Palette

Search for a command to run...

The mighty Git!

Basic concept of git

Updated
5 min read
The mighty Git!

What Comes to Mind When You Hear "Git"?

When I first heard of GitHub, I thought, "Does this have something to do with tying things up like knots?" Later, after gaining some knowledge, I was surprised to find there actually is a bit of “tying up” involved—but in a technical sense!

Most of us have a vague idea about Git—that it’s a tool where multiple people can work together on the same project. You might think, “Why bother with Git? I could just paste code into a link and send it to my teammate!” But Git is so much more, especially on big projects with dozens of developers. In these projects, people might be writing code in the same function, which brings up questions like, "How do we bring everyone's unique ideas together cohesively?"

It’s a challenge to catch mistakes, fix bugs, and roll out updates seamlessly without accidentally breaking everything. This is where Git shines, helping developers to effectively manage these issues without feeling like they’re bringing in heavy artillery to kill a mosquito.

Git: The Unsung Hero of Team Projects

Git is a Distributed Version Control System (DVCS) that can track changes to code over time, which means you can view, manage, and restore previous versions easily.

Some might wonder, "What’s version control, and what does ‘distributed’ mean here?"

So, What Exactly is Git?

First, Git and GitHub are not the same things. GitHub is an application that uses Git to allow multiple people to work on the same project simultaneously. To understand Git better, let’s start with the basics of version control.

Think back to when you first tackled calculus, and you struggled with one equation after another. You might have erased it all and started from scratch, only to realize the original method was better. With version control, especially for programming, you don’t lose those prior methods or approaches. Version control tools like Git allow you to save all prior versions, making it easy to switch back.

Git stores all changes as separate versions, and developers typically organize these into branches:

  • Master Branch: When you create a repository in Git, the first version, called the "master branch," is created by default.

  • Feature or Topic Branch: Developers often create a new branch to work on a specific task, like a new feature or a bug fix.

  • Develop Branch: Once a feature is finished, it's merged into the develop branch for further integration and testing.

  • Release Branch: When the project is nearly ready, it’s merged into the release branch for final preparations.

With multiple developers working together, having these branches makes it easier to manage, test, and fix issues across versions without affecting each other’s work.

Centralized vs. Distributed Version Control

Version control systems can be either centralized or distributed. Centralized version control works on a client-server approach, where changes are stored on a central server and synced to each connected client. Distributed version control, however, like Git, works on a peer-to-peer approach, where all connected users share data directly with each other. This peer-to-peer method makes Git much faster, as each client’s computer can independently manage changes without relying on a central server.

What Makes Git Special?

In a Git environment, you can track every change to the code, check past versions, and work offline without needing an internet connection to manage versions. But when working on a team project where you need to share code with others, an internet connection and cloud storage become essential. For this purpose, platforms like GitHub, a popular web-based version control system, make it incredibly easy to share and collaborate on code. GitHub offers extensive features for open-source contributions, issue tracking, and team collaboration.

Key Terms to Know in Git

If you’re diving into Git, here are some terms to get familiar with:

  • Repository: A data structure in Git that stores all elements of your project.

  • Clone or Fork: Creating a complete copy of a repository on your computer.

  • Local Repository: The version of the repository on your personal computer.

  • Remote Repository: The shared repository that a team collaborates on.

  • Commit: Creating a version in the local repository.

  • Push: Updating a branch with a new commit.

      # Common Git Commands
    
      1. git init
         - Initializes a new Git repository in the current directory.
    
      2. git clone <repository>
         - Creates a copy of an existing repository by downloading it from a remote location.
    
      3. git status
         - Displays the state of the working directory and the staging area, showing any changes.
    
      4. git add <file/directory>
         - Stages changes to be included in the next commit.
    
      5. git commit -m "message"
         - Records the staged changes in the local repository with a descriptive message.
    
      6. git push
         - Uploads local repository changes to a remote repository.
    
      7. git pull
         - Fetches and integrates remote changes into the current branch.
    
      8. git fetch
         - Retrieves new data from a remote repository without merging it into the current branch.
    
      9. git branch
         - Lists all branches or creates a new branch.
    
      10. git checkout <branch/tag>
          - Switches to a different branch or a specific commit's snapshot.
    
      11. git merge <branch>
          - Integrates changes from one branch into the current branch.
    
      12. git rebase <branch>
          - Reapplies commits on top of another base commit, used to maintain a linear history.
    
      13. git log
          - Shows the commit history for the project, including commit messages and IDs.
    
      14. git diff
          - Displays the differences between commits, branches, or files.
    
      15. git reset <file>
          - Unstages a file from the staging area while retaining changes in the working directory.
    
      16. git rm <file>
          - Removes a file from the working directory and stages the deletion.
    
      17. git stash
          - Temporarily saves changes that are not ready to commit.
    
      18. git tag <tag_name>
          - Adds a tag to the current commit for marking and easy retrieval.
    
      19. git remote
          - Manages the remote connections to other repositories.
    
      20. git config
          - Modifies Git's configuration settings, such as username and email.
    

What’s Next?

Now that you have a basic idea of what Git is and why it’s used, we’ll save the step-by-step on how to use Git for another post. For now, remember that Git is a tool that helps you keep every version of your code intact, even if you change your mind several times along the way. And when you work with others, Git keeps it all tied together—no ropes or knots required!

Happy coding!