Skip to content

Instantly share code, notes, and snippets.

@deanrather
Created July 8, 2014 05:46
Show Gist options
  • Save deanrather/44bf0d4988730ecf1e7d to your computer and use it in GitHub Desktop.
Save deanrather/44bf0d4988730ecf1e7d to your computer and use it in GitHub Desktop.
How to use Git Patch

How to use Git Patch

In this scenario, a Git Repo has been exported, and the contents of the repo deployed onto an environment.

This deployment does not have any knowledge of Git.

On the deployment:

Initialise a new git repo, so any changes can be tracked.

git init
git add .
git commit -am "initial commit"

You can now make your changes, then we commit them as usual. Let's say we make 2 commits for this patch.

echo 'hello world' > test-file
git add test-file
git commit -m "test commit"

echo 'some test' > another-test-file
git add another-test-file
git commit -m "added another file"

Then we create the patches for these 2 commits

git format-patch -2

This will create 2 patch files.

On the Original Git Repo

Copy the patch files onto the machine with your actual git repo, then apply them like this

git apply 0001-test-commit.patch
git diff # review changes
git add test-file
git commit -m "applied patch commit 1"

git apply 0002-added-another-file.patch
git diff # review changes
git add another-test-file
git commit -m "applied patch commit 2"
@ForrestErickson
Copy link

I am going to try this and I hope this helps some people.
I made a folder with a simple README.md. I made the changes above
After initializing the repository I change the root branch from master to main.

Here is the git CLI and a folder where I was just after the step "git format-patch -2"
image
image

Here is the text of the patch file: 0001-test-commit.patch
image

Here is the text of the patch file: 0002-added-another-file.patch
image

Here is the git status in the CLI
image

I applied the first patch with:

git apply 0001-test-commit.patch
git diff # review changes
git add test-file
git commit -m "applied patch commit 1"

I got an error
image

error: test-file: already exists in working directory
On branch main

Trying just the line "git apply 0001-test-commit.patch" produces the error.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment