Reputation: 29977
Summary: I forgot to switch branches when developing and would like to move the files/changes to the appropriate branch.
Details: I have two files A
and B
which are initially in master
. I then created a branch branch1
to make some breaking changes in file A
.
While developing these changes I also modified B
and created a file C
. The problem is that I now realize I made a mistake with branching and that
B
should be in its own branch2
(which does not exist), C
belongs in master
. All of them are now commited in branch1
.
The commits were pushed to a remote repository but it does not matter that much, I can have the other developer clone the repo (as opposed to fetch).
What is the general methodology to recover in such a case, ie. to move files or changes between branches?
Upvotes: 4
Views: 2763
Reputation: 1848
When this happens to me, I usually use the stash to move changes around branches. Assuming you didn't commit anything:
git stash
, or alternatively git stash save 'useful message to remember what I was doing'
to save your changesgit checkout -b branch2
git stash apply
to apply your changes to Bgit checkout master
C
which shouldn't have changed nor moved while performing these commands, unless there is another file named C
.Since you already commited everything, I simply suggest you to undo it; this shouldn't be a problem unless you shared your work with someone else.
git reset HEAD^
should perform this, leaving you with the files as if they hadn't been commited to the branch. You can now perform the above operations.
If you already shared your work with the rest of the world, however, I wouldn't alter the history, unless you can coordinate everyone involved to retrieve your changes.
Upvotes: 0
Reputation:
git cherry-pick <SHA1_hash>
git diff <starting_commit_hash> <ending_commit_hash> > changes.patch
git checkout <branch>
git apply --check changes.patch
git apply -p1 < changes.patch
rm changes.patch
git diff <starting_SHA1_hash> <ending_SHA1_hash> -- path/file > changes.patch
git diff --full-index --binary <starting_SHA1_hash> <ending_SHA1_hash> > changes.patch
git reset --hard <SHA1_hash> or git branch -D <branch>
Upvotes: 0
Reputation: 4275
I am thinking that you are going to want to look at the git log using the following command:
sudo git log
Then, you will want to checkout/revert the hash of a previous commit that was done where you edited a file but not others yet and that is what you want in branch2. To checkout/revert to the old commit use the following command:
sudo git revert THELONGNUMERICHASHCODEGOESHERE
e,g. sudo git revert 102b78f76dcf2302345570b4f738b28700266f87
After you have checkout the commit you want, you can then out the branch into the new branch you want with the following command:
sudo git checkout -b branch2
Moving on to file C, this one might be tricky but you can try:
sudo git checkout master
sudo git checkout master-2
branch1
into master-2
. While on master-2
run: sudo git merge branch1
sudo git checkout branch1
B
using sudo git log
sudo git checkout branch-fileb
branch-fileb
, merge back in branch1 using sudo git merge branch1
branch-fileb
using: sudo git merge master
master
, then merge in branch branch-fileb
using: sudo git merge branch-fileb
And the branches should all be how you want them now.
For good housekeeping delete and local unused branched and any unused remote branches.
Upvotes: 0
Reputation: 1323863
A simple solution is, from where you are on branch1
:
git checkout -b branch2
), and remove C
. Add, commit, and pushC
from branch1
(git show branch1:path/to/C >path/to/C
). Add, commit and push.branch1
, remove the extra files, add, commit and push.That way, no merge, no history rewrite, not even a revert, just some additional commits pushed to remote to cleanup the current situation.
Upvotes: 3