Some tips for working with Git can help ease your life

Tram Ho

[Memo] Some Tips and Tricks when working with Git can help ease your life

Below are some problems you usually solve. Chances are everyone has a better solution, can you help me with comments?

Note: This post will be regularly updated every time I remember or have a new problem and find a new solution.

Some issues

Usually in a project, when do you usually create a new branch?
This will be different for each project. But normally we will create a new branch for each feature and sometimes for each task.
This results in a considerable increase in the amount of local branch after a long time
And when $git branch -a we will see all the branches are arranged alphabetically.
In the long run, there are usually 2 problems:

  1. How can I order the branch list by commit time (or by branch creation time)?
  2. How to delete unnecessary branches?
    To delete the branch, we can use $ git branch -D named_branch .
    However, if there are too many branches, the job is maxed back and forth, even though it’s easy

Solution.

1. OrderBy Git branchs by commit time

To order by branch according to the commit time we use the following two commands:

$git branch --sort=-committerdate # desc

Or

$git branch --sort=committerdate # asc

2. Delete multiple branches according to the pattern

To delete multiple branches at once, we should create a rule to name each branch from the beginning.
And when we want to delete, we just need to give patterns of branches to be able to solve them.

$git branch | grep "<pattern>" | xargs git branch -D

Updating …

Thank you everyone for reading here.
If there is a problem and how to solve it, please comment for me as well!

Share the news now

Source : Viblo