1

Suppose I cloned branch1 and made new branch branch2 where I made changes to some files. However some new files are added in branch1 after that and some changes are made in previous files of branch1. I want those files and changes to be also in branch2. How to do this using git ?

branch1->some new files added and some previous files modified
|
|cloned
|
branch2->modified few files->add new files in branch1 here also as well as changes in old files

Can we use rebasing here ?

I will try to elaborate more pictorially :

branch1-a-b-c-d-e-f-g-h
|
|
|clone
|
|
branch2-x-y-z

What I want :

branch1-a-b-c-d-e-f-g-h
|
|
|clone
|
|
branch2-x-y-a-z-b-c-d-e-f-g-h

How to use rebasing here ?

  • 2
    Does this answer your question? [How to get just one file from another branch?](https://stackoverflow.com/questions/2364147/how-to-get-just-one-file-from-another-branch) – isherwood Jul 13 '21 at 13:00
  • the new files in branch1 are in one commit without other changes? – Moshe Fortgang Jul 13 '21 at 13:01
  • @mfort some changes in previous files of branch1 are made and some new files are added too. I want those changes in branch1 to reflect in branch2. Can I use rebasing here ? –  Jul 13 '21 at 13:04
  • @isherwood I have more than one new files in branch1 and some changes in old files are made. I want them to reflect in branch2. Should I use rebasing ? –  Jul 13 '21 at 13:09
  • @KRS7784 yes you can rebasing here and also can use cherry-pick. see https://stackoverflow.com/questions/14635672/rebase-a-single-git-commit – Moshe Fortgang Jul 13 '21 at 13:10
  • @mfort there might be multiple commit in branch1 and branch2 . Can you write answer (list of commands) on how to proceed. Should I used commands mentioned here https://stackoverflow.com/questions/11563319/git-rebase-basics ? I am in fear and I don't want to do any damage to branch1 . Could you please write answer if you know. –  Jul 13 '21 at 13:14

2 Answers2

1

git checkout source_branch paths

Example from branch sourceBranchName we get file Test.java

  1. Create new branch:

    git checkout -b targetBranchName

  2. Get state of Test.java file from the sourceBranchName to current branch without autocommit: git checkout sourceBranchName path to file or dirrectory:

    git checkout sourceBranchName pathTo/Test.java

Jackkobec
  • 5,889
  • 34
  • 34
  • I have multiple files in branch1, How to copy all of them into branch2 ? Can we use rebasing here ? Because some files which are common in branch1 and branch2, some changes have been made in them also –  Jul 13 '21 at 13:00
  • As far as I know the method above is working for directories too. Try to use git checkout sourceBranchName pathToDir/* The * means all files in dir. Also wildcards for files names can be used instead the *. Use extended GIT documentation in the official website. – Jackkobec Jul 13 '21 at 13:15
  • If files in the sourceBranch have been already committed, you can use cherry pick to get all of them. – Jackkobec Jul 13 '21 at 13:20
  • I have elaborated more on what I want to do in question –  Jul 13 '21 at 13:23
  • just merge branch1 into the branch2 – Jackkobec Jul 13 '21 at 13:30
  • Will that destroy changes I did in branch2 ? For example I wrote some extra line of codes in xyz.java in branch2, will those changes get reverted or lost ? –  Jul 13 '21 at 13:32
  • If u have crossing changed files, resolve conflicts and chose correct code during merge procedure. – Jackkobec Jul 13 '21 at 13:54
0

The sample way is to use cherry-pick for example:

git checkout branch2
git cherry-pick <commit-hash>

this copy the commits that you want from branch1 to branc2 and it does not affect branch1 at all

If you want to get all the changes from branch1, use git merge branch1 in branch2

Moshe Fortgang
  • 711
  • 4
  • 18
  • If I merge will that destroy changes I did in branch2 ? For example I wrote some extra line of codes in xyz.java in branch2, will those changes get reverted or lost ? Can I use first answer provided here : https://stackoverflow.com/questions/14893399/rebase-feature-branch-onto-another-feature-branch –  Jul 13 '21 at 13:33
  • I think you'll just go from brunch2 to a new branch and do all your experiments there for example `git checkout -b branch3` run it from branch2 – Moshe Fortgang Jul 13 '21 at 13:43
  • will cherry-pick revert my changes in branch2 if they are not in branch1 ? –  Jul 13 '21 at 13:43
  • cherry-pick not revert you changes if they are not in branch1 – Moshe Fortgang Jul 13 '21 at 13:45
  • does rebasing takes lot of time, after following accepted answer here https://stackoverflow.com/questions/14893399/rebase-feature-branch-onto-another-feature-branch, on terminal it's showing $(branch2|REBASE 1/5) –  Jul 13 '21 at 14:04