Skip to content

Instantly share code, notes, and snippets.

@mrkpatchaa
Last active September 29, 2020 03:51
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save mrkpatchaa/f5949c4564cd504f72f110e6cbbd015f to your computer and use it in GitHub Desktop.
Save mrkpatchaa/f5949c4564cd504f72f110e6cbbd015f to your computer and use it in GitHub Desktop.
[Git command to export only changed files between two commits] #git

Use case : Imagine we have just created a project with composer create-project awesone-project (currently V0.2). 2 weeks later, there is a new release (V0.3). How to update your project ? Since composer update only updates the project dependencies, it is not what we are looking for. Composer doesn't know about awesome-project since it's not in our composer.json.

After trying many git solutions, I've come to this :

git archive --output=changes.zip HEAD $(git diff --name-only SHA1 SHA2 --diff-filter=ACMRTUXB)

This command will check for changes between the two commits and ignore deleted files. And after checking, it will copy those files into an archive. So you must git clone awesome-project before doing this.

git diff --name-status SHA1 SHA2 | grep "D\t"

This one will show you deleted files between the 2 commits, to help applying changes to your project.

After deleting files, you can unzip changes.zip and run \cp -rf ../changes/* . from your project directory to update your project with modified files.

PS : Some files could not be present in the latest commit. So you can first run git checkout SHA1 before running theses commands.

Inspired of https://gist.github.com/betweenbrain/2284129

Other solutions tried

  • Git Patch
cd project
git format-patch SHA1~..SHA2`
cd ../my-project
git am *.patch --reject

Errors :

The copy of the patch that failed is found in: .git/rebase-apply/patch
pathspec '0001-add-contributing-to-the-components-generator-guide.patch' did not match any files
Rejected hunk #1.

Nope : git am --abort

  • Git pull from remote origin and merge
git remote add awesome-project https://github.com/awesome/project.git
git remote update
git tag -l
git checkout tags/v4.1.0 new-branch
git branch -f master new-branch
git checkout master
Switched to branch 'master'
Your branch and 'origin/master' have diverged,
and have 1298 and 3 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

I've just added 1300 commits to my git history ... Nope

git branch -D new-branch
git reset --hard origin/master
git remote rm awesome-project
git remote update
@jegelstaff
Copy link

Tremendous! This is exactly what I needed. Thank you.

@kjeasy
Copy link

kjeasy commented Oct 10, 2019

Quite nice, just one line, no extra tools required, I love it.

@renekorss
Copy link

Instead of --diff-filter=ACMRTUXB you can do --diff-filter=d to ignore deleted files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment