Home > Software design >  How can I compare two commits in git and move the changed files to a new directory?
How can I compare two commits in git and move the changed files to a new directory?

Time:10-11

I am trying to generate a patch with only the files that are changed.

If the last 4 commits are A --> B --> C --> D, I want the latest version of the changed files only from all 4 commits.

So if File123.txt was changed in commit A and again in commit C, I want the version from commit C to be moved to a new directory, along with all the other changed files from all 4 commits.

CodePudding user response:

I ran git diff HEAD.. but I don't see any kind of output.

That is expected, since git diff HEAD.. would compare two commits, the second being... HEAD itself.

git diff @

That would compare HEAD with its parent commit (unless HEAD is a merge commit, in which case, you need the -m option (By default, git diff-tree --stdin does not show differences for merge commits).

I would also consider the git format-patch (as in here) to generate the patches (to apply with git am)

CodePudding user response:

If you want to generate a single patch file, which combines the changes of the last four commits, just run :

git diff xx D   # where 'xx' is the parent of A
# equivalent to :
git diff A^ D

# or if D is your active commit (HEAD) :
git diff HEAD~5 HEAD

If you want to list the files that were modified in these commits, just add --name-only to any of the commands above :

git diff --name-only HEAD~5 HEAD
  •  Tags:  
  • git
  • Related