Think of a situation you need to create a patch with a unmerged pull request or from commits that did not apply to master branch for some reason.
Best practice:
Do your patch in a separate branch.
Will do it practically 😃 Let's use this Test Repository or you can use your own Repository.
git clone https://github.com/Rajithkonara/jenkinsTest.git
cd jenkinsTest
git checkout -b patch_test
Now we are in the new branch (patch_test), do your fix here After doing changes commit them.I have made one new commit, Let's consider this commit as the patch we need.Note that we did not merge these changes to the master branch.
git log --oneline
d039b7b remove headers a2d28ef with error 77e5f30 git hook-1Now in a real scenario, you will send a PR to the Original repo. Here will use the commits.
For patch creating either of these methods can be used.
1) Creating patch file from the commit.
We are gonna create the patch from the latest commit. All the changes we have made will be included in this file so you can apply the patch from a single file. git format-patch master --stdout > remove_headers.patch
This will create a new file remove_headers.patch with all changes from the latest commit against the master. Now the patch is ready.2) Creating the patch file from PR
- Push your changes to your forked repo and create a pull request against original repo.- Go to pull request URL.
- Add .patch at the end of the pull request URL, it should be like this,
https://github.com/Rajithkonara/jenkinsTest/pull/1.patch
- This will create a .patch file with all commits, download and save it.
Now in both these above steps, we will get same patch file containing the changes you need to add.
Applying The Patch.
- keep the .patch file outside the git repository directory.
- Now we are in patch_test branch
- checkout to master branch. Since here we are applying a patch to our master branch. ( you can decide )
- commit log in master branch.
- As you can see the latest commit in the patch_test branch is not applied to master branch yet.
Check what changes will be applied by patching.
- Make sure you are in the master branch.
git apply --stat ../remove_headers.patch
Test the patch before Applying it.
git apply --check ../remove_headers.patch
If you don't get any errors running this command you are good to go with the patch.Applying for real using git am.
git am --signoff < ../remove_headers.patch
Great !!! you have successfully applied the patch use a git log to view your commits.References: https://www.devroom.io/2009/10/26/how-to-create-and-apply-a-patch-with-git/
Thank you !!!!
good Work Rajith !
ReplyDeleteThank you Ruwan !!!
ReplyDelete