Git tips

Compare a file across different branches

This shows a diff

git diff <branch1>:<path/to/file> <branch2>:<path/to/file>

You can even apply the changes based on this diff

git diff <branch1>:<path/to/file> <branch2>:<path/to/file> | git apply

git rm --cached

This won't remove a file from your local machine. Also, this is recorded differently from git rm by git. Let's say I run git rm --cached garbage-file, commit and push it. When another person pulls this commit, the garbage-file on his local machine will not be deleted. It simply becomes untracked.

Delete submodule completely

# Remove the submodule entry from .git/config
git submodule deinit -f path/to/submodule

# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/path/to/submodule

# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f path/to/submodule

Create an archive from a git repo

git archive --format tar.gz --output /full/path/to/file.tar.gz master