So, there's a number of legacy answers here. I'm going to show you how I start over on a branch in 2021:
When you have made dozens of commits and dozens of files changed and you need to reset
git checkout master
git pull origin master
git checkout -b feat-foo-v2 # make a second version of feat-foo branch
now you have a fresh branch. But you likely still did a bunch of work that is still good, you just need to pull in those files. While in your root git directory:
git checkout feat-foo -- path/to/file/to/be/used.java
Now you have a copy of the individual file from the old branch. Do this a few more times, and the old branch will be obsolete. Now you can feel free to delete that branch, and rename feat-foo-v2
to feat-foo
.
Let's say you have a file with some changes, and you only want to pull in some of those changes. I suggest getting familiar with the --patch
option on git checkout
:
git checkout -p feat-foo -- path/to/file.java
will open up a dialog that allows you to select the parts of the changes to the file you want to keep.
When you can't just make a new branch
For some reason, you're just enamored with your feature branch and you're unwilling or unable to give it up. You know you need to reset a few files, but you shouldn't need to reset the whole thing. Creating a whole new branch was actually just an un-necessary step. We can pull the fresh files from master:
git checkout master -- path/to/file.java
and now a file is reset! And if you just want to reset part of a file, --patch
should work the same way.