Mastering Git and GitHub Commands: A Comprehensive Guide

Mastering Git and GitHub Commands: A Comprehensive Guide

·

5 min read

What is version control?

Version control is a system that keeps track of the changes or modifications over time. It can create different versions of the project which helps us to go back and forth as required. It helps multiple developers to work on the same project simultaneously, without interfering with each other's work.

Introduction to Git and GitHub

Git is a version control system that helps developers keep track of their changes, create new branches, merge changes, and keep a complete history of project development.

GitHub is a web-based platform built on top of Git that provides a place for developers to store, share, and collaborate on Git repositories. It offers features like pull requests, issue tracking, and web hosting, making it a popular choice for open-source and collaborative software development.

Commands

Let's go through these Git commands with a simple story to help understand their use

  • git init

    • You've just started writing a novel, and you want to use version control to keep track of your changes. You set up a blank book on your computer.

        git init
      
  • git status

    • As you write your novel, you want to know which parts you've edited and which are still the same. You periodically check the status.

        git status
      
  • git clone

    • A friend wants to collaborate on your novel. You make a copy of your book for them to work on, preserving your original.

        git clone repository_url
      
  • git add

    • You've made changes to a chapter, and you want to add it to the list of things to be saved in your next draft.

        git add chapter1.txt
        git add . # adds all the files that has been modified
      
  • git commit

    • After making several edits and adding chapters, you're happy with your changes. You commit your work, creating a new version of your book.

        git commit -m "Added chapter 2"
      
  • git branch

    • You decide to explore an alternate ending. You create a new branch, like a parallel universe for your novel.

        git branch alternate_ending
      
  • git checkout

    • You switch to the alternate ending branch to work on it without affecting the main storyline.

        git checkout alternate_ending
      
  • git log

    • Curious about the journey, you check the log to see the history of your novel's development, including all the edits and branches.

        git log
      
  • git reset

    • Realizing your alternate ending is a mistake, you want to undo the last few changes without erasing everything. You reset to a specific commit.

        git reset HEAD~2
      
  • git reset is a command used to reset the current branch to a specific commit or point in history.

  • HEAD~2 refers to the commit that is two steps back from the current HEAD (your current position).

  • git stash

    • Your friend calls to discuss a plot hole in your novel. You stash your current changes to focus on the issue, preserving your work for later.

        git stash
      
  • git stash pop

    • After resolving the plot hole, you unstash your changes and continue where you left off.

        git stash pop
      
  • git stash clear

    • Over time, you accumulate too many stashes. You decide to clear them all to avoid clutter.

        git stash clear
      
  • git merge

    • You've finished the alternate ending, and it's time to merge it back into the main storyline, combining the two branches.

        git merge alternate_ending
      

Developers initialize a repository using git init, add changes to a staging area to prepare for commit, and then create permanent snapshots of these changes with git commit. The staging area acts as a filter, allowing developers to selectively include changes in commits. Once satisfied with their work, they can use git push to share these commits with a remote repository, facilitating collaboration and ensuring a synchronized codebase. This iterative process empowers teams to manage and track project development efficiently.

  • git remote -v

    • To verify which remote repositories your local repository is connected to, you check the list of remotes.

        git remote -v
      
  • git pull upstream main

    • You're collaborating with others on a project. To update your local repository with changes from the main project repository (upstream), you use this command.

        git pull upstream main
      
  • git reset --hard commitId

    • You made a mistake in your last few commits and want to completely reset your project to a previous state.

        git reset --hard commitId
      
  • git rebase -i commitId

    • You want to squash several commits into one to make the commit history more concise and organized.

        git rebase -i commitid
      
    • This will open a text editor with a list of your recent commits in your chosen text editor (usually Vim or your default text editor).

    • The interactive rebase file will look something like this:

        pick abc123 Commit message 1
        pick def456 Commit message 2
        pick ghi789 Commit message 3
      
    • To squash the second and third commits into the first one, change the word pick to squash or simply s:

        pick abc123 Commit message 1
        squash def456 Commit message 2
        squash ghi789 Commit message 3
      
    • Save and exit the text editor. In Vim, you can press Esc, type :wq, and hit Enter.

  • Amend the Last Commit

    • You forgot to include a file in your last commit, and you want to add it without creating a new commit.

        git add missed-file.txt 
        followed by 
        git commit --amend
      
  • Revert a Specific Commit

    • You need to undo a specific commit without rewriting history.

        git revert commitid
      
  • Correcting the Most Recent Commit Message

    • You need to fix a typo in the last commit message.

        git commit --amend -m "Corrected typo"
      

In this blog, we've learned a variety of Git and GitHub commands that empower developers to efficiently manage version control, collaborate seamlessly, and address and correct errors in their projects. These essential tools form the backbone of modern software development, enabling teams to work together effectively and maintain the integrity and progress of their codebase."