DEV Community

zankyr
zankyr

Posted on • Updated on

Git & tricks: commands

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

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'
Enter fullscreen mode Exit fullscreen mode

Restore file from another branch

To restore a file using the version from another branch:

$ git restore --source develop -- /path/to/file/to/restore
Enter fullscreen mode Exit fullscreen mode

where

  • --source develop is the source branch. This version will override the file in your current branch;
  • /path/to/file/to/restore is 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-*')
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)