Here’s the situation. I’m working on the master branch. I create file1 and commit. I create file2 and commit. Whoops. I may want to use file2, someday, but it’s definitely not something that should be put in the master branch. So that I don’t lose file2 I use
git checkout head~1
git branch new-branch
git checkout new-branch
so that I can continue developing. I add file3 to new-branch. If you’ve been paying attention, I’ve got two branches, master that contains “file1” and “file2” and new-branch that contains “file1” and “file3”.
Now is the time to get the changes I’ve made back into the master branch. What’s the best way to do this? I definitely want the head of the master branch to point at the files as they appear in new-branch, but I also don’t want to lose the work I’ve done in file2 by doing a reset, in case I want to use it.
Keep in mind this is a simplification. Instead of just three files, I’ve got a dozen files with tens of lines of code being changed all over the place all with multiple commits. I certainly hope the solution isn’t to do a file-by-file merge/checkout, because that would be a huge pain.
Any ideas?
Oops. Very simple. Make a new branch from where you are:
This will make the file2 change the commit for
savingfile2. Now go back and unwind one step on masterAt this point, the commits leading up to master will reflect the addition of file1,
and the additional commit between master and savingfile2 will be the addition of file2 to that.
If you make more changes to master, and then want to bring file2 back eventually, you’ll want to rebase that side-branch onto the new master:
And now we finally want file2:
That should do it.