Improve git branch lookup logic