Guy Wood
Guy Wood

Reputation: 307

Tidy local git branches (best practice)

How to tidy local git branches after remote repo merge (approved pull request)

New to Git for a script project I have. I want to know how to tidy my local branches after I've pushed branch feature1 which has been pull requested and approved (thus merged into master) on the remote repo.

git checkout -b feature1

make changes....

git add *
git commit -m 'feature1 changes'
git push origin feature1

A pull request is then created, approved, merged into master and then deleted. However, on my local repo I still have the feature1 branch which differs from my local master. What is the best way to get back to a local master that is identical to remote? What is the best way to do this?

I realise working this way itself isn't necessarily best practice but at this stage its theoretical for when I have dev, test branches etc.

Upvotes: 0

Views: 538

Answers (3)

spidyx
spidyx

Reputation: 1117

When I'm working on Azure Devops projects, I use git fetch --prune to purge deleted branches on remotes. Then I use git branch -d on deleted branch the previous command has listed.

Note : if you squash when you complete a pull request, you will have to use git branch -D

Upvotes: 3

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41595

Use this command:

git remote prune origin

Now your local branch feature1 will be deleted because the branch doesn't exist in the remote.

Upvotes: 0

padawin
padawin

Reputation: 4476

Once a branch is merged, there is no need to keep it, so you can delete it as follow:

git branch -d branchName # to delete the local branch
git push origin :branchName # to delete the branch on remote "origin"

Upvotes: 0

Related Questions