user16092902
user16092902

Reputation:

How to copy files and changes in one branch to other branch

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 ?

Upvotes: 1

Views: 2657

Answers (2)

Moshe Fortgang
Moshe Fortgang

Reputation: 1150

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

Upvotes: 0

Jackkobec
Jackkobec

Reputation: 6755

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

Upvotes: 1

Related Questions