0

I created a new branch using git checkout -b new_branch_name.

I haven't run git add or git commit yet.
I want to get rid of this branch completely, including any new files, and switch back to master. How do I do this?

8bittree
  • 1,769
  • 2
  • 18
  • 25
josh
  • 115
  • 1
  • 12
  • 1
    Possible duplicate of [How do I delete a Git branch both locally and remotely?](https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-both-locally-and-remotely) – Raibaz Jul 31 '17 at 12:52

2 Answers2

9

Use the following to switch back to master:

git checkout master

Use the following to delete the branch you created:

git branch -D new_branch_name
tambre
  • 4,625
  • 4
  • 42
  • 55
  • This deleted the branch, but not the files on my machine. I even went back to the master branch and pulled from github and that didnt work. I had to manually delete the files I made. – josh Jul 31 '17 at 15:35
  • 1
    @josh There is nothing unexpected about that. Since you did not make a commit, Git has/had no knowledge of those files. It is wrong to expect them to be deleted when you checkout another branch; 'master' for example. – Thom Parkin Aug 01 '17 at 00:29
2

A branch is just a pointer to a commit:

$ cat .git/refs/heads/master
6eef8fb523469b12abc530cd105c7b92a4a0a76a

So, when you delete a branch with git branch -D branch_name, it just deletes the file branch_name in .git/refs/heads, it doesn't touch the working tree (i.e. the actual c or java or python or whatever files you're using git to track).

To clean up any untracked files after deleting your branch, just use:

git clean -df

The -d flag will also remove untracked directories. The -f flag forces the clean in case the config option clean.requireForce is set to true. You can add the -i flag if you want to review the work interactively.

If you have any tracked files with changes after deleting your branch, you can reset those with:

git reset --hard
8bittree
  • 1,769
  • 2
  • 18
  • 25