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

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 👇

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👇

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

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!