Git for Noob-to-Pro — #2(Branches)

Search for a command to run...

No comments yet. Be the first to comment.
Bun, the hyper-fast JavaScript runtime known for its incredible speed and Zig-based architecture, is being completely rewritten in Rust. For a project that has over 22 million monthly downloads and ea

Next.js 16.3 is almost here, and it's packed with a ton of improvements and let's see them in this post 1.Instant Navigation For a while now, there’s been a valid, lingering debateServer Components vs

It’s official. React has merged its long-awaited Rust port of the React Compiler (formerly known as React Forget) into the main repository. What started as an experimental research project by Joseph S

If you follow modern technology trends, you have likely been led to believe that artificial intelligence is entirely built on Python. Every tutorial, machine learning framework documentation, and open

Just when the JavaScript ecosystem was catching its breath after last week’s TanStack Router compromise, the Mini Shai-Hulud supply chain worm has continued its relentless march. Developed by the thre

If you don’t have time to read but want to know what’s there in this series. Find the quick read 👇

We are going to see the commonly used Git snippets for the repository which will make you from Noob to Pro.
The Git Repository snippets that we are going to look at in this series are
Local Repository
Branches
Commits
Module
Stash
If you are new to this series check out the Part 1👇
In this post, we are going to see the topic Branches which will have the following sub-topics like
Create a local branch
Delete a local branch
Delete detached branches
Delete remote branch
Get the current branch name
Rename remote branch
Discard uncommitted changes
Merge a branch
View local branches
Delete merged branches
View merged branches
View branches sorted by date

The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off the main using git branch new_branch. Once created you can then use git checkout new_branch it to switch to that branch.
git checkout -b <new-branch>
#Example
git checkout -b develop
git checkout -b unitest
Deletes a local branch.
git branch -d <branch> to delete the specified local <branch>.git branch -d # Delete fully merged branch
git branch -D # Delete branch (even if not merged)
#Example
git checkout main
git branch -d patch-1 # Deletes the `patch-1` local branch
Deletes all detached branches.
Use git fetch --all --prune to garbage collect any detached branches.
This is especially useful if the remote repository is set to automatically delete merged branches.
git fetch --all --prune
#Example
git checkout main
git branch
# main
# patch-1
# patch-2
# Assuming `patch-1` is detached
git fetch --all --prune
git branch
# main
# patch-2
Deletes a remote branch.
git push -d <remote> <branch> to delete the specified remote <branch> on the given <remote>.git push -d
# Examples
git checkout main
git push -d origin patch-1 # Deletes the `patch-1` remote branch
Prints the current branch name.
git rev-parse --abbrev-ref HEAD to print the name of the current branch.git rev-parse --abbrev-ref HEAD
# Examples
git checkout patch-1
git rev-parse --abbrev-ref HEAD # Prints `patch-1`
Renames a branch both locally and on the remote.
Use git branch -m <old-name> <new-name> to rename the local <old-name> branch to <new-name>.
Use git push origin --delete <old-name> to delete the old remote branch.
Use git checkout <new-name> to switch to the renamed branch.
Use git push origin -u <new-name> to set <new-name> as the remote branch for the renamed branch.
git branch -m
git push origin --delete
git checkout
git push origin -u
# Example
git checkout main
git branch -m patch-1 patch-2 # Renamed the local branch to `patch-2`
git push origin --delete patch-1
git checkout patch-2
git push origin -u patch-2 # Renames the remote branch to `patch-2`
Discards all uncommitted changes to the current branch.
git reset --hard HEAD to reset the local directory to match the latest commit and discard all unstaged changes.git reset --hard HEAD
# Example
git reset --hard HEAD
# Discards all unstaged changes
Merges a branch into the current branch.
Use git checkout <target-branch> to switch to the branch into which you want to merge.
Use git merge <source-branch> to merge a branch into the current branch.
git checkout
git merge
# Example
git checkout main
git merge patch-1 # Merges the `patch-1` branch into `main`
Prints a list of all local branches.
Use git branch to display a list of all local branches.
Use arrow keys to navigate, and press Q to exit.
git branch
# Example
git branch
# main
# patch-1
# patch-2
Deletes all local merged branches.
Use git branch --merged <branch> to list all branches merged into <branch>.
Use the pipe operator (|) to pipe the output and grep -v "(^\*|<branch>)" to exclude the current and the target <branch>.
Use the pipe operator (|) to pipe the output and xargs git branch -d to delete all of the found branches.
git branch --merged | grep -v "(^\*|)" | xargs git branch -d
# Example
git checkout main
git branch
# main
# patch-1
# patch-2
# Assuming `patch-1` is merged into main
git branch --merged main | grep -v "(^\*|main)" | xargs git branch -d
git branch
# main
# patch-2
Prints a list of all merged local branches.
Use git branch -a --merged to display a list of all merged local branches.
Use arrow keys to navigate, and press Q to exit.
git branch -a --merged
# Example
git checkout main
git branch -a --merged
# patch-1
# patch-2
Prints a list of all local branches sorted by date.
Use git branch --sort=-committerdate to display a list of all local branches and sort them based on the date of their last commit.
Use arrow keys to navigate, and press Q to exit.
git branch --sort=-committerdate
# Examples
git branch --sort=-committerdate
# main
# patch-1
# patch-2
