Reputation: 1228
I am trying to create a colorized diff between two folders in the same repository (This may be the totally wrong approach).
I believe one way of achieving this is through git diff --no-index folder1 folder2
and it will automatically create a patch for you (Cool).
However, the diff also includes (as one might expect), all of the .gitignore
'd files as well.
folder1
node_modules
src/
.gitignore
package.json
README.md
folder2
node_modules
src/
.gitignore
package.json
README.md
I would like to ignore node_modules
.
Possibilities? Alternatives?
Upvotes: 15
Views: 2884
Reputation: 60255
Cheaty version:
git diff $(git commit-tree -m '' @:folder1) $(git commit-tree -m '' @:folder2)
which makes an on-the-fly commit for each tree (as they were on checkout) and diffs the two commits.
You can easily make scratch commits if you want to do this for the current worktree contents,
( export GIT_INDEX_FILE=.git/scratch-index
git add .
temp=`git write-tree`
git diff $(git commit-tree -m '' $temp:folder1) $(git commit-tree -m '' $temp:folder2)
)
and for finer control over what you're using you can pipe git ls-files
with whatever pathspecs you want to git update-index --add --stdin
.
Upvotes: 1
Reputation: 51810
If you care about comparing the two versioned contents of the directories (e.g : the contents of dir1/
and dir2/
in the current commit, without taking into account whatever modifications there could be in the index or the worktree), you can ask git diff
to compare two trees already written in your repo :
# 'HEAD:dir1' is a syntax to designate the tree object stored at path 'dir1'
# in the current active commit ('HEAD')
git diff HEAD:dir1 HEAD:dir2
This will compare only the content tracked by git (hence: the .gitignore
d content, which isn't written at all in the repo, will not show up)
If you also want to compare the modifications in the index or the worktree while respecting .gitignore
rules, you may use several tricks to have git
create that tree :
git write-tree
will create a tree object from the staged content
treehash=$(git write-tree)
git diff ${treehash}:dir1/ ${treehash}:dir2/
you may use git stash create
to quickly have a tree which contains that content
hash=$(git stash create)
git diff ${hash}:dir1/ ${hash}:dir2/
(note that git stash *create*
is not the same as git stash
or git stash push
, it creates the structure that is stored in the stash but doesn't update the stash with it)
Upvotes: 0
Reputation: 3367
I don't have a simple solution as one would hope, but two somewhat unconventional yet easy alternatives.
# -x means that files from the .gitignore will also be cleaned
# --interactive shows you what will be deleted before doing so
git clean -x --interactive folder1 folder2
# now the files simply don't exist any more
git diff --no-index folder1 folder2
cd ..
git clone original-directory clone-of-repo
cd clone-of-repo
# in the clone, ignored files are naturally not included
git diff --no-index folder1 folder2
Upvotes: 0
Reputation: 141996
You can try and use the assume-unchanged
flag
https://git-scm.com/docs/git-update-index
When this flag is specified, the object names recorded for the paths are not updated.
Instead, this option sets/unsets the "assume unchanged" bit for the paths.When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).
Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.
Upvotes: 0