说我有这个命令序列:
current_branch="$(git rev-parse --abbrev-ref HEAD)"
git checkout -b "foo"
git lock "$current_branch" # i made this up
我想要做的是锁定一个分支,以便在完成它之后我不会意外地对它进行更改。例如,在功能分支被压扁并合并到集成分支之后。
有没有办法用git做到这一点?也许有一种方法可以使用https://git-scm.com/docs/git-worktree锁定工作树?
我们自己推出这个功能怎么样?让我们从你的git lock
命令开始。我们可以把它写成别名;
$ git config alias.lock "! touch .locks;
git rev-parse --abbrev-ref HEAD | cat - .locks | sort | uniq > .locks.tmp;
mv .locks.tmp .locks;"
每当我们调用git lock
时,我们都会将当前分支添加到.locks
文件中,这是我们独特的锁定分支列表。
然后创建(或编辑).git/hooks/pre-commit
以包括;
#!/bin/sh
if grep -Fxq `git rev-parse --abbrev-ref HEAD` .locks
then
cat <<\EOF
Error: Branch is locked
EOF
exit 1
fi
每当我们承诺确保我们没有提交到锁定的分支时,它将检查.locks
文件。
在你的.gitignore
中添加一个条目来忽略我们新的.locks
文件,你就完成了。
用法示例;
adam@lime ~/git-lock $ git checkout -b MuhBranch
Switched to a new branch 'MuhBranch'
adam@lime ~/git-lock $ git commit -m "Final changes." --allow-empty
[MuhBranch 0304f21] Final changes.
adam@lime ~/git-lock $ git lock
adam@lime ~/git-lock $ git commit -m "Just one more..." --allow-empty
Error: Branch is locked
记得使用.git/hooks/pre-commit
制作你的chmod u+x .git/hooks/pre-commit
可执行文件。
要锁定分支,可以使用git hooks。看看这个SO。
#!/bin/sh # lock the myfeature branch for pushing refname="$1" if [[ $refname == "refs/heads/myfeature" ]] then echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" echo "You cannot push to myfeature! It's locked" echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" exit 1 fi exit 0