Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent pulling in git from different than the current branch

Tags:

git

pull

fetch

Currently if you are in branch V2 and you do "git pull origin V3" it merges V3 to V2 and doesn't even warn or prompt about it. Can this option somehow be blocked? I read all the similar questions here and people suggest I can use fetch or show a warning in this case. This is OK, I will start using fetch instead of pull but I want to block other pull users. Also, I assume there are cases when you have to make pull instead of fetch. Thanks

like image 874
walla Avatar asked Dec 03 '13 15:12

walla


1 Answers

If you link the remote branch as the local branch upstream source, you won't need to manually write the remote and branch.

# set upstream
git push -u origin V2

# Then simply pull from default branch
git pull

This'll work. Though, you're mostly always better to fetch and rebase your local branch on top of the remote. This make a cleaner and more usable history.

To answer the question directly though, you could create a custom before merge hook which check the branches being merged and alert when you're merging a remote branch not matching the local branch name. But, this is overhead and might be annoying as it'll check on every merge (because git pull === git fetch && get merge)

like image 77
Simon Boudrias Avatar answered Oct 13 '22 06:10

Simon Boudrias