一部のファイルのみをコミットするにはどうすればいいですか? 質問する

一部のファイルのみをコミットするにはどうすればいいですか? 質問する

プロジェクトが 2 つあります。1 つは「公式」プロジェクトで、もう 1 つは軽い変更 (いくつかのファイルを追加) です。新しいブランチを作成し、そこに新しいファイルを配置しました。しかし、開発中に、両方のブランチに共通するいくつかのファイルが変更されました。

これらのファイルのみをコミットするにはどうすればよいですか?

ベストアンサー1

あるブランチに変更をコミットし、その変更を他のブランチで表示できるようにしたいとします。Git では、ブランチを変更するときに HEAD の上に変更があってはなりません。

変更されたファイルのみをコミットするには、次の操作を行います。

git commit [some files]

または、ステージングエリアが清潔であることが確実な場合は、

git add [some files]       # add [some files] to staging area
git add [some more files]  # add [some more files] to staging area
git commit                 # commit [some files] and [some more files]

そのコミットを両方のブランチで利用できるようにしたい場合は、

git stash                     # remove all changes from HEAD and save them somewhere else
git checkout <other-project>  # change branches
git cherry-pick <commit-id>   # pick a commit from ANY branch and apply it to the current
git checkout <first-project>  # change to the other branch
git stash pop                 # restore all changes again

おすすめ記事