DEV Community

Cover image for GitHub 101: Introduction to Version Control and Collaboration πŸ‚
ShaaN
ShaaN

Posted on

GitHub 101: Introduction to Version Control and Collaboration πŸ‚

Introduction:
What is GitHub πŸ’­?
It’s your gateway to the world of version control and collaborative development! As a beginner in the coding realm, understanding how to effectively manage code, work with others, and track changes is crucial for your growth as a developer. That's where GitHub comes in.

In this comprehensive guide, I will introduce you to the fundamentals of GitHub in a beginner-friendly manner. GitHub, a widely acclaimed platform, simplifies version control and enhances collaboration, enabling developers like you to work seamlessly on projects of any size.

Throughout this tutorial, I will provide you the practical insights to help you navigate GitHub with confidence πŸ”₯

By the end of this journey, you will have gained a solid foundation in using GitHub to manage your code, collaborate with others, and contribute to the wider developer community πŸ«‚

The Big Difference πŸ‹:
Before we begin, let's clarify the difference between GitHub and Git. It's important for beginners to understand this key distinction as we embark on our coding journey:

  • GitHub is a platform that helps developers collaborate on coding projects. It provides a web-based interface to host and manage Git repositories.

  • Git, on the other hand, is a version control system that tracks changes to code over time. It allows developers to work on different versions of their code, create branches, and merge changes.

  • In simple terms, Git is the tool that manages code changes, and GitHub is the online platform that makes it easy for developers to share and collaborate on those code changes with others.

  • Let’s understand that with the help of this Real World Example:

  • Imagine you and your friends are working on a group art project. Git is like a magical notebook that keeps track of all the changes you make to the artwork. It remembers every stroke of the brush or pencil. So, if someone accidentally messes up a part of the artwork, you can easily go back to an earlier version to fix it.

  • GitHub, on the other hand, is like a special gallery where you and your friends can showcase your artwork. It's a place where everyone can store their own copies of the art and share them with others. You can see everyone's contributions, make comments, and work on the artwork together. In this example, Git is the magical notebook that remembers all the changes, and GitHub is the online gallery where you and your friends display and collaborate on your art project.

Git Installation and configuration πŸ”»:
Here's a step-by-step guide to setting up Git.

Step 1: Install Git:

  • Visit the official Git website: https://git-scm.com/downloads
  • Download the appropriate Git installer for your operating system.
  • Run the installer and follow the on-screen instructions.
  • Choose the recommended settings unless you have a specific preference.

Step 2: Configure Git:

  • Open a terminal (Command Prompt on Windows or Terminal on macOS/Linux).
  • Set your name using the following command:

    arduinoCopy code
    git config --global user.name "Your Name"
    
    
  • Set your email address using the following command:

    arduinoCopy code
    git config --global user.email "your@email.com"
    
    
  • Optionally, you can configure other Git settings as well:

    • Set your preferred text editor:

      arduinoCopy code
      git config --global core.editor "your-text-editor"
      
      
    • Enable helpful command line aliases:

      csharpCopy code
      git config --global alias.st status
      git config --global alias.co checkout
      git config --global alias.br branch
      git config --global alias.ci commit
      git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
      
      

Step 3: Create or Clone a Repository:

  • To create a new Git repository, navigate to the desired project directory in the terminal and run:

    csharpCopy code
    git init
    
    
  • To clone an existing repository, obtain the repository URL and run:

    bashCopy code
    git clone <repository-url>
    
    

Step 4: Start Using Git:

  • Add files to the staging area using:

    scssCopy code
    git add <file(s)>
    
    
  • Commit the changes in the staging area with a descriptive message:

    sqlCopy code
    git commit -m "Commit message"
    
    
  • Push your commits to a remote repository (if available):

    perlCopy code
    git push
    
    

Step 5: Additional Commands:

  • Switch between branches:

    phpCopy code
    git checkout <branch-name>
    
    
  • Create a new branch:

    phpCopy code
    git branch <branch-name>
    
    
  • Merge changes from one branch into another:

    phpCopy code
    git merge <branch-name>
    
    
  • View the commit history:

    bashCopy code
    git log
    
    
  • Check the status of your repository:

    luaCopy code
    git status
    
    

Remember, this is just a basic overview of Git's essential commands. As you become more comfortable with Git, you can explore advanced features like branching strategies, collaboration workflows, and more.

Git Commands πŸ“»:

So, let’s learn about the commands of Git using the Pareto principle:

The Pareto principle, also known as the 80/20 rule, states that roughly 80% of the effects come from 20% of the causes. Applying this principle to the Git commands, you can focus on the most commonly used commands that provide the most value. Here is a list of Git commands categorized based on their significance:

  1. Basic Commands:
