folibis
folibis

Reputation: 12864

Git - update only files existing in current branch

I have 2 branches in my local repo - master(core files) and master_with_adds(core + additional configuration)

All 2 branches contain same files but master_with_adds contains additional folder. Now I switched to master_with_adds, make some changes and commit it. How can I update my master branch with only files existing in master? i.e. I have to copy only system and libs, folder adds must not be copied.

SOLVED: I've found the answer here git 'merge' only existing files

Upvotes: 1

Views: 553

Answers (2)

Michael Durrant
Michael Durrant

Reputation: 96484

git checkout will do it. Something like:

$ git branch
* master
  master_with_adds
$ git checkout master_with_adds /system /libs
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   .....
#   new file:   .....
#
$ git commit -m "'Merge' code from 'master_with_adds' branch"
[master]: created 4d3e37b: "'Merge' code from 'master_with_adds' branch"
4 files changed, 72 insertions(+), 0 deletions(-)
create mode 100644 ...

Upvotes: 0

jthill
jthill

Reputation: 60275

If the changes you want brought over don't include adding or removing files, you can

git checkout master
git ls-files -z \
| xargs -0 git checkout master_with_adds --

and you're done. If you do want to import additions and removals easiest is probably to do those by hand, starting from there, although you can implement the complete procedure by using the GIT_INDEX_FILE environment variable to construct a sideband index of your complete desired state by feeding selected git ls-tree output to git update-index --index-info, then git write-treeing that, and git read-tree -uming the resulting tree to complete the transition. That strikes me as a bit overkill here.

Upvotes: 1

Related Questions