This is my collection of unconventional git commands. Here you won’t find basic commands like git log or git checkout, but more particular instructions that have come in handy during my daily work.
- Empty commit
- Restore file from another branch
- Delete branches by filters.
- Delete all local branches not matching the provided name(s)
Empty commit
If you want to make a commit even if you didn't modify any file, for example
to create a bookmark or checkpoint, just pass the --allow-empty option:
$ git commit --allow-empty -m 'Started working on PR'
Restore file from another branch
To restore a file using the version from another branch:
$ git restore --source develop -- /path/to/file/to/restore
where
-
--source developis the source branch. This version will override the file in your current branch; -
/path/to/file/to/restoreis the path (absolute or relative) of the file to restore.
Delete branches by filters.
In the example, all the branches starting with "fix" will be removed:
$ git branch -D $(git branch --list 'fix-*')
Delete all local branches not matching the provided name(s)
In this example, all the branches different from master, main or develop will be deleted:
$ git for-each-ref --format '%(refname:short)' refs/heads \
| grep -v "master\|main\|develop" \
| xargs git branch -D
Top comments (0)