Git apply patch Example
1. Introduction to Git
In this post, we feature a comprehensive article on Git apply patch. Git is a distributed version control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity and support for distributed, non-linear workflows. Git is free and open-source software distributed under the terms of the GNU General Public License version 2.
The major difference between Git and any other VCS is the way Git thinks about its data. Conceptually, most other systems store information as a list of file-based changes. Git thinks of its data more like a set of snapshots of a mini filesystem. Every time you commit or save the state of your project in Git, it basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot. To be efficient, if files have not changed, Git doesn’t store the file again—just a link to the previous identical file it has already stored. Git has detailed documentation specified in the URL.
1.1 Git Commands
These are some of the common git commands which are usefulStatus Command
This is used to add the check the status of all the files under git consideration
- untracked – This is the initial state. The file is not tracked by git so far and will be tracked after adding to the local repository.
- staged- This is the state for a tracked file but the latest changes have not been committed.
- Committed – The file has been committed to the local repository but it is not available in the remote repository.
git add <filename>
This is used to add the file to the local repository for committing. The file is supposed to be in staged state once the command has been executedCommit Command
git commit -m "Message"
This is used to commit the file to the local repository. Each commit needs a message to indicate the reason for the commit which is provided by the
-m flagPush Command
git push origin master
This is used to push our commits to the remote repository. A single commit contains multiple files and their changes. Once the commit is pushed, a success message gets displayed on the screen. Here, origin refers to the remote branch pointer while master is the remote branch.Pull Command
Ideally, a git repository is used by multiple folks and can have commits at various intervals. In order to push our commits, We need to pull in the remote changes. This is done by the above command. The default execution of this does a merge operation which tries to merge the remote with our local and this is indicated by a merge commit.
Alternatively, to maintain a linear history the
-r flag is used which sequences the commits instead of a merge. Hence, there is no separate merge commit and local commits are added on top of remote commits indicating a linear history. This is called rebase.
2. Git Patch
In this section, We will look at ways of creating patch file and how we can apply the patches.
2.1 Git Patch Use cases
Generally the patch files generated are sent via email. They can be generated for a repository and applied to another. Most likely they will be applied in another branch. Another usecase is when the remote repository is undergoing outage, the commits can be passed via patch files to other developers.
2.2 Git Patch Create
Let us initialize a new git repository by executing the following command in a new folder(test)
cd test git init
This creates an empty repository. Now lets add an empty file 1.txt to the folder and commit the file using the below command.
git commit -m "Initial commit"
Now we can create a branch where we can apply the patch files. A new branch of the repository can be created by using the command
git checkout -b branch1
The above command creates the branch and also navigates to the branch. We can see that empty file we created is present also in the branch. We can switch back to the original
master branch using the command
git checkout master
master branch, We will add content to the text file. We will add first hi to the text file and commit it. Then subsequently add hello to the text file and commit it. The final contents of the text file should be1.txt
Git also has a log command using which we can verify git history. After our series of commits git history must be similar to belowLog Command
git log --pretty=oneline
git log displays all the commit messages along with the changes. Appending the
oneline flag displays only the commit messages.Displayed Log
* ae92d22 (HEAD -> master) Added hello * 33915bd Added hi * 423a8c8 Initial commit
Now we have added two commits on top of our original branch which is missing in the branch
branch1. Now we can create patch files for importing in another repository.Patch Create Command
git format-patch -2
This creates two patch files one for each commit respectively. Patch files are created in the same directory. If the patch files need to be created in a specified directory, this can be achieved by the below command.Patch Create Command
git format-patch -2 -o <dirname>
The full path of the directory with the name must be specified in the above command. This creates the patch files in the specified directory.
2.3 Git Apply Patch
To apply the patches, We have to navigate to the branch
branch1 using the checkout command. Once we are in branch1, we can apply the patch in two ways.Patch Apply with manual commit
git apply 0001-Added-hi.patch
In this mode, the patch commit is applied but the files are neither staged nor committed. User has to manually commit the file after adding the file.Manual commit
ga 1.txt git commit -m "Added hi as apply patch"
Another approach is to apply the patch file as commit itself. This results in each patch generating a new commit on the target repository. We can achieve it by running the below command.Patch Apply as commit
git am 0002-Added-hello.patch
Now we can verify the history of the branch by running the
git log command. This should be similar as below.Displayed Log
* 20b396e (HEAD -> branch1) Added hello * 89a042d Added hi as apply patch * 6b1886b Commit 1
We can see the difference between the last two commit messages indicating the difference in how the patch was applied.
In this post, we saw how to create a git patch and two ways of applying the git patch in another branch.