Lets Play Git - GitHub
Beginner Guide to Git Branching

Engineer @Ciena | Software Engineering | Full Stack Development | Typescript , Java, React Node | DSA LeetCode (600+) | System Design |
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

Commands:
git commitgit commitgit commitAFTER
Git Branching & Merging:
git branch <branchName>: Creates a new branchgit checkout <branchName>: Switches to branchNamegit 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 C1git checkout bugFix This switches the pointer to newBranchgit commit This creates a new Commit C2 below C1 and now the newBranch is pointing to C2git 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 valuesgit checkout branchName^This will move the HEAD pointer to one level above the current pointer.git checkout HEAD~4This 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
-foption. So something like:git branch -f main HEAD~3moves (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 pushedgit revert HEAD
AFTER

Configuring Git
Scenario: Setting up Git for the first time
Open your terminal.
Set your username:
git config --global user.name "Your Name"Set your email:
Set your email:git config --global user.email "your.email@example.com"Verify your configuration:
git config --list
2. Initializing a Repository
Scenario: Starting a new project
Navigate to your project directory:
cd /path/to/your/projectInitialize Git:
git initAdd a
.gitignorefile (optional):echo "node_modules/" > .gitignoreAdd your files to the repository:
git add .Commit your changes:
git commit -m "Initial commit"
3. Cloning a Repository
Scenario: Working on an existing project
Copy the repository URL from GitHub or another platform.
Clone the repository:
git clone <repository-url>Navigate into the project directory





