'How to add files to another branch?
For some reason I want to abandon my branch_A but I have some files just committed in that branch_A. I want to add them to branch_B. How can I do that?
Solution 1:[1]
If you're confused on how to execute right commands in console which was proposed above, then you have a lazy, easy option:
1) Copy your files out of your project directory 2) Checkout to your branch_B 3) Replace your copied files with checked-out ones, you can even merge them 4) Commit the change
Solution 2:[2]
git reset HEAD^ will move you one commit back. ie. just before you made the commit onto branch_A. Now switch to branch_B using git checkout branch_B. Then git add the files you want to commit and then git commit them. They will be on branch_B.
Solution 3:[3]
Switch on branch_A: git checkout branch_A , use git log and record your commit id. Then switch on branch_B :git checkout branch_B and use git cherry-pick your commit id to move the commit into branch_B.
Solution 4:[4]
to selectively add changes from another branch
git checkout --patch other_branch file1 file2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Novitoll |
| Solution 2 | Noufal Ibrahim |
| Solution 3 | Jintao Zhang |
| Solution 4 | Geoff Langenderfer |
