Reputation: 3800
I have a master
/develop
branching system that i have come to love, but it comes with a basic rule. No commits are done on develop
or master
, only merges. This is great, but recently i accidentally made some changes/commits on my develop branch and it annoys the tar out of me.
I looked into moving the ownership of those commits from develop
into another already existing branch (We'll call it work
), the one i should have been working on in the first place, but i decided to just let this one go. Instead, i'd like to fix the issue to begin with.. How does one go about locking a branch, so that commit simply doesn't work on it for traditional, normal changes?
Eg, if you made changes on a "locked" branch, you couldn't git add
nor could you git commit -a
. I suppose technically i'm asking to lock staging, but you get the idea. Any thoughts on this? Or would i simply be better off learning git well enough that i know how to fix commit parent issues?
Upvotes: 20
Views: 10853
Reputation: 32873
Copy this:
#!/bin/bash
if [[ `git symbolic-ref HEAD` == "refs/heads/master" ]]
then
echo "You cannot commit in master!"
exit 1
fi
into a file named pre-commit
in .git/hooks/
It will prevent you from commiting into the branch master
. You can easily customize it to add more branches or to personalize the error message and so on.
BTW, a useful trick to know when you made modifications in master
but you want to commit them in somebranch
is to do:
git stash
git checkout somebranch
git stash apply
and then you are in somebranch
with your modifications ready to commit!
Upvotes: 21