Alexander Mills
Alexander Mills

Reputation: 100210

Configure Git to show warning on pushing to different branch than current branch

Is there a way to configure Git/Github to prompt with a warning message upon this scenario:

$> git branch
$> *my_special_branch
$> git commit -am "I am about to make a grave error"
$> git push origin master

basically I want to prevent myself and my team from pushing to a different remote branch than the current one I am on without a warning or confirmation prompt...

so basically I want to prevent pushing to the remote master branch from any branch but a local master branch...

Upvotes: 4

Views: 1166

Answers (3)

Alexander Mills
Alexander Mills

Reputation: 100210

I think the best way to do this is like so:

Default behavior of "git push" without a branch specified

you can configure Git to have some default behavior

for example, if you configure Git like so:

git config --global push.default current

you can omit the branchname and it will default to pushing the current local branch to the remote branch with the same name.

so, if you run this command, omitting the "refspec"/branchname

$> git push origin

then it will only push the current branch to the remote/origin branch with the same name

it saves you from typing long branch names, and serves as bit of a safety

Upvotes: 1

Abhijeet Kamble
Abhijeet Kamble

Reputation: 3201

As per my understanding, you want a user permissions for specific branches.

There are various tools(Repository Management tools) present which provide this functionality.

with the help of the tools you can give branch level permission's to users. As Master branch is always deployment ready so only lead will have access and no one else can merge with master branch. Same way developers will have access to create pull request( for code review and a request for merging with other branch) and lead will review it and once lead is convinced with the functionality he approves and merges the code with the master branch.

Choosing Repository management tool require if the repository is present on-premise or it is on cloud. if on-premise my recommendation is to use Stash else you can go with bit bucket or github for cloud.

Upvotes: 1

ifma
ifma

Reputation: 3818

I don't think there is a way to do what you are asking Git to do. Since you and your team-members have push access to the repository, you are free to push to any other branch you want, master or otherwise.

Well what you could do is to commit something to master while you are on a different branch, and in that way you can't accidentally push because the master is ahead by a commit.

Another thing is to just have push access for yourself, and then your team-members will have to do a pull request if they want to commit something to master, but this isn't ideal I'm sure.

Interesting question though, I will save it for later in case someone comes along with a concrete answer. Sorry couldn't be of more help than this.

Upvotes: 2

Related Questions