Understanding Git and GitHub: A Comprehensive Guide

date
Jun 2, 2024
slug
Understanding-Git-and-GitHub
status
Published
tags
git
github
Interview
QA
summary
Git tracks code changes. GitHub stores & shares them. This guide unlocks.
type
Post

What is Git?

Git is a version control system that helps developers keep track of code changes, collaborate on projects, and maintain a complete history of every change. Key features include:
  • Tracking changes when files are modified, added, or deleted.
  • Staging changes for commit.
  • Committing changes to create permanent snapshots.
  • Viewing the history of commits.
  • Reverting to previous commits without storing separate copies of every file.

Why Use Git?

Git is widely adopted because it allows developers to:
  • Collaborate from anywhere in the world.
  • See the full project history.
  • Revert to earlier versions of the project.

What is GitHub?

GitHub is a platform that uses Git, offering tools to host and manage code repositories. It is the largest host of source code globally and has been owned by Microsoft since 2018. Over 70% of developers use Git.

Configuring Git for the First Time

  1. Set up your username and email:
    1. bashCopy code $ git config --global user.name "<Enter your username here>" $ git config --global user.email "<Enter your email here>"
  1. Initialize a new Git repository:
    1. bashCopy code $ git init
      Git will start tracking changes in the folder you specified.

Staging and Committing Files

  1. Add files to the staging area:
    1. bashCopy code $ git add <filename with extension>
      To stage all files:
      bashCopy code $ git add --all $ git add -A
  1. Commit staged files:
    1. bashCopy code $ git commit -m "<Enter your message here>"
      For small changes, you can commit directly without staging:
      bashCopy code $ git commit -a -m "<Enter your message here>"

Checking Status and Log

  1. Check the status of files:
    1. bashCopy code $ git status
  1. View commit history:
    1. bashCopy code $ git log
      For a compact view:
      bashCopy code $ git log --oneline

Branching in Git

Branches allow you to work on different parts of a project without affecting the main branch.
  1. Create a new branch:
    1. bashCopy code $ git branch <name of branch>
  1. Switch to a branch:
    1. bashCopy code $ git checkout <branch name>
      To create and switch to a new branch simultaneously:
      bashCopy code $ git checkout -b <branch name>
  1. Delete a branch:
    1. bashCopy code $ git branch -d <branch name>
  1. Merge branches:
    1. bashCopy code $ git merge <branch name>

Working with GitHub

  1. Push local repository to GitHub:
      • Copy the URL of the GitHub repository.
      • Add the remote repository and push changes:
        • bashCopy code $ git remote add origin <paste copied URL here> $ git push --set-upstream origin master
  1. Pull changes from GitHub:
    1. bashCopy code $ git pull origin
  1. Push updates to GitHub:
    1. bashCopy code $ git push origin
  1. Clone a repository from GitHub:
    1. bashCopy code $ git clone <copied URL>

Undoing Changes in Git

  1. Revert to a previous commit:
    1. bashCopy code $ git revert HEAD --no-edit
  1. Reset to a specific commit:
    1. bashCopy code $ git reset <commithash>
  1. Amend the most recent commit:
    1. bashCopy code $ git commit --amend -m "<Commit Message>"

 
If you have any questions, please contact me.