- Search the reflog for info about a commit (only works if the relevant info hasn't been garbage-collected):
git reflog --no-abbrev | grep <HASH>
- List the number of lines in each file:
git ls-files | xargs wc -l
- Count the number of lines of all Python files
git ls-files | grep \\.py$ | xargs wc -l
- Search the code for a certain string. The oldest commit containing the string is listed first:
git log --all -p --reverse --source -S "string"
git --no-pager log --all -p --reverse --source -S "string"
- remove last commit
git reset HEAD^
- interactive rebase onto origin
git rebase -i origin
-
Staging only parts of a file
- edit the diff before adding it to the staging area:
git add -e <filename>
- quickly stage individual hunks:
git add --patch <file>
- edit the diff before adding it to the staging area:
-
Combine the last two commits into a single commit:
git rebase -i HEAD~2
- Check out only a certain subfolder:
git clone -n --depth=1 --filter=tree:0 https://example.com/test.git
cd test
git sparse-checkout set --no-cone /subfolder
git checkout