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?
Usually git does not overwrite anything during merge.
The “Your local changes to the following files would be overwritten by merge” error occurs when you try to pull a remote repository to your local machine whose contents conflict with the contents of your local version of the repository. To fix this error, either stash your changes away for later or commit your changes.
The reason for error messages like these is rather simple: you have local changes that would be overwritten by the incoming new changes that a "git pull" would bring in. For obvious safety reasons, Git will never simply overwrite your changes.
I'm working on the master branch. I create file1 and commit.
date >file1
git add file1
git commit -m 'added file1'
I create file2 and commit.
date >file2
git add file2
git commit -m 'added file2'
Whoops. I may want to use file2, someday, but it's definitely not something that should be put in the master branch.
Oops. Very simple. Make a new branch from where you are:
git checkout -b savingfile2
This will make the file2 change the commit for savingfile2
. Now go back and unwind one step on master
git checkout master
git reset --hard HEAD~1
At 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:
date >file3
git add file3
git commit -m 'adding file3'
date >file4
git add file4
git commit -m 'adding file4'
And now we finally want file2:
git checkout savingfile2
git rebase master # might need to fix conflicts here
git checkout master
git merge savingfile2 # will be a fast-forward
git branch -d savingfile2 # no need any more
That should do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With