- **`git init`**: Initialize a new Git repository.
- **`git clone`**: Clone a repository from a remote source.
- **`git add`**: Add changes to the staging area.
- **`git commit`**: Create a new commit with the changes from the staging area.
- **`git push`**: Push commits to a remote repository.
- **`git pull`**: Fetch changes from a remote repository and merge them into the current branch.
Enter fullscreen mode Exit fullscreen mode
  1. Branching and Merging:
- **`git branch`**: List, create, or delete branches.
- **`git checkout`**: Switch branches or restore files.
- **`git merge`**: Merge changes from one branch to another.
- **`git rebase`**: Reapply commits on top of another base commit.
Enter fullscreen mode Exit fullscreen mode
  1. Inspection and Comparison:
- **`git status`**: Show the status of the working directory and staging area.
- **`git log`**: Show commit history.
- **`git diff`**: Show differences between commits, branches, or files.
Enter fullscreen mode Exit fullscreen mode
  1. Remote Repositories:
- **`git remote`**: Manage remote repositories.
- **`git fetch`**: Download objects and refs from a remote repository.
- **`git remote add`**: Add a remote repository.
- **`git remote remove`**: Remove a remote repository.
Enter fullscreen mode Exit fullscreen mode
  1. Undo and Discard Changes:
- **`git reset`**: Reset the current branch to a specific commit.
- **`git revert`**: Create a new commit that undoes previous commits.
- **`git clean`**: Remove untracked files from the working directory.
Enter fullscreen mode Exit fullscreen mode
  1. Collaboration:
- **`git branch -r`**: List remote branches.
- **`git push -u`**: Set up tracking information for a branch.
- **`git fetch --prune`**: Prune stale remote-tracking branches.
- **`git blame`**: Show who last modified each line of a file.
Enter fullscreen mode Exit fullscreen mode

The Pareto principle serves as a general guideline, but it's essential to explore and understand other commands as needed.

Setting up Git and Github πŸ“¦:

Setting up Git and GitHub involves configuring Git locally on your machine and connecting it to your GitHub account. Here's a step-by-step guide.

Step 1: Create a GitHub Account:

  • Visit the GitHub website: https://github.com
  • Click on "Sign up" and follow the instructions to create your account.

Step 2: Create a New Repository on GitHub:

  • Log in to your GitHub account.
  • Click on the "+" icon in the top-right corner and select "New repository".
  • Give your repository a name, choose the visibility (public or private), and add a brief description if desired.
  • Click "Create repository" to create the new repository on GitHub.

Step 3: Connect Git to GitHub:

  • Open a terminal (Command Prompt on Windows or Terminal on macOS/Linux).
  • Change to the directory of your local Git repository or create a new directory for your project.
  • Initialize Git in the directory using the following command:

    csharpCopy code
    git init
    
    
  • Add the remote repository URL to your local repository using the following command:Replace <repository-url> with the URL of your GitHub repository (e.g., https://github.com/username/repository-name.git).

    csharpCopy code
    git remote add origin <repository-url>
    
    
  • Verify that the remote repository has been added successfully:

    Copy code
    git remote -v
    
    

Step 6: Push Your Local Repository to GitHub:

  • Add the files you want to track and include in the initial commit using:

    scssCopy code
    git add <file(s)>
    
    
  • Commit the changes with a descriptive message:

    sqlCopy code
    git commit -m "Initial commit"
    
    
  • Push the committed changes to the remote repository on GitHub:Note: If you created a branch other than the default "master" branch, replace "master" with your branch name.

    perlCopy code
    git push -u origin master
    
    

That's it! Your local Git repository is now connected to your GitHub repository. You can continue working on your project locally, make commits, and use Git commands like git pull and git push to synchronize your changes with GitHub.

That concludes the introduction and setup of Git. It's important to note that while this provides a solid foundation, there is always more to learn and explore. I have provided you with relevant information, essential commands, and the necessary procedures to get started with Git.

Therefore, I am delighted to offer you a comprehensive Git cheatsheet and some valuable video courses for reference. These resources will serve as invaluable tools to enhance your practical knowledge and understanding of Git. Utilize them to delve deeper into the intricacies of Git and unlock its full potential.

Resources:
Books:
πŸ‘‰πŸΌ "Pro Git" by Scott Chacon and Ben Straub
It is widely regarded as one of the most comprehensive and authoritative resources on Git. The book is freely available online.

Link:
Git - Book

Video Courses:
Git Tutorial for Beginners: Learn Git in 1 Hour by Programming with Mosh

Git Tutorial for Beginners - Git & GitHub Fundamentals In Depth By Tech with Tim

Conclusion:
In a nutshell, Git and GitHub revolutionize code management and collaboration. With Git, track changes effortlessly, while GitHub provides a user-friendly platform for seamless teamwork.

Embrace the coding world, keep learning, and embark on a rewarding career journey.

Happy codingπŸ‘πŸΌ!

Thank You

Top comments (0)