Home > Blockchain >  Specify a one-direction branch in git
Specify a one-direction branch in git

Time:06-04

Say a repo has a master branch and a junk branch. Is there a way to config git to make sure that my colleagues and I never merge junk into master?

CodePudding user response:

The short answer is no. In particular Git doesn't really merge branches at all; it merges commits. A branch name like junk is just a marker meaning "the latest such commit", that Git keeps up to date for you. But since you can run:

git merge a123456

where that's a hash ID rather than a branch name, if that commit is the latest commit on the junk branch, that will merge that commit ("that branch") into the current branch, even if the current branch is master, and there won't be any indication that this is merging junk into master even though it is doing exactly the same thing.

All that said, you can use various Git hooks, or avoid using the git command itself directly, to attempt to keep yourself from making mistakes. For instance, write your own front-end command—I'll call it mgit here for my git but you can name it git if you like as long as you make it invoke "the real Git" somehow—and make your mgit merge check:

mgit() {
    local subcmd="$1"
    case "$subcmd" in
    merge)
         # if current branch is master and argument is junk, error
         ... write code here ...
         ;;
    ... add additional subcommands here ...
    esac
}

Writing mgit is just a Small Matter of Programming, and now you have a command that does what you want it to do.

(As for the Git hooks, there is as yet none in the right place to stop yourself from making this particular mistake, so don't go that way unless you want to get really involved in the Git project. There's ongoing work to make hooks more usable and friendly and such, and you could get into that.)

  •  Tags:  
  • git
  • Related