{"thread":{"id":"41907","subject":"Bug Report","startedAt":"2016-04-03T00:25:17Z","lastAt":"2016-04-03T02:22:30Z","messageCount":3,"participants":["Benjamin Sandeen","Eric N. Vander Weele","Jacob Keller"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"282541","messageId":"04bf01d18d3f$4ce38410$e6aa8c30$@u.northwestern.edu","threadId":"41907","inReplyTo":null,"subject":"Bug Report","fromName":"Benjamin Sandeen","fromEmail":"benjaminsandeen2016@u.northwestern.edu","sentAt":"2016-04-03T00:25:17Z","receivedAt":"2016-04-03T00:25:17Z","isPatch":false,"sender":{"key":"benjaminsandeen2016@u.northwestern.edu","avatar":null},"body":"Today, I managed to create a duplicate branch in a git repository.  While\nthis may not be a bug per se, I do think that it is confusing and some way\nof preventing such issues in the future may be helpful.\n\nI first cloned the repository:\n\n$ git clone https://github.com/CodeForChicago/superclass.git\n\nThen, I created a new branch (or so I thought):\n\n$ git checkout -b lesson_page\n\nHowever, this branch has already existed for about 4 weeks, without my\nknowledge.  I proceeded to do some work on the files it contained, and when\nit came time to commit and push, and when I pushed, I got the following\nmessage:\n\nTo https://github.com/CodeForChicago/superclass.git\n ! [rejected]        lesson_page -> lesson_page (non-fast-forward)\nerror: failed to push some refs to '\nhttps://github.com/CodeForChicago/superclass.git'\nhint: Updates were rejected because the tip of your current branch is behind\nhint: its remote counterpart. Integrate the remote changes (e.g.\nhint: 'git pull ...') before pushing again.\nhint: See the 'Note about fast-forwards' in 'git push --help' for details.\n\nGiven that I had believed that I had created the branch just a few hours\nprior and was the first to attempt to push to it, this error was\nconsternating.\n\nI may be wrong (I am aware that my understanding of git is limited), but I\nbelieve that the git checkout -b command is simply supposed to create a new\nbranch and then switch to it (I'm not aware of any subtle behavior that goes\non behind the scenes if the \"new\" branch that the user is attempting to\ncreate already exists).  This is why I said it \"may not be a bug per se\".\nHowever, I expect most people who use git to expect this command to create a\nnew branch and then switch to it (this is what most sources online will tell\nusers to do to create a new branch), and as such, it would be extremely\nbeneficial if git were to, at the very least, alert the user to the conflict\nin some way or another.\n\nThanks,\nBen\n\nLead Consultant, Northwestern University Information Technology\nResearch Assistant, Center for Interdisciplinary Exploration and Research in\nAstrophysics at Northwestern University\nPhsyics, Weinberg College of Arts and Sciences\nComputer Science, Weinberg College of Arts and Sciences\nClassics, Weinberg College of Arts and Sciences\n"},{"id":"282543","messageId":"CAGhVqYthSWCqXW2=3dSUweZ2hqTRSHJiHGNWsmQxUxtmAbnzYw@mail.gmail.com","threadId":"41907","inReplyTo":"04bf01d18d3f$4ce38410$e6aa8c30$@u.northwestern.edu","subject":"Re: Bug Report","fromName":"Eric N. Vander Weele","fromEmail":"ericvw@gmail.com","sentAt":"2016-04-03T02:20:55Z","receivedAt":"2016-04-03T02:20:55Z","isPatch":false,"sender":{"key":"ericvw@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1284017?v=4"},"body":"On Sat, Apr 2, 2016 at 8:25 PM, Benjamin Sandeen\n<benjaminsandeen2016@u.northwestern.edu> wrote:\n> Today, I managed to create a duplicate branch in a git repository.  While\n> this may not be a bug per se, I do think that it is confusing and some way\n> of preventing such issues in the future may be helpful.\n\nThis can be confusing.  I'll hopefully try to help and explain below.\n\n> I first cloned the repository:\n>\n> $ git clone https://github.com/CodeForChicago/superclass.git\n>\n> Then, I created a new branch (or so I thought):\n>\n> $ git checkout -b lesson_page\n\nAt this point, you created a *local* branch called 'lesson_page' which\npoints to the current HEAD and then switched to that branch .  This\nlocal branch is independent of the remote branch called\n'origin/lesson_page'.\n\n> However, this branch has already existed for about 4 weeks, without my\n> knowledge.  I proceeded to do some work on the files it contained, and when\n> it came time to commit and push, and when I pushed, I got the following\n> message:\n>\n> To https://github.com/CodeForChicago/superclass.git\n>  ! [rejected]        lesson_page -> lesson_page (non-fast-forward)\n> error: failed to push some refs to '\n> https://github.com/CodeForChicago/superclass.git'\n> hint: Updates were rejected because the tip of your current branch is behind\n> hint: its remote counterpart. Integrate the remote changes (e.g.\n> hint: 'git pull ...') before pushing again.\n> hint: See the 'Note about fast-forwards' in 'git push --help' for details.\n>\n> Given that I had believed that I had created the branch just a few hours\n> prior and was the first to attempt to push to it, this error was\n> consternating.\n\nThe non-fast-forward push is preventing history being rewritten- this\nis a good thing :).\n\n> I may be wrong (I am aware that my understanding of git is limited), but I\n> believe that the git checkout -b command is simply supposed to create a new\n> branch and then switch to it (I'm not aware of any subtle behavior that goes\n> on behind the scenes if the \"new\" branch that the user is attempting to\n> create already exists).  This is why I said it \"may not be a bug per se\".\n> However, I expect most people who use git to expect this command to create a\n> new branch and then switch to it (this is what most sources online will tell\n> users to do to create a new branch), and as such, it would be extremely\n> beneficial if git were to, at the very least, alert the user to the conflict\n> in some way or another.\n\nThe `git checkout -b` command is working as expected.  `git checkout\n-b <name>` is equivalent to `git branch <name> && git checkout\n<name>`.  If you were to execute `git checkout lesson_page`, you would\nget the desired behavior you were expecting because in the presence of\none remote, git will actually execute `git checkout -b lesson_page\n--track origin/lesson_page` - more details can be found in `git help\ncheckout`.  Effectively, it checkouts 'origin/lesson_page' as a new\nlocal branch named 'lesson_page'.\n\nHowever, you indicated that you did not know there was a remote\nbranch already named 'lesson_page'.  After cloning the repository, you\ncan use `git branch -a` to see all remotes to determine which form of\n`git checkout` to use.\n\n> Thanks,\n> Ben\n>\n> Lead Consultant, Northwestern University Information Technology\n> Research Assistant, Center for Interdisciplinary Exploration and Research in\n> Astrophysics at Northwestern University\n> Phsyics, Weinberg College of Arts and Sciences\n> Computer Science, Weinberg College of Arts and Sciences\n> Classics, Weinberg College of Arts and Sciences\n>\n>\n>\n>\n> --\n> To unsubscribe from this list: send the line \"unsubscribe git\" in\n> the body of a message to majordomo@vger.kernel.org\n> More majordomo info at  http://vger.kernel.org/majordomo-info.html\n"},{"id":"282544","messageId":"CA+P7+xo_MifcB8ry846=j2n7ZRn_80Os7kH1Ns+HMjuQf0N_Qw@mail.gmail.com","threadId":"41907","inReplyTo":"04bf01d18d3f$4ce38410$e6aa8c30$@u.northwestern.edu","subject":"Re: Bug Report","fromName":"Jacob Keller","fromEmail":"jacob.keller@gmail.com","sentAt":"2016-04-03T02:22:30Z","receivedAt":"2016-04-03T02:22:30Z","isPatch":false,"sender":{"key":"jacob.keller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/874719?v=4"},"body":"Hi,\n\nOn Sat, Apr 2, 2016 at 5:25 PM, Benjamin Sandeen\n<benjaminsandeen2016@u.northwestern.edu> wrote:\n> Today, I managed to create a duplicate branch in a git repository.  While\n> this may not be a bug per se, I do think that it is confusing and some way\n> of preventing such issues in the future may be helpful.\n>\n> I first cloned the repository:\n>\n> $ git clone https://github.com/CodeForChicago/superclass.git\n>\n> Then, I created a new branch (or so I thought):\n>\n> $ git checkout -b lesson_page\n>\n> However, this branch has already existed for about 4 weeks, without my\n> knowledge.  I proceeded to do some work on the files it contained, and when\n> it came time to commit and push, and when I pushed, I got the following\n> message:\n\nThe branch existed in the remote repository, but it doesn't exist\nlocally. You never fetched a copy into refs/remotes/origin since you\ndidn't say you were interested, and git will fully allow you to create\nlocal branches with the same name as remote branches.\n\n>\n> To https://github.com/CodeForChicago/superclass.git\n>  ! [rejected]        lesson_page -> lesson_page (non-fast-forward)\n> error: failed to push some refs to '\n> https://github.com/CodeForChicago/superclass.git'\n> hint: Updates were rejected because the tip of your current branch is behind\n> hint: its remote counterpart. Integrate the remote changes (e.g.\n> hint: 'git pull ...') before pushing again.\n> hint: See the 'Note about fast-forwards' in 'git push --help' for details.\n>\n\nWhen you tried to push this branch, it will push into\nrefs/heads/lesson_page on the remote, which already exists. Since it\ncannot perform a fast-forward update, as your local work isn't based\ndirectly on the tip of the remote branch, you either need to merge,\nrebase, or start from scratch.\n\n> Given that I had believed that I had created the branch just a few hours\n> prior and was the first to attempt to push to it, this error was\n> consternating.\n\nLet me try to explain. You created your own local branch, which\nhappened to share the same name as an already existing branch. Had you\nknow this you could have fetched and checked out that branch. You can\nview all branches using \"git branch -a\" or \"git ls-remote\".\n\n>\n> I may be wrong (I am aware that my understanding of git is limited), but I\n> believe that the git checkout -b command is simply supposed to create a new\n> branch and then switch to it (I'm not aware of any subtle behavior that goes\n> on behind the scenes if the \"new\" branch that the user is attempting to\n> create already exists).  This is why I said it \"may not be a bug per se\".\n> However, I expect most people who use git to expect this command to create a\n> new branch and then switch to it (this is what most sources online will tell\n> users to do to create a new branch), and as such, it would be extremely\n> beneficial if git were to, at the very least, alert the user to the conflict\n> in some way or another.\n\ngit checkout -b <branch_name> will create a new branch in your local\ncopy of the repository. Git is distributed. You can do \"git checkout\n--track <branch>\" to attempt to create a local branch which tracks the\nupstream branch, and then git status will provide useful information\nabout the relationship between your local work and the remote work.\n\nIt could maybe be improved to notice that a remote has a branch with\nthe same name. However, git can support multiple remotes, so\ndetermining which remote to care about is difficult.\n\nIn your case you have a couple of options to fix it. I would suggest at least\n\n\"git branch --set-upstream-to=origin/<branch_name>\" so that git status\nwill give you useful information about the branch relationship. Then\nyou can merge or rebase your work into the branch.\n\nThe issue is in understanding how git distributes branches, and how it\ncould handle this. I suspect improvements could be made so that it\nwill attempt to warn you when you create a branch that already exists.\nHowever, often you do this *intending* to make it track the specific\nbranch so I am not sure how much good a warning would do. Just a\nmessage wouldn't really hurt anything, however.\n\nThanks,\nJake\n\n>\n> Thanks,\n> Ben\n>\n"}]}