How to pick some files in a commit using Git
-
20-06-2021 - |
Вопрос
In one of the commits that I want to cherry-pick
from Branch B to Branch A, there are four files that changed, but I only want one of the files in that commit to be on Branch A. How do I get that file on Branch A? Thanks!
Решение
It's a little bit of a hassle but this works:
If you have more files you don't want to commit then files you want to commit:
git checkout BranchA
git cherry-pick <sha1>
git reset HEAD^
git add <file>
git commit
git reset --hard
If you have more files to commit then not:
git checkout BranchA
git cherry-pick --no-commit <sha1>
git reset HEAD <file>
git commit
Другие советы
One option would be git show -p COMMIT_HASH:path/to/file > path/to/file
to grab the file which has changed and then add and commit.
cherry-pick, revert all files but the one you want, commit amend:
git cherry-pick BranchB
git show --pretty=format: --name-only | grep . | grep -v fileyouwant | xargs git checkout HEAD^ --
git commit -a --amend -C HEAD