将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

How do you get git to always pull from a specific branch?
你怎么把Git总是从一个特定的分支pull?

I'm not a git master, but i have been working with it for some time now, with several different projects.in each project, i always git clone [repository] and from that point, can always git pull, so long as i don't have outstanding changes, of course.

Recently, i had to revert to a previous branch, and did so with git checkout 4f82a29.when i was again ready to pull, i found that i had to set my branch back to master.Now, i can not pull using a straight git pull but instead, have to specifygit pull origin master, which is annoying, and indicates to me that i don't fully understand what is going on.

What has changed which does not allow me to do a straight git pull without specifying origin master, and how to i change it back?

UPDATE :


-bash-3.1$ cat config
[core]
 repositoryformatversion = 0
 filemode = true
 bare = false
 logallrefupdates = true
[branch"master"]
[remote"origin"]
 url = git@github.com:user/project.git
 fetch = refs/heads/*:refs/remotes/origin/*

UPDATE 2 : to be clear, i understand that my original method may have been incorrect, but i need to fix this repo so that i can simply use git pull again.Currently, git pull results in :


-bash-3.1$ git pull
You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me either. Please
name which branch you want to merge on the command line and
try again (e.g. 'git pull ').
See git-pull(1) for details on the refspec.

If you often merge with the same branch, you may want to
configure the following variables in your configuration
file:

 branch.master.remote = 
 branch.master.merge = 
 remote..url = 
 remote..fetch = 

See git-config(1) for details.

I can tell git pull which branch to merge, and it works correctly, but git pull does not work as it did originally before my git checkout.

时间:

Under [branch"master"], try adding the following to the repo's Git config file ( .git/config ) :


[branch"master"]
 remote = origin
 merge = refs/heads/master

This tells Git 2 things :

  1. When you're on the master branch, the default remote is origin.
  2. When using git pull on the master branch, with no remote and branch specified, use the default remote (origin) and merge in the changes from the master branch.

I'm not sure why this setup would've been removed from your configuration, though.you may have to follow the suggestions that other people have posted, too, but this may work (or help at least).

If you don't want to edit the config file by hand, you can use the command-line tool instead :


$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master

If you prefer, you can set these options via the commmand line (instead of editing the config file) like so :


 $ git config branch.master.remote origin
 $ git config branch.master.merge refs/heads/master

Or, if you're like me, and want this to be the default across all of your projects, including those you might work on in the future, then add it as a global config setting :


 $ git config --global branch.master.remote origin
 $ git config --global branch.master.merge refs/heads/master


git branch --set-upstream master origin/master

This will add the following info to your config file:


[branch"master"]
 remote = origin
 merge = refs/heads/master

If you have branch.autosetuprebase = always then it will also add :


 rebase = true

Git pull combines two actions -- fetching new commits from the remote repository in the tracked branches and then merging them into your current branch.

When you checked out a particular commit, you don't have a current branch, you only have HEAD pointing to the last commit you made.so git pull doesn't have all its parameters specified.that's why it didn't work.

Based on your updated info, what you're trying to do is revert your remote repo.if you know the commit that introduced the bug, the easiest way to handle this is with git revert which records a new commit which undoes the specified buggy commit :


$ git checkout master
$ git reflog #to find the SHA1 of buggy commit, say b12345
$ git revert b12345
$ git pull
$ git push

Since it's your server that you are wanting to change, i will assume that you don't need to rewrite history to hide the buggy commit.

If the bug was introduced in a merge commit, then this procedure will not work.see How-to-revert-a-faulty-merge.

I find it hard to remember the exact git config or git branch arguments as in mipadi's and Casey's answers, so i use these 2 commands to add the upstream reference :


git pull origin master
git push -u origin master

This will add the same info to your. git/config, but i find it easier to remember.

Your immediate question of how to make it pull master, you need to do what it says.specify the refspec to pull from in your branch config.


[branch"master"]
 merge = refs/heads/master

There is also a way of configuring Git so, it always pulls and pushes the equivalent remote branch to the branch currently checked out to the working copy.it's called a tracking branch which git ready recommends setting by default.

For the next repository above the present working directory :


git config branch.autosetupmerge true

For all Git repositories, that are not configured otherwise :


git config --global branch.autosetupmerge true

Kind of magic, IMHO but this might help in cases where the specific branch is always the current branch.

When you have branch.autosetupmerge set to true and checkout a branch for the first time, Git will tell you about tracking the corresponding remote branch :


(master)$ git checkout gh-pages
Branch gh-pages set up to track remote branch gh-pages from origin.
Switched to a new branch 'gh-pages'

Git will then push to that corresponding branch automatically :


(gh-pages)$ git push
Counting objects: 8, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 1003 bytes, done.
Total 6 (delta 2), reused 0 (delta 0)
To git@github.com:bigben87/webbit.git
 1bf578c..268fb60 gh-pages -> gh-pages

...