How to rebase or clean your local branches

Rebasing your branches

To rebase a branch means to update the branch with the latest code from the master branch, so you can be sure that you are never testing an outdated code. This is usually done when you encounter messages like this: “The source branch is 28 commits behind the target branch”.

This process is important because, in some situations, the code from the master branch has too many changes and there might be new bugs that you will not find if testing on an outdated branch.

To do this, you have to open your terminal and enter the following commands. Say, for example, you want to rebase branch XX-1234:

	git checkout master
	git fetch -p origin
	git checkout XX-1234
	git merge origin/master
	git push origin XX-1234

btw, -p is short for prune. After fetching, it removes any remote-tracking branches which no longer exist on the remote.

After fetching, remove any remote-tracking branches which no longer exist on the remote.

This set of commands will update your local master branch, merge the code from the master branch to your local XX-1234 branch, then push the changes to the remote repository.

Cleaning your local branches

Sometimes, if you are not careful, your local repository might get too crowded with local branches, and those will use your available disk space.

To prevent this from happening, you should regularly delete your local branches. To do this, you must open your terminal, navigate to your local repository and enter this command:

	git branch | grep -v "master" | xargs git branch -D

This will delete all your local branches, except the master branch and the branch that you are currently on.

To delete only one branch from the local repository, use this command:

	git branch -d branch_name