[英]How can I avoid an accidental dcommit from a local branch
Sometimes, I create local branches in git, and I'd like to get a warning message when I try to dcommit from them. 有时,我在git中创建本地分支,当我尝试从它们中提取时,我想收到一条警告消息。
How can I prevent myself from accidentally dcommiting from a local branch? 如何防止自己意外地从当地分支机构撤职?
An alternative to pre-commit hooks, if you're using Linux (or Git bash or Cygwin or similar), is to wrap git
in a shell helper function. 如果您正在使用Linux(或Git bash或Cygwin或类似的),那么预提交挂钩的替代方法是将git
包装在shell辅助函数中。 Add the below to your ~/.bashrc
(for bash, Git bash) or ~/.zshrc
(for zsh) file, or whatever the equivalent is for your shell: 将以下内容添加到~/.bashrc
(对于bash,Git bash)或~/.zshrc
(对于zsh)文件,或者等效于shell的任何内容:
real_git=$(which git)
function git {
if [[ ($1 == svn) && ($2 == dcommit) ]]
then
curr_branch=$($real_git branch | sed -n 's/\* //p')
if [[ ($curr_branch != master) && ($curr_branch != '(no branch)') ]]
then
echo "Committing from $curr_branch; are you sure? [y/N]"
read resp
if [[ ($resp != y) && ($resp != Y) ]]
then
return 2
fi
fi
fi
$real_git "$@"
}
(I've tested this with bash and zsh on Red Hat, and bash on Cygwin) (我在Red Hat上使用bash和zsh进行了测试,并在Cygwin上进行了bash测试)
Whenever you call git
, you'll now be calling this function rather than the normal binary. 每当你调用git
,你现在都会调用这个函数而不是普通的二进制函数。 The function will run git normally, unless you're calling git svn dcommit
while attached to a branch that's not master. 该函数将正常运行git,除非你在附加到非master的分支时调用git svn dcommit
。 In that case, it'll prompt you to confirm before doing the commit. 在这种情况下,它会在提交之前提示您确认。 You can override the function by specifying the path to git
explicitly (that's what the $real_git
is doing). 您可以通过明确指定git
的路径来覆盖该函数(这就是$real_git
正在做的事情)。
Remember that after updating ~/.bashrc
or equivalent, you'll need to reload it, either by starting a new shell session (logging out and logging in again) or by running source ~/.bashrc
. 请记住,在更新~/.bashrc
或等效文件后,您需要重新加载它,方法是启动一个新的shell会话(注销并再次登录)或运行source ~/.bashrc
。
Edit : As an enhancement, you can remove the first line, starting real_git=
, and replace the other instances of $real_git
with command git
, which achieves the same thing but in the preferred way. 编辑 :作为增强,你可以删除第一行,启动real_git=
,并用command git
替换$real_git
的其他实例,这实现了相同的功能,但是以首选的方式。 I've not updated the script itself as I've not been able to test the change on zsh. 我没有更新脚本本身,因为我无法在zsh上测试更改。
First thing that comes in mind is using a git pre-commit hook to solve the problem. 首先要记住的是使用git pre-commit钩子来解决问题。 This would be easy for pure git repos: 对于纯git repos来说这很容易:
But as discussed in Hooks for git-svn , this isn't fully working. 但正如Hooks中针对git-svn所讨论的那样,这并没有完全奏效。 VonC came up with an (accepted) answer where he utilizes an intermediate bare repo that acts like kind of a proxy between git ans SVN. VonC想出了一个(接受的) 答案 ,他利用一个中间的裸仓,就像git和SVN之间的代理一样。
Maybe this could help you too. 也许这对你也有帮助。
In case anyone else needs this for Windows Powershell: 万一其他人需要这个Windows Powershell:
function CallGit
{
if (($args[0] -eq "svn") -And ($args[1] -eq "dcommit")) {
$curr_branch = &{git branch};
$curr_branch = [regex]::Match($curr_branch, '\* (\w*)').captures.groups[1].value
if ($curr_branch -ne "master") {
Write-Warning "Committing from branch $curr_branch";
$choice = ""
while ($choice -notmatch "[y|n]"){
$choice = read-host "Do you want to continue? (Y/N)"
}
if ($choice -ne "y"){
return
}
}
}
&"git.exe" @args
}
Set-Alias -Name git -Value CallGit -Description "Avoid an accidental git svn dcommit on a local branch"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.