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.

vonbrand
  • 11,412
  • 8
  • 32
  • 52
dritan
  • 882
  • 2
  • 8
  • 22

4 Answers4

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
Community
  • 1
  • 1
0x90
  • 39,472
  • 36
  • 165
  • 245
  • Don't understand what you want, checkout to the branch master and then pull and it will be merged to the current branch – 0x90 Apr 03 '13 at 12:58
  • can you please rewrite your question ? and make it more clear – 0x90 Apr 03 '13 at 13:48
  • 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. – 0x90 Apr 03 '13 at 14:25
  • Exactly what I was going to post. It is always safe to run `git fetch` regardless of your working tree and whatnot. – TheBuzzSaw Apr 03 '13 at 15:25
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

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
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.

Cometsong
  • 568
  • 9
  • 21
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.

vonbrand
  • 11,412
  • 8
  • 32
  • 52
  • @dritan, `git reset --hard HEAD^` should do. – vonbrand Apr 05 '13 at 11:06
  • 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 – dritan Apr 09 '13 at 10:28