![]() |
VOOZH | about |
Git Patch is a feature in git that allows you to create a patch file (diff file) from changes in one branch and apply those changes to another branch.
This process demonstrates how to create a patch from one branch and apply it to another branch.
Create a new repository named Pat and add a file text.txt inside it.
👁 ImageStage the file and commit it to the master branch with an initial message.
git add text.txt
git commit -m "initial"👁 ImageView the commit history to verify the changes.
git log👁 ImageCreate a new branch named feature and switch to it.
git branch feature
git checkout feature👁 ImageEdit the text.txt file by adding some new content.
👁 ImageStage and commit the updated file in the feature branch.
git add text.txt
git commit -m "this is in feature branch"👁 ImageView the commit history of the feature branch.
git log👁 ImageGenerate a patch file from the feature branch changes.
git format-patch master -o patches👁 ImageWe can also create a patch file using a particular commit's hash key. For example:-
git format-patch -1 012fe999affd9b92f6c96eb72a7260273411ea81 -o patchesThe hash key is present in the git log written in dark yellow.
Move back to the master branch to apply the patch.
git checkout master👁 ImageNote: Here, "cat text.txt" is used to view the file. This may not run in Windows. Please use Notepad or any code editor to view this.
Apply the generated patch file to the master branch.
git am patches/<patch-file-name>👁 ImageCheck whether the patch has been successfully applied.
git log
cat text.txt👁 Image