【发布时间】:2011-12-16 22:24:43
【问题描述】:
除非我确定,否则我想阻止自己意外地向主分支提交某些内容。所以我尝试了这个脚本来确定我在哪个分支上但是有一个问题。当我创建一个新分支时,即使我在另一个分支上,git name-rev 也会返回 master
$ git branch
ignore
master
* set_support
$ git name-rev --name-only HEAD
master
这是我的脚本。
#!/bin/sh
# Check to see if we are on master branch. Stop accidental commits
if [ "`git name-rev --name-only HEAD`" == "master" ]
then
if [ -f i_want_to_commit_to_master ]
then
rm i_want_to_commit_to_master
exit 0
else
echo "Cannot commit to master branch Adrian"
echo "Remember to create file 'touch i_want_to_commit_to_master' to commit to master"
fi
exit 1
fi
exit 0
对于 Mark:我根据最新的稳定标签和相同的结果重建了 git。它仅在向新分支提交后才有效。
$ mkdir gittest
$ cd gittest
$ git init
Initialized empty Git repository in /home/adrian/gittest/.git/
$ touch file1
$ git add file1
$ git commit
[master (root-commit) 7c56424] New file
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file1
$ git branch
* master
$ git checkout -b new_branch
Switched to a new branch 'new_branch'
$ git name-rev --name-only HEAD
master
$ git --version
git version 1.7.7.1
$ git branch
master
* new_branch
$ touch file2
$ git add file2
$ git commit
[new_branch 1e038fb] new file
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file2
$ git name-rev --name-only HEAD
new_branch
【问题讨论】:
-
你使用的是什么版本的 git,在哪个操作系统上?
git branch后跟git name-rev HEAD的结果看起来像一个(令人惊讶的)错误,如果你真的准确地复制和粘贴了。 -
我从源代码构建 git - 最后一次构建是 $ git describe v1.7.7-rc3 $ git --version git version 1.7.7-rc3 $ uname -a Linux iceweasel.bluedreamer 2.6.40.3-0 .fc15.x86_64 #1 SMP 2011 年 8 月 16 日星期二 04:10:59 UTC x86_64 x86_64 x86_64 GNU/Linux