4

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...

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • Not with the four-word form, no: but note that with this form, you're pushing your master to origin's master, and you're not pushing `my_special_branch` at all. – torek Jul 23 '15 at 02:25
  • my experience dictates otherwise. git push origin master seems to push the current local branch to the remote master branch – Alexander Mills Jul 23 '15 at 02:43
  • The comment 2 stories above is false, you will push to master whatever branch you are on like the OP said. But if you do git push origin my_special_branch, then yes you are pushing my_special_branch to a new remote branch. – ifma Jul 23 '15 at 03:59
  • @ItoA: see https://www.kernel.org/pub/software/scm/git/docs/git-push.html and note that this four-word form, `git push origin master`, specifies both a repository (`origin`) and a refspec (`master`). Then see the 3rd paragraph under the `` option: a missing `:` means push to the configured destination, or by default the same name, i.e., `master`. (It's true that if you have changed some configuration parameters, you can override this, but in general it would push your `master` to the remote's `master`.) – torek Jul 23 '15 at 07:27

3 Answers3

2

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.

ifma
  • 3,673
  • 4
  • 26
  • 38
1

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.

Abhijeet Kamble
  • 3,131
  • 2
  • 30
  • 36
1

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

Community
  • 1
  • 1
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817