Essential Git for Scientists
  • Introduction
  • Basic Concepts
    • Version Control
    • Git
    • Repo
    • Commits
    • Branches
    • Remotes
    • GitHub
    • De-centralisation
    • Summary
  • Basic Operations
    • Install Git
    • Create a Repo
    • Make a Commit
    • Inspect a Previous Commit
    • Revert a Change
    • Make a Branch
    • Extend a Branch
    • Fast-forward merge
    • Resolve conflicts
  • Intermediate Concepts
    • Commits
    • Three Trees
    • Rebase
    • Fetch
    • Pull
    • Push
  • Advanced Concepts
    • Reset
    • Interactive rebase
    • Formatted patches
    • Blame
    • Stash
    • Log filter
  • Cookbook
    • Undo
    • Branches
    • Diff
    • Stash
    • Merge
    • Hooks
    • Squashing
    • Rebase
    • Interactive Rebase
    • LFS
    • Submodules
    • Remote
    • Force push
    • Identify merged branches
    • Formated patches
    • Apply patches
    • Interactive rebase
    • Squash commits
    • Pull rebase
    • Log
    • Blame
    • Biset
    • Reset
  • Exercise
    • Exercise 1
    • Exercise 2
Powered by GitBook
On this page
  • How to do it
  • Command
  • How it works
  • What to look out for
  1. Advanced Concepts

Formatted patches

You can apply commits from another branch to the current branch with no problem.

  • For a single commit, it's a cherry-pick

  • For a sereis of commit, it's a rebase

However, this needs to be in the same repo. What if you want to apply a commit to a different repo?

How to do it

Well, you can do that via formatted patches. The way it works is that you

  1. save the commit as a formatted patch, a plain text file

  2. send the file to someone

  3. execute the formatted patch

Command

To generate the formatted patch

git format-patch -l <commit> # Single commit
git format-patch <commit-1>...<commit-2> # A series of commits

How it works

Each commit consists of a set of changes, and each change can be expressed as an instruction. A formatted patch is a text version of these instructions, which Git can understand and run.

What to look out for

To apply the formatted patch, the current file should be compatible. For example, if the change is to remove the line import pandas as pd in file analyse.py, it will fail if

  • you don't have a file named analyse.py, or

  • the file doens't have this line

PreviousInteractive rebaseNextBlame

Last updated 1 year ago