How to make your local repo track all remote branches in Git

Hey, I'm a postgraduate in Cyber Security with practical experience in Software Engineering and DevOps Operations. The top player on TryHackMe platform, multilingual speaker (Kazakh, Russian, English, Spanish, and Turkish), curios person, bookworm, geek, sports lover, and just a good guy to speak with!
Problem:
In Git you can fetch all branches from all remotes with git fetch --all. But this method has one downside: fetch will not create local branches in your local repository for remote tracked branches. You have to do this manually. Hence, we can automate this process by using git alias and bash scripting.
Prerequisites:
- Configured
git-extras - Configured
git syncviahub
Create global git alias track-all-branches
git config --global alias.track-all-branches '!git fetch -p && for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done; git branch -a'
View created alias:
git-alias
Usage:
git track-all-branches
If remote branch was deleted, you can sync your local repo with
git sync
CAUTION: git synccommand is dangerous, as it will delete your local branch. Use this command after git track-all-branches when you are confident that you no longer need the local branch of tracked remote in your repository.
Example output
The output of git track-all-branches will list all local and remote branches and notify if the new branch was added to track. In this example remote repo has a new branch created extras-4:

Explanation:
git fetch -p- before fetching, removes any remote-tracking references that no longer exist on the remote.git branch -r- lists all remote tracking branchesgit branch --track [remote_branch]- tracks the remote branch





