简体   繁体   中英

how to merge two branches with few changes not all

I am working on Visual Studio Code and using git as the VCS , now i have a branch called X from this branch i have created another branch called Y using command ( git checkout -b Y ) then in Y branch i have added some new code as well as made some changes in the previous code.

Now I want to merge Y into X only with the changes not the new code which i have added.

also what are the problems in checking out to the parent branch without commit the changes made in child branch?

I don't think a git merge is the correct process for your situation. You may want to cherry-pick a commit or two into the root branch (only those commits that don't include the new files). I suggest this because Git's merge feature is intended to bring the entirety of a branch into another branch.

With that said, it is possible to do merge and accomplish your objective. To do so, perform a merge without letting the merge process make the commit . The result will be a set of changes staged for commit. You can then modify your staged changes (by unstaging the files you don't want merged in) before making the final commit.

Based on your example above you would first checkout branch X then:

git merge Y --no-commit --no-ff

If it was a clean merge, after the above command you should see a message:

"Automatic merge went well; stopped before committing as requested"

From that point you can unstage any changes you don't want merged in and then perform a git commmit with only the desired changes merged in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM