简体   繁体   中英

Git: How do I selectively copy changes from master to branch?

I created a branch myBranch from the master repo some time ago. Since then, several changes have been pushed to master and now I need to make sure some of these changes, but not all of them, make their way into myBranch. In other words, only some modified files need to be copied from master to myBranch (I know that "copy" is probably not the right git term, but I don't know what the right one is).

So if I file file1.c and file2.c that have been modified in master, and I want to copy only file2.c to myBranch, what the right way to do it? Thx

If you want to bring commit over, you can use git cherrypick <commit-sha> .

That'll create a new commit that contains the same change as the picked commit.

Otherwise, if your commits are too broad and changes files you don't want to bring over, then you can checkout parts of master like so git checkout master -- /file/to/update . The file will be checked out exactly as the version on master (so if you have changes on your branch on the same file and you want to keep them, then this is not the best idea).

If none of these works for you... then I'd suggest you to copy changes by hand (or play a lot with interactive rebase) and work on better (more commits and smaller change scope) commits in the future.

Anyway, note that in all the cases, playing like this with history might bring up a lots of conflicts when you try to merge your branch back into master.

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