How to move code between branches?

Say, i have 2 branches in my git project and I wrote some code in one branch and want to copy it to another branch (while I don't want to merge or cherry-pick commits). I usually use ctrl+c, git checkout another-branch, ctrl+v, but when there's planty of code in different places it quickly becomes a mess. I would first copy all the code to some temp file, then checkout another branch and past it hunk by hunk.

Is there some more efficint way to do it? For example, it would be nice if I could select the files with the code that I want to move, and sort of "freeze them" (copy them outside of project and mark as read-only). Anything you can suggest?

4 comments
Comment actions Permalink

Hello Dziamid,

Why don't you want to cherry-pick or merge? It is a natural way to move changes between branches in Git. I'd say, much more natural than copy-pasting.

0
Comment actions Permalink

Cherry picking can be a hard work, because you may need to find all commits related to the file in question, cherry-pick them all, then rebase the commits and remove irrelavant code. And merging is sometimes impossible, because some branches are not supposed to be merged (e.g. master can be merged into slave, but not the other way)

0
Comment actions Permalink

So do you mean that you want to move not particular changes, but entire content of a file or several files?
Then you may use the following command to get the file content as it were in specific revision:
  git show <commit_hash>:<relative_path_to_file>
To apply the changes to the file you may do:
  git show <commit_hash>:<relative_path_to_file> > <relative_path_to_file>

0
Comment actions Permalink

Seems like a better solution. Thanks.

0

Please sign in to leave a comment.