Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copy one file or directory out of a git repo as a patch
#!/bin/bash
#Inspired by http://blog.neutrino.es/2012/git-copy-a-file-or-directory-from-another-repository-preserving-history/
#Copy a file or directory out of a git repository, preserving history!
#Creates DESTINATIONPATH with patches that can be applied with git am
#e.g.
#0001-Add-new-theme-Gum.patch
#0002-Add-syntax-highlighting-for-Gum-theme.patch
#0003-Gum-Fix-tag-URLs-not-being-slugified-and-therefore-b.patch
#0004-Gum-Add-Disqus-support.patch
#0005-Gum-Use-article-title-as-the-title-of-the-generated-.patch
#0006-Gum-HTML-escape-tag-names-when-rendering-them.patch
#Usage: copy-git-file.sh /some/repo/interesting/thing /destination/patch/path
#todo, test $1 and $2
DESTINATIONPATH=$2
SOURCE=$1 #first arg to script, either file or dir
pushd $SOURCE
git format-patch -o $DESTINATIONPATH $(git log $SOURCE|grep ^commit|tail -1|awk '{print $2}')..HEAD $SOURCE
popd
@JJ

This comment has been minimized.

Copy link

@JJ JJ commented Nov 23, 2015

I'm missing something here. Do you have to copy the file to the new destination first?

@OlegEfrem

This comment has been minimized.

Copy link

@OlegEfrem OlegEfrem commented Dec 23, 2015

Doesn't work for files, fails with the message: copy-git-file.sh: line 19: pushd: /some/path/SomeFile.scala: Not a directory

@galaxystar

This comment has been minimized.

Copy link

@galaxystar galaxystar commented Feb 16, 2016

I made some modifications:

  1. Check if SOURCE is in fact a directory, since the documentation says it can be a file (was causing problems with pushd/popd)
  2. Made SOURCE and DESTINATIONPATH play nice with paths that contain spaces.
#!/bin/bash
#Inspired by http://blog.neutrino.es/2012/git-copy-a-file-or-directory-from-another-repository-preserving-history/
#Copy a file or directory out of a git repository, preserving history!
#Creates DESTINATIONPATH with patches that can be applied with git am
#e.g.
#0001-Add-new-theme-Gum.patch
#0002-Add-syntax-highlighting-for-Gum-theme.patch
#0003-Gum-Fix-tag-URLs-not-being-slugified-and-therefore-b.patch
#0004-Gum-Add-Disqus-support.patch
#0005-Gum-Use-article-title-as-the-title-of-the-generated-.patch
#0006-Gum-HTML-escape-tag-names-when-rendering-them.patch

#Usage: copy-git-file.sh /some/repo/interesting/thing /destination/patch/path

#todo, test $1 and $2
DESTINATIONPATH="$2"
SOURCE="$1" #first arg to script, either file or dir

if [ -d "${SOURCE}" ] ; then
   pushd "$SOURCE"
fi

git format-patch -o "$DESTINATIONPATH" $(git log "$SOURCE"|grep ^commit|tail -1|awk '{print $2}')..HEAD "$SOURCE"

if [ -d "${SOURCE}" ] ; then
   popd
fi

All that being said, I had a hard time getting SourceTree to import the patch.

@hellboy81

This comment has been minimized.

Copy link

@hellboy81 hellboy81 commented Sep 6, 2016

Please post Windows version of this script

@dbrisinda

This comment has been minimized.

Copy link

@dbrisinda dbrisinda commented Dec 20, 2016

This can be simplified:

git format-patch -o $DESTINATIONPATH $(git log $SOURCE|grep ^commit|tail -1|awk '{print $2}')..HEAD $SOURCE

can be replaced with:

git format-patch -o $DESTINATIONPATH --root $SOURCE
@CNG

This comment has been minimized.

Copy link

@CNG CNG commented Apr 20, 2017

I further automated this in a function that handles patching and cleanup: https://github.com/CNG/dotfiles/blob/master/mods-available/git/functions/git-import

@dgpokl

This comment has been minimized.

Copy link

@dgpokl dgpokl commented Feb 13, 2019

I further automated this in a function that handles patching and cleanup: https://github.com/CNG/dotfiles/blob/master/mods-available/git/functions/git-import

Sadly, this link seems broken now. Can you re-share? @CNG

Edit: Found it: https://github.com/CNG/dotfiles/blob/458fb4a92a603f9c9bbaac3111d2202711396e5f/files/zsh/config/git/functions/git-import

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.