1

I've started using git fairly recently and have been really happy with it, it's definitely a great tool.

But today I've got it in a really weird state, that I can't seem to get out of, though.

I have two remote branches, master and pqueues. I want to be able to pull those branches into the locals branches of the same name, but when I do git pull it pulls and merges master into whichever branch I have checked out. If I do git pull on local pqueues, it does a merge of itself and master. I really don't understand why.

Also If I do git push origin pqueues on pqueues it says Updates were rejected because the tip of your current branch is behind when I know I'm the only person to commit to that remote branch, and my local branch is a few commits ahead of remote.

How can I find out what's actually going on and get it back to a working order?

GP89
  • 6,016
  • 3
  • 29
  • 57
  • 5
    Git pull is "git fetch" followed by "git merge" http://stackoverflow.com/questions/292357/whats-the-difference-between-git-pull-and-git-fetch?rq=1 Maybe you want git fetch? – Thilo Sep 14 '12 at 02:06
  • 1
    Also you could add a full git pull command like `git pull master origin/master` or `git pull pueues origin/pqueues` or simply `git pull ` – Adrian Cornish Sep 14 '12 at 02:10

2 Answers2

1

There are two cases where a pull results in a merge. 1. You've made commit(s) on your local branch e.g. 'master' and someone else pushed commit(s) to 'origin/master' since when your local 'master' was last in sync with 'origin/master' 2. You have made no commits to 'master' but someone else has 'rewritten history' by rewinding 'origin/master' to an earlier commit then adding commit(s) to it.

For case (1) if you can perform a "git pull --rebase" which will replay your changes on the new HEAD of origin/master'

Case (2) can be avoided by never rewriting history: if a commit has to be undone, use git revert which will add a new commit that undoes the previous one and the commit and the revert all show in git and can be fast-forwarded by all downstream repos.

karmakaze
  • 30,051
  • 1
  • 28
  • 30
  • That makes a lot of sense, I think I'm in case 1. but when I did `git pull --rebase` it made new branches?? `* [new branch] pqueues -> pqueues/pqueues * [new branch] master -> pqueues/master` – GP89 Sep 14 '12 at 02:30
  • The branch 'pqueues' is a local branch which tracks the remote 'pqueues/pqueues'. When working with a single remote, the convention is to call it 'origin'. Under certain circumstances (e.g. git checkout 'branch-name') a local branch will be created matching the remote origin/branch-name. – karmakaze Sep 14 '12 at 04:53
0

Note: whatever git pull you are using (and git pull --rebase is a good idea to update your current branch on top of what is being pull), git pull will first do a git fetch.

(See "git fetch/git pull from Git Reference)"

And git fetch will fetch the remote branches: if they don't exist in your local repo, will create them.
That is why you see on your first git pull:

* [new branch] pqueues -> pqueues/pqueues 
* [new branch] master -> pqueues/master 

But you will that only for the first git pull. All the other ones will fetch and update the existing remote tracking.
See "Git: What is a tracking branch?" and "What is the difference between a remote tracking branch, and a branch on a remote?", to better understand what git fetch updates.

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • But I already have a local pqueues and master? I created the repo and the remote branch pqueues, and it makes 2 new branches pqueues/queues and pqueues/master if I pull now. It didn't used to do this, it used to pull pueues to my local pqueues and master to my local master (which is what I want, i think). now I have the same commits duplicated in my local tree branching up to pqueues and pqueues/pqueues. I think I've definitely broken something – GP89 Sep 14 '12 at 09:35
  • @GP89 it would be best to clone again your remote repo into a fresh local one, and start from there then. – VonC Sep 14 '12 at 10:27
  • I would but I'd want to push my local changes first, but it currently wont let me push my local pqueues (because its behind the tip.. although no one else has committed to it but me- so I know its not) and I can't seem to pull, as all that does is merge master into my local pqueues. Should I dump all my files onto a new clone and commit that or is there another option? – GP89 Sep 14 '12 at 13:54
  • @GP89 I would first explore the option of dumping your files in a new clone, in order to check if that allows you to move *quickly* forward. – VonC Sep 14 '12 at 14:08