简体   繁体   English

如果从不同的分支拉出来,git会收到警告

[英]git receive warning if pulling from different branch

How can I make git issue a warning to me if I am on branch "x" and trying to pull from branch "y"? 如果我在分支“x”并尝试从分支“y”拉出来,如何让git向我发出警告?

To give an example; 举个例子;

Consider that my current local working branch is x and I am issuing: 考虑我当前的本地工作分支是x,我发出:

git pull origin y

At this point I want git to issue a warning similar to "You are fetching and merging a different branch into current working branch. Do you want to continue? (y/n)" 此时我希望git发出类似于“你正在获取并将不同的分支合并到当前工作分支中的警告。你想继续吗?(是/否)”

Is this possible? 这可能吗? May be with some config option? 可能有一些配置选项?

I know how to revert this mistake, however I would rather be warned before doing such mistake. 我知道如何恢复这个错误,但我宁愿在做出这样的错误之前得到警告。

It is not possible to stop git processing before a pull request and take a user input. 在拉取请求之前无法停止git处理并接受用户输入。 I mean, it is possible, but for that you will have to make a few changes to git and build it from source. 我的意思是,它是可能的,但为此你必须对git进行一些更改并从源代码构建它。 I am not going down this path. 我不会走这条路。

There is a nifty little trick to use conditional statements within git hooks . git hooks中使用条件语句有一个漂亮的小技巧。 You can use Ruby (or any other language) within a git hook to make it conditional. 您可以在git钩子中使用Ruby(或任何其他语言)使其成为条件。 This works best in a pre-commit hook or a hook that is invoked before any actual processing happens. 这在pre-commit钩子或在任何实际处理发生之前调用的钩子中效果最佳。 In case of a git pull , the only hook you can latch on to is the post-merge hook and that too is invoked only after the entire processing is done. git pull情况下,你可以锁定的唯一钩子是post-merge钩子,并且只有在整个处理完成后才会调用它。 So, there is no point really asking the user if they want to merge another branch after the branch is merged, right :) However, you can show them a message. 因此,没有必要真正询问用户是否要在合并分支后合并另一个分支,正确:)但是,您可以向他们显示消息。

For this, use something like: 为此,请使用以下内容:

#!/usr/bin/env ruby

branch=$(git rev-parse --symbolic --abbrev-ref $1)
if branch != $GIT_DIR/ORIG_HEAD
  puts "You merged a different branch dim wit"
end

Now, what I am trying to do is compare if the merged branch was the same as the original branch name and if not, show the user a message. 现在,我要做的是比较合并的分支是否与原始分支名称相同,如果没有,则向用户显示一条消息。 This will appear as remote on your git merge summary. 这将在您的git合并摘要中显示为remote

Again, am not sure if this will work, cause I am not really sure if git rev-parse --symbolic --abbrev-ref $1 will give you the branch name. 再次,我不确定这是否会起作用,因为我不确定git rev-parse --symbolic --abbrev-ref $1会给你分支名称。

This is again, just a suggestion, not a solution to your question :( 这又是一个建议,而不是你的问题的解决方案:(

If your current branch has been set up to track an upstream branch (as in git branch --set-upstream test origin/test ), you would simply have to : 如果您的当前分支已设置为跟踪上游分支 (如git branch --set-upstream test origin/test ),您只需:

 git pull origin

(avoiding any possibility of a mistake) (避免任何错误的可能性)

Note: depending on your git version, that will also have a consequence on your push operation: see " Difference between git checkout --track origin/branch and git checkout -b branch origin/branch ". 注意:根据您的git版本,这也将对您的推送操作产生影响:请参阅“ git checkout --track origin/branchgit checkout -b branch origin/branch之间的区别

So to answer precisely your question: there is no built-in way to get that warning with git . 所以要准确回答你的问题: 没有内置的方法来通过git获得警告
You could version the hook script recommended in rizwaniqbal 's answer (upvoted), but each user would have to declare that hook in his/her own repo. 您可以对rizwaniqbal回答 (upvoted)中推荐的钩子脚本进行版本控制,但每个用户都必须在他/她自己的回购中声明该钩子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM