Home > git > Deleting a branch (locally and remotely)

Deleting a branch (locally and remotely)

Problem:

I needed to delete a git branch but not lose the data on the branch. The desired outcome was to have a new branch that had the same details are the old branch and to empty out the old branch.

My existing state:

master # Empty
old_branch # Has a bunch of files I do not want to lose.

My desired state:

master # Empty
old_branch # Empty
new_branch # Has all the files that were in old_branch.

Solution:
It was a pretty painless process and straight forward. Git rocks!

$ git branch --list # See local branches
$ git branch --remotes # List the remote-tracking branches.
$ git checkout <old_branch>
$ git branch <new_branch>
$ git branch --delete <old_branch>
$ git push origin --delete <old_branch> 
$ git fetch origin --prune # After fetching, remove any remote-tracking references that no longer exist on the remote.
$ git push origin <new_branch>
$ git checkout master
$ git branch <old_branch> # Now old_branch is the same as master
$ git push origin <old_branch>
$ git branch --remotes
$ git branch --list

Source:
http://stackoverflow.com/questions/2003505/delete-a-git-branch-both-locally-and-remotely

Advertisements
Categories: git Tags:
  1. January 15, 2016 at 7:17 am

    I would have done something like this.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: