Lets Play Git - GitHub

Lets Play Git - GitHub

Beginner Guide to Git Branching

Welcome! Lets play with Git.
This guide covers essential Git operations frequently used by working professionals, by making practical scenarios and step-by-step instructions.

Prerequisites

  • Install Git: Download Git

  • A GitHub account (optional but recommended for remote repositories)

Basics Commands

Lets just familiarize yourself with the git commands and then we will dive into experimentation.

Git Commits:

  • git commit: Its like “Ctrl + s". In complex words this command will create a snapshot of your code written till now.
    SCENARIO: Suppose we have our main branch and we want to make 2 changes in the code and we want to have a snapshot. Here our main branch is pointing to C1.

    BEFORE

    BEFORE

    Commands:

    git commit

    git commit

    git commit

    AFTER


Git Branching & Merging:

  • git branch <branchName> : Creates a new branch

  • git checkout <branchName> : Switches to branchName

  • git merge <branchName> : Merges the code of the branchName to the branch on which you are currently switched on.

    SCENARIO: Suppose you are working on a main branch and made some changes. Then you need to create a new branch and then make some changes there as well. At last you need to merge the work done on both the branches.

    BEFORE

Commands:
git branch bugFix This creates a new branch that is pointing to C1
git checkout bugFix This switches the pointer to newBranch
git commit This creates a new Commit C2 below C1 and now the newBranch is pointing to C2
git checkout main This switches the pointer to main, which is still pointing to C1;
git commit This creates a new Commit C3. and now two parallel branches are created.

AFTER

git merge bugFix This will merge both the branches and create a new Commit C4 pointing to main.

Git Rebase

  • git rebase <branchName> The second way of combining work between branches is rebasing. Rebasing essentially takes a set of commits, "copies" them, and plops them down somewhere else.
    BEFORE

Command:
git rebase main
AFTER

git rebase bugFix


Relative Refs

  • git log: This helps to find the has of particular commits like C1, C2 etc; Example of a hash value- fed2da64c0efc5293610bdd892f82a58e8cbc5d8
    Relative refs are used to move to different commits without using the hash values

  • git checkout branchName^ This will move the HEAD pointer to one level above the current pointer.

  • git checkout HEAD~4 This will move the HEAD pointer to 4 level above the current pointer.

    BEFORE

    Commands:
    git checkout HEAD~4
    AFTER

    You can directly reassign a branch to a commit with the -f option. So something like:

    git branch -f main HEAD~3

    moves (by force) the main branch to three parents behind HEAD.

    BEFORE

    git branch -f main HEAD~3


Git Reset & Revert

git reset HEAD~1: This will switch your HEAD and the branch pointer to one above the current commit i.e. the previous commit.

git revert HEAD: This will create a new copy of previous commit and move the pointer to that commit.

Scenario: You are working on a local branch and you need to reverse your changes to the previous commit C1. Then you need to switch to another branch named “pushed” and revert the changes of pushed branch to C1 preserving the pushed commit.
BEFORE

git reset HEAD~1

git checkout pushed
git revert HEAD
AFTER


Configuring Git

Scenario: Setting up Git for the first time

  1. Open your terminal.

  2. Set your username:

     git config --global user.name "Your Name"
    
  3. Set your email:
    Set your email:

     git config --global user.email "your.email@example.com"
    
  4. Verify your configuration:

     git config --list
    

2. Initializing a Repository

Scenario: Starting a new project

  1. Navigate to your project directory:

     cd /path/to/your/project
    
  2. Initialize Git:

     git init
    
  3. Add a .gitignore file (optional):

     echo "node_modules/" > .gitignore
    
  4. Add your files to the repository:

     git add .
    
  5. Commit your changes:

     git commit -m "Initial commit"
    

3. Cloning a Repository

Scenario: Working on an existing project

  1. Copy the repository URL from GitHub or another platform.

  2. Clone the repository:

     git clone <repository-url>
    
  3. Navigate into the project directory

Thank you for visiting!