Vikas
Vikas

Reputation: 985

Local Branch deleted in github

I have a local branch named mass-communication-rm and I have pushed the code to that branch but due to some issue I deleted that branch from local and now again I created the branch with same name so if I push code to that branch so what will happen will it create a separate branch or the code will be pushed to same branch.

Upvotes: 0

Views: 67

Answers (4)

torek
torek

Reputation: 490038

What matters to Git are the commits.

To really understand what's going on, keep these two ideas separate in your head:

  • Commits have hash IDs, like b7bd9486b055c3f967a870311e704e3bb0654e4f.
  • Branch names like master simply store one commit hash ID.

The true name of any commit is its raw hash ID. If I have commit b7b<blah>, and you have commit b7b<blah> (for the same <blah>), we have the same commit. It does not matter what branch name, or names, we use for it, as long as the ID matches.

When you delete a branch name in your Git, that's pretty much all that happens: you have deleted the name mass-communication-rm, which your Git was using to remember some big ugly hash ID. Let's use the letters MCR to stand in for the hash ID. Since you ran a successful git push earlier, you sent that commit, the one with ID MCR, to another Git repository, and that other Git repository now has it. As long as they still have commit MCR, you can always call up that Git repository and get it back.

Now, when you gave them—the Git on GitHub—commit MCR, you were calling it mass-communication-rm. You probably told them to call it mass-communication-rm. Remember, their Git is just like your Git: it has its own branch names. Just as in your Git, each of their branch names remembers one big ugly hash ID.

Your Git also remembers that their Git exists and has a URL that your Git stores under the remote name origin. Your own Git will remember their branch names under your own Git's origin/* names, which your Git calls remote-tracking names. This means that your Git has a name, origin/mass-communication-rm, that remembers the hash ID of commit MCR ... so your Git also still has a name for this big ugly hash ID!

Let's review:

  • Your Git remembers their Git's URL via the short name origin (which your Git calls a remote).
  • You gave them—the GIt on GitHub, at that URL—commit MCR and told them to keep it under their name mass-communication-rm.
  • So your Git has origin/mass-communication-rm, which remembers the big ugly hash ID for commit MCR.

If you run git branch -r (to list remote-tracking names), or git branch -a (to list both local branch names and remote-tracking names), you will see that your own mass-communication-rm is gone, but origin/mass-communication-rm is still there.1

This, in turn, means you can run:

$ git checkout mass-communication-rm

to re-create your own local mass-communication-rm name based on origin/mass-communication-rm. That is, your Git will create a new local branch name, and stuff into that name, the big ugly hash ID your Git is remembering under the name origin/mass-communication-rm.

You can then add new commits. Adding a new commit to a branch consists of making the new commit (in the usual way) and then having your own Git set your branch name to remember the new, unique, big-ugly-hash-ID that your Git generates for that new commit. The new commit itself remembers the previous hash ID; your own branch name remembers the new ID, and your Git finds the old commit—the one we've been calling MCR here—by searching backwards from the new commit.

For much more on the above, including another role that branch names have in keeping commits "alive", see Think Like (a) Git.


1Note that git branch -r prints origin/mass-communication-rm, but git branch -a prints remotes/origin/mass-communication-rm. The full name is refs/remotes/origin/mass-communication-rm. The full name of your own branch is refs/heads/mass-communication-rm.

Git normally strips off the refs/ and the heads/ or remotes/ parts of these names as they're not very useful. For some unknown reason, git branch -a doesn't strip off the remotes/ part, while git branch -r does.

Upvotes: 0

Hassan-Zahid
Hassan-Zahid

Reputation: 427

Github will try to push changes to the same branch. Whether it will or not it depends. If your new local branch has the exact same code as of online one, Githhub will allow you to push new changes. Otherwise, it will show an error saying

Updates were rejected because the remote contains work that you do not have

Solution: 1

A better solution would be to delete your local branch using:

git branch -D mass-communication-rm

Note: It will delete all code in local branch mass-communication-rm

Now fetch online branch using

git fetch origin mass-communication-rm

Checkout to that branch

git checkout mass-communication-rm

Continue your work...

Solution: 2

Other solution could be to pull the online mass-communication-rm branch. It will merge the online branch into your local one and then try pushing. It will work.

Cheers!

Upvotes: 0

Bhavesh Gupta
Bhavesh Gupta

Reputation: 32

As you said, you deleted that branch from local, not remote. Now rewrite it and try to push, it will be merged to same remote branch.

Upvotes: 0

joker
joker

Reputation: 3752

You don't have to create a new branch with the same name. Just checkout to the branch:

git checkout mass-communication-rm

git is smart enough to create a local branch of mass-communication-rm if it only exists as remote branch. The local branch is created automatically and it will track its correspondent remote branch.

This is the safest way to get back the same branch as you deleted; AFAIK.

I understand this doesn't answer your original question as in the post, but I think this answers the root cause pushed you to write the post.

Upvotes: 1

Related Questions