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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055956485/fedc849f-c8ae-4686-9757-40e5e9114917.gif align="left")

### On this Series

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

1. Local Repository
    
2. Branches
    
3. Commits
    
4. Module
    
5. Stash
    

If you are new to this series check out the Part 1👇

[![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055958488/6af67ff4-de6d-494d-afec-6723c8c06ec8.png align="left")](https://medium.com/yavar/git-for-noob-to-pro-1-repository-40b0fd31aa34)

### On this Post

In this post, we are going to see the topic Branches which will have the following sub-topics like

1. Create a local branch
    
2. Delete a local branch
    
3. Delete detached branches
    
4. Delete remote branch
    
5. Get the current branch name
    
6. Rename remote branch
    
7. Discard uncommitted changes
    
8. Merge a branch
    
9. View local branches
    
10. Delete merged branches
    
11. View merged branches
    
12. View branches sorted by date
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055960190/f9dfc4cc-c7c1-488b-a3b1-0335c808f3ce.png align="left")

### 1\. Create a local branch

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

### 2\. Delete a local branch

Deletes a local branch.

* Use `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*

### 3\. Delete detached branches

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*

### 4\. Delete remote branch

Deletes a remote branch.

* Use `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*

### 5\. Get the current branch name

Prints the current branch name.

* Use `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\`*

### 6\. Rename remote branch

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\`*

### 7\. Discard uncommitted changes

Discards all uncommitted changes to the current branch.

* Use `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*

### 8\. Merge a branch

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\`*

### 9\. View local branches

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*

### 10\. Delete merged branches

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*

### 11\. View merged branches

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*

### 12\. View branches sorted by date

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*

### Congratulations!

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055962461/2c0225f8-d204-44e1-8e7e-d3979ff43f51.gif align="left")
