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
  • Git Tutorial
  • Git Exercises
  • Git Basic Commands
  • Git Cheat Sheet
  • Git Interview Questions
  • Git Bash
  • GitHub
  • Git Branch
  • Git Merge
  • Git WorkFlow
  • Git Hooks
  • Git LFS
  • Git Rebase
  • Git Cherry Pick
Open In App
Next Article:
Patch Operation in Git
Next article icon

Patch Operation in Git

Last Updated : 22 Jun, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
Git patch is a feature in git which enables you to create a patch file from a feature in one branch and apply it in another branch. A patch file has all the differences between the two branches. Using the patch file, we can apply the changes in a different branch. This can be visualized as - Suppose you are working on a project. The main project is in the "master" branch. You are working on a feature on a separate branch named "feature1". Now, you make all the changes in the "features1" branch and then create something known as a "patch file". Now, this patch file can be used in the master branch to add that particular feature you worked on.

Creation and Working of Patches

Let's see the following example: Step 1: There is a repository named 'Pat'. The user created a file "text.txt" in the master.   Step 2: Then it is added to the master branch and the change is committed with a message "initial".   Step 3: Checking logs   Step 4: Now, let create a new branch named feature and checkout to our feature branch.   Step 5: Change the "text.txt" by adding a few words.   Step 6: Next, we will save these changes, add and commit the changes in our feature branch with a message "this is in feature branch".   Step 7: Checking logs   Step 8: Now comes the main part- Creating a patch file. For creating a patch file we will write
git format-patch {branch in which we will apply our patch file} -o {folder in which we will save our patch file}
Currently, we are in the feature branch. We need to compare our changes to the branch where we want to apply our changes(here master branch). Then we use "-o" and specify the folder in which the patch file will be created (this is optional). We can also create a patch file using a particular commit's hash key. For example:-
git format-patch -1 012fe999affd9b92f6c96eb72a7260273411ea81 -o patches
The hash key is present in the git log written in dark yellow.   Step 9: Now, we come back to our master branch and check our "text.txt".
Note: Here, "cat text.txt" is used to view the file. This may not run in Windows. Please use Notepad or any code editor to view this.
  Step 10: Now, we finally apply our patch file in the master branch.
git am {path to patch file}
  Step 11: Checking logs

Next Article
Patch Operation in Git

A

ayushmankumar7
Improve
Article Tags :
  • Git
  • GitHub

Similar Reads

    Introduction to Github
    Collaboration and version control are important for software development. GitHub has become an important platform for developers, enabling seamless teamwork and efficient project management. In this article, we'll introduce you to GitHub, explaining what it is, how it works, and why it's a must-have
    6 min read
    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
    Using Patches in Git
    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 pa
    4 min read
    What is Git Interactive Rebasing?
    In the version control system, Git becomes a powerful and flexible tool for managing project history. Among its list of features, Git interactive rebasing stands as a powerful tool. In this article, we will learn more about Git Interactive Rebasing. Table of Content What is Interactive Rebasing?Prim
    4 min read
    Staging in Git
    In this article we will try to understand each and every aspect which may help us to understand about Staging in Git. To keep a track of modifications or changes in the file we have to bring that changes to our staging area which we bring by using Staging. Below command and this process of adding th
    8 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