Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
Using Patches in Git
Next article icon

Using Patches in Git

Last Updated : 11 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Git allows developers to manage changes, collaborate with others, and maintain a history of their work. One of the lesser-known but highly useful features in Git is the ability to create and apply patches. This article will guide you through the process of using patches in Git, including creating patches, applying patches, and understanding their benefits.

Table of Content

  • What is a Patch in Git?
  • Creating a patch for a single file
  • Creating patch for a binary image
  • Creating patches form the commits
  • Applying a Patch

What is a Patch in Git?

A patch in Git is a file that contains a set of changes (diffs) between two versions of a repository. Patches are useful for sharing changes without using a central repository or for applying changes from one branch or repository to another.

Creating a patch for a single file

Suppose the description in gfg.txt file is changed, git diff --cached gfg.txt is done to check the changes made. Changes can be seen with git-diff --cached after a file is staged. If a new file is inserted in the repository it will not show the changes with git-diff, unless --cached is used with it.  

Now, suppose user wants to create a patch for this single file that was edited. git diff > gfg-intro.patch will be used where gfg-intro is the patch name.  

Creating patch for a binary image

If a binary image is added like a jpg or a png file in the repository(Project Folder). The changes of a binary file can be seen with git diff --staged --binary > image.jpg where "image.jpg" is the filename. Here --staged is used as we first add the file in the staging area then use git-diff to see changes.  

Now, to create a patch for this newly added binary file, git diff --staged --binary > binary.patch can be used where binary is the patch name. It is same like the above, the only difference is using --binary while creating the patch.  

Creating patches form the commits

To create the patch files for all the commits made in a particular branch, git format-patch -1 test-patchcan be used, where "test-patch" is the branch name and -1 is the number of commits that are to be included for creating the patch. In the below example we have used -1 and the resultant patch is named as 0001-Add-description.patch. Note that the name of file is based on the name of commit on that particular branch. In this way multiple patch files for multiple commits can be created in a particular branch. In below example it has generated only 1 patch file for the latest commit. You can also use git format-patch -1 HEAD, where HEAD is sha of the commit where header is pointed. 

To create a patch file from one commit to other i.e a patch file in which changes include all the commits from starting commit and end commit. It is done by git diff starting-commit-sha ending-commit-sha myPatch.patch, where "myPatch" is the patch name and the starting and ending sha of the commits are included.  

Applying a Patch

Suppose there is a patch file with changes done, here in our example for gfg.txt file. If the user wants to check these changes are correct or not, so a different branch is created from the master and applied the patch to check the changes. This is how the patch is applied in the new branch. 

Applying is done by git apply 0001-Add-description.patch where "0001-Add-description.patch" is the patch name that is to be applied. After applying the patch, the related file will be modified with the changes done in that patch file and can be reviewed, before pushing it to the master branch of the main repository.
 


Next Article
Using Patches in Git

S

sambhavkaul
Improve
Article Tags :
  • Web Technologies
  • Git
  • Write From Home

Similar Reads

    Git Introduction
    Git is a powerful and widely used version control system that helps developers track changes in their code, collaborate with others, and manage project history effectively. Whether you are a professional developer or just starting out, understanding Git is important for modern software development.
    5 min read
    Git - Working Tree
    Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific ap
    4 min read
    Saving a File in Git
    Git allows you to track changes, collaborate with others, and manage codebase efficiently. One of the fundamental tasks when working with Git is saving files, a process that involves several steps, including adding files to the staging area and committing them to the repository. In this article, we
    2 min read
    What is "origin" in Git?
    When working with Git, especially in collaboration with others, you will often encounter the term "origin." Understanding what "origin" means and how it functions within Git’s workflow is important for effective source code management. In this article, we will walk you through everything you need to
    4 min read
    How to Setup Git Using Git Config?
    Git is a powerful version control system that helps developers manage their code efficiently. To use Git effectively, you need to configure it properly using the git config command. This setup ensures that Git recognizes your identity, preferred settings, and workflow preferences.How the git configu
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences