Pritam Barhate
Pritam Barhate

Reputation: 775

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"?

To give an example;

Consider that my current local working branch is x and I am issuing:

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)"

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.

Upvotes: 1

Views: 213

Answers (2)

VonC
VonC

Reputation: 1324606

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 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".

So to answer precisely your question: there is no built-in way to get that warning with 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.

Upvotes: 1

automaticAllDramatic
automaticAllDramatic

Reputation: 2063

It is not possible to stop git processing before a pull request and take a user input. I mean, it is possible, but for that you will have to make a few changes to git and build it from source. I am not going down this path.

There is a nifty little trick to use conditional statements within git hooks. You can use Ruby (or any other language) within a git hook to make it conditional. This works best in a pre-commit hook or a hook that is invoked before any actual processing happens. 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. 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.

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.

This is again, just a suggestion, not a solution to your question :(

Upvotes: 2

Related Questions