Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slackero/e6866b8a6d711f81fd53c96ca1bb76de to your computer and use it in GitHub Desktop.
Save slackero/e6866b8a6d711f81fd53c96ca1bb76de to your computer and use it in GitHub Desktop.
Git command to export only changed files between two commits

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment