1

Sometimes I type accidentaly the wrong branch name, when I do "git pull origin BRANCH".
Is there a way to forbid "git pull origin XXX" when XXX is not the same branch I have currently checked on my local repository? i.e. allow "git pull" only if the local-checked branch is XXX and not YYY.

That is, I do want "merge" to hapen, but only if I pull the same branch I have currently checked in my local repository. Is there a way I can configurate the local repository to allow "git pull" only from the same branch-name?

For example: Case 1:
Current checked branch on local repository is "test"

[* test ] /code$ git pull origin master 

This merges the code from "test"-branch to my local "master" branch. ==> I want to forbid this.

Case 2:

Current checked branch on local repository is "master"

[* master ] /code$ git pull origin master 

This merges the changes from remote "master" branch to my local "master" branch. ==> This is ok.

I hope I explained my problem well.

Thanks in advance.

4 Answers 4

3

You should use git fetch

which does the following:

Fetches all the objects from the remote repository that are not present in the local one.

EDIT:

What is the difference between 'git pull' and 'git fetch'? ("git pull does a git fetch followed by a git merge")

EDIT 2:

AFAIK you can't forbid to pull into a branch since it can be locally name and be whatever whenever.

But you can change the project to the very last commit before the pull by using git pull --abort didn't check it should be something like this.

One more thing you can do is git checkout to the SHA1 of the branch before the you made the git pull.

Remember you can always undo all by (irreversible):

git clean -fd git reset HEAD --hard 
Sign up to request clarification or add additional context in comments.

4 Comments

Don't understand what you want, checkout to the branch master and then pull and it will be merged to the current branch
can you please rewrite your question ? and make it more clear
AFAIK you can't do it, since the local branches are orthogonal to those on the server. but I think there is some hack to do so. I meant you should update your original language so others can help you too, BTW if it can be done someone on SO knows it for sure and will answer your question.
Exactly what I was going to post. It is always safe to run git fetch regardless of your working tree and whatnot.
1

Use git fetch which does not merge the code. git pull is actually a combination of two commands git fetch followed by git merge

Comments

1

Don't worry too much, you can undo the mess by a judicious use of git reset. Just make sure that each time you pull your repository is in shipshape.

2 Comments

@dritan, git reset --hard HEAD^ should do.
yes, but this resets only the last commit. during merge there ara possible more then one commits that are merged. That is, i have to find out, how many commits are merged and then run git reset --hard HEAD~3 # to reset the last 3 commits
1

From 'man git-pull':

 git pull runs git fetch with the given parameters and calls git merge to merge the retrieved branch heads into the current branch. 

Thus,

 git fetch origin master 

does not do an automatic merge.

You can also do:

 git fetch origin master:tmp 

to create a new branch 'tmp' from the fetched master branch.
Then git merge FETCH_HEAD will show you any conflicts.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.