Search code examples
gitgit-fetch

git branch contains other unwanted branch


The admin has told me not to merge develop into my branches before pushing them to remote, and that I should create new branches based on master, that way no code from develop will accidentally get merged into master. OK, that has worked fine for several weeks, but a couple of days ago I accidentally merged develop into one of my branches. That didn't create a big problem for the admin, he just told me not to do it again, but since then I started using "git branch --contains" to make sure.

Problem: When I fetch the remote master and run "git branch --contains", it shows that develop is part of it:

git branch --contains
  develop
* master

How can that be possible??

I see 3 possible explanations:

  1. remote master was at some point accidentally merged with develop
  2. some unknown error
  3. I'm not updating my local branch correctly with the fetched master

  1. is very unlikely since the admin is a guru who couldn't possibly have let that happen
  2. is what I suspect is happening
  3. possible, but I think I've been doing it correctly all these weeks with this code:

git fetch
git checkout master
git reset --hard origin/master
git clean -f
git pull origin master

So as long as develop is in master I cannot create/push any new branches.

Any idea what's going on?


Solution

  • The problem was my understanding of git branch --contains. It doesn't list branches contained in the currently checked out branch, but branches which contain it.

    So to know if master contains develop I should do

    git checkout develop
    git branch --contains
    * develop
    

    So there was no problem after all. As this shows, master is not part of the list of branches which contain develop.

    While this showed that develop did contain master, but that's ok.

    git checkout master
    git branch --contains
      develop
    * master