除非我确定,否则我想阻止自己意外地向主分公司提交一些东西.所以我尝试使用这个脚本来确定我所在的分支但存在问题.当我创建一个新的分支git name-rev返回master时,即使我在另一个分支上
$ git branch
ignore
master
* set_support
$ git name-rev --name-only HEAD
master
Run Code Online (Sandbox Code Playgroud)
这是我的剧本.
#!/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
Run Code Online (Sandbox Code Playgroud)
对于马克:我重建了针对最新稳定标签和相同结果的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
Run Code Online (Sandbox Code Playgroud)
Ada*_*ruk 11
此命令用于查找提交的友好名称.发生的事情是HEAD首先解析为提交的sha1,然后确定名称.我猜它是随意挑选名字的主人,因为它首先git log --decorate
出现在遇到的东西.
我只想解析git branch
你的测试中的输出:
"`git branch | grep \* | cut -f2 -d' '` == "master"
Run Code Online (Sandbox Code Playgroud)
或者更直接的方式是:
$(git symbolic-ref HEAD 2>/dev/null) == "refs/heads/master"
Run Code Online (Sandbox Code Playgroud)
作为替代方案,您可以git rev-parse
按照本答案中的建议使用.所以if表达式将是:
"$(git rev-parse --abbrev-ref HEAD)" == "master"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5938 次 |
最近记录: |