Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to move files or file changes between branches?

Tags:

git

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

  • the file B should be in its own branch2 (which does not exist),
  • file 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?

like image 995
WoJ Avatar asked Oct 19 '25 22:10

WoJ


1 Answers

A simple solution is, from where you are on branch1:

  • create branch2 (git checkout -b branch2), and remove C. Add, commit, and push
  • get back to master (git checkout master), and copy C from branch1 (git show branch1:path/to/C >path/to/C). Add, commit and push.
  • finally, get back on 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.

like image 173
VonC Avatar answered Oct 22 '25 16:10

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!