Majk
Majk

Reputation: 87

Move a java class file from one branch in git to another branch

I have this java class flie.java which I commited and pushed with other java files in branch A. I want to move just the file.java from branch A to branch B without moving the other files that are pushed in branch A. I tried to do it from eclipse but It wasn't possible. I tried from git by following these steps: moving a file in git but there wasn't the option to move it to an existing branch. I know I can copy the file and paste it to branch B in eclipse, but that is not the purpose! Is there any git command that I can possibly use to move that file?

Upvotes: 0

Views: 1378

Answers (1)

Timothy Truckle
Timothy Truckle

Reputation: 15622

git checkout commitOrBranch -- path/to/file

So git checkout branchB -- path/file.java this with just checkout the branch with the file be moved then? – Marco

The file will be added to the current branch. Git will not raise a conflict when both branches will be merged.

But it may raise a conflict if you delete the file in the original branch before the merge.


If either side modifies the file further (not merely if the original branch deletes it) a conflict will almost certainly be raised. – Mark Adelsberger

The conflict is raised only if the file is change on both branches (before merge). This is exactly the advantage of "copying" the file via git instead of doing it via file system.

Upvotes: 2

Related Questions