handlerFive
handlerFive

Reputation: 879

Can't commit files from origin/<name> to origin/master

While pushing some fixes to the master branch I accidentally created a new branch origin/<name> and it got uploaded and git showed branch name was just created. When I execute $ git show-ref

The output is:

57c425c922d25ebb90c0acdb824f103eb5086188 refs/heads/master
57c425c922d25ebb90c0acdb824f103eb5086188 refs/remotes/origin/HEAD
57c425c922d25ebb90c0acdb824f103eb5086188 refs/remotes/origin/master
aeb2652a82d0d95af940d42007e4315621f7e4d1 refs/remotes/origin/ron

My codes didn't merge with origin/master. How can I clean this up? And working with a new local branch and commit all those changes to the master branch in the repository

Upvotes: 1

Views: 97

Answers (1)

torek
torek

Reputation: 489173

There's a terminology issue you should deal with first, because otherwise everything's going to be horribly confusing:

  • A name like master is a branch name.

  • A name like origin/master is a remote-tracking name, or sometimes—well, more typically, really—called a remote-tracking branch name. I don't like this phrase, because these remote-tracking things do not work enough like (local) branch names, and it's too easy to confuse the two.

These both exist inside your own Git repository, but the remote-tracking names exist because of some other Git repository:

  • origin, by itself, is a remote. A remote is just a short name that represents some other Git repository, and stores a URL by which your Git can call up that other Git.

With all of that in mind, there's a sort of secret, which is important, and which git show-ref is showing you: branch names and remote-tracking names both use a more general mechanism. This means they have full names, which are spelled starting with refs/heads/ and with refs/remotes/ respectively.

You can't, with git branch or git checkout -b, create a remote-tracking name. You can only create a branch name. Your Git will create, update, and destroy remote-tracking names based on the other Git's branch names! Remember, each Git is its own Git, so each one has its own branch names. Your remote-tracking names are just your Git's way of remembering their Git's branch names.

Now, you said:

... I accidentally created a new branch origin/<name> ...

but as I noted above, you literally can't do that. You can create a (local) branch named, say, origin/ron, but its full name will be refs/heads/origin/ron.1 Your git show-branch output shows refs/remotes/origin/ron which means you don't have a branch named ron. That means there's nothing to delete!

Now, if you want origin/ron to go away, that's a little trickier. You need to convince the other Git to delete its branch named ron. If you use a web GUI to talk to the other Git, that may have its own clicky buttons for creating and deleting branch names in that Git. Or, you can use your Git, with its git push, to send a request to their Git that they delete their ron, which is pretty straightforward:

git push --delete origin ron

will do that.

Once they don't have a branch named ron—a reference whose full name, in their Git, is refs/heads/ron—you then want to have your Git obtain their latest information, and use that to "prune" any left-over remote-tracking names that you have, using:

git fetch --prune origin

That will have your Git call up their Git and get their list of branches (which now won't include their ron), then update your remote-tracking names based on their branches (which will delete your origin/ron).


1Don't do it! It won't break Git—Git knows, because of the refs/heads/ part, that it's a branch name, not a remote-tracking name—but it's confusing as all heck. If you did do it by accident, use git branch -d to delete it, or git branch -m to rename it (m stands for "move" = rename), so that it does not look like a remote-tracking name.

Upvotes: 2

Related Questions