AppyPrinciplesGetting started
git's cheat sheet Apply a patch

The standard process for working collaboratively on the same piece of software is is to use branches. That being said, in some situations it can be appropriate to use a more lightweight approach: producing and applying diffs (also named patches).

Producing a diff

Suppose you have made some changes on the main branch in some app (the output has been shortened and translated from french):

MySelf@MyLaptop:~/projets/HubSessions$ git status
...
Updated files:
    updated :         Advice.py
    updated :         HsGroup.py

The detailed diff can be produced by the git diff command:

MySelf@MyLaptop:~/projets/HubSessions$ git diff
diff --git a/Advice.py b/Advice.py
index 19830fa..52e61c9 100644
--- a/Advice.py
+++ b/Advice.py
@@ -12,6 +12,8 @@ from HubSessions.HsGroup import HsGroup
 NO_PERFORMER = 'Mail needs to be sent to item performers regarding a given ' \
                'advice but no performer has been found.'
 
+# This addition is crucial.
+ # ------------------------------------------------------------------------------
 class Config:
     '''Configuration about advices'''
diff --git a/HsGroup.py b/HsGroup.py
index 541c4a2..6bbb656 100644
--- a/HsGroup.py
+++ b/HsGroup.py
@@ -614,6 +614,10 @@ class HsGroup:
         '''On user page "prerogatives", groups must be sorted (a) by committee
            and (b) by their order as defined in their container.'''
         container = group.container
+        if not container: # Should not happen
+            self.log('%s (%s, %s) has no container.' % \
+                     (group.id, group.title, group.state), type='warning')
+            return sys.maxint
         try:
             order = container.ids('hsGroups').index(group.id)
         except ValueError:

This code comes from an old project written in Python 2. The crucial change on Advice.py was added to illustrate the fact that the diff can contain changes to several files.

You can produce a single file containing this output via this command:

MySelf@MyLaptop:~/projets/HubSessions$ git diff > hubsessions.crucial.diff

Then, you may send this file by email to your favourite repository guru, that will be in charge of integrating the changes.

Applying a diff

Suppose you are now the guru in question (what a nice feeling, huh?). You open your mailbox, read the file and realize how pertinent and awesome the change is. After a few seconds, you copy the file in the root of your local copy of the same repository.

guru@PowerfulLaptop:~/projets/HubSessions$ ls hubsessions.crucial.diff
hubsessions.crucial.diff

As a guru, you are working hard and one of your local files is currently undergoing heavy changes.

guru@PowerfulLaptop:~/projets/HubSessions$ git status
...
Unvalidated updates :
  updated : Item.py
Unversioned files:
  hubsessions.crucial.diff
...
guru@PowerfulLaptop:~/projets/HubSessions$ git diff
diff --git a/Item.py b/Item.py
...
- 
+# I am working in this file.
...

You apply the diff (or patch) by performing the following command.

guru@PowerfulLaptop:~/projets/HubSessions$ git apply hubsessions.crucial.diff

The changes were applied, leaving the existing change on Item.py untouched.

guru@PowerfulLaptop:~/projets/HubSessions$ git status
...
Unvalidated changes:
    updated :         Advice.py
    updated :         HsGroup.py
    updated :         Item.py
Unversioned files:
    hubsessions.crucial.diff
...

You can now delete hubsessions.crucial.diff and perform any subsequent update until you commit.