Thursday, 27 December 2018

GIT: Working with patch files

49.1. What is a patch file?

patch is a text file that contains changes to other text files in a standarized format. A patch created with the git format-patch command includes meta-information about the commit (committer, date, commit message, etc) and also contains the changes introduced in binary data in the commit.
This file can be sent to someone else and the receiver can use it to apply the changes to his local repository. The metadata is preserved.
Alternatively you could create a diff file with the git diff command, but this diff file does not contain the metadata information.

49.2. Create and apply patches

The following example creates a branch, changes several files and creates a commit recording these changes.
# create a new branch
git branch mybranch
# use this new branch
git checkout mybranch
# make some changes
touch test05
# change some content in an existing file
echo "new content for test01" >test01
# commit this to the branch
git add .
git commit -m "first commit in the branch"
 The
next example creates a patch for these changes.
# creates a patch --> git format-patch master
git format-patch origin/master

# this creates the file:
# patch 0001-First-commit-in-the-branch.patch
To apply this patch to your master branch in a different clone of the repository, switch to it and use the git applycommand.
# switch to the master branch
git checkout master

# apply the patch
git apply 0001-First-commit-in-the-branch.patch
Afterwards you can commit the changes introduced by the patches and delete the patch file.
# patch is applied to master
# change can be committed
git add .
git commit -m "apply patch"

# delete the patch file
rm 0001-First-commit-in-the-branch.patch
Use the git am command to apply and commit the changes in a single step. To apply and commit all patch files in the directory use, for example, the git am *.patch command. You specify the order in which the patches are applied by specifying them on the command line.

49.3. Create a patch for a selected commit

You can specify the commit ID and the number of patches which should be created. For example, to create a patch for selected commits based on the HEAD pointer you can use the following commands.
# create patch for the last commit based on HEAD
git format-patch -1 HEAD

# create a patch series for the last three commits
# based on head
git format-patch -3 HEAD

0 comments:

Post a Comment