Git & tricks: commands
zankyr
Posted on November 17, 2022
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 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-*')
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
💖 💪 🙅 🚩
zankyr
Posted on November 17, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
githubcopilot AI Innovations at Microsoft Ignite 2024 What You Need to Know (Part 2)
November 29, 2024
productivity ✍️🤖 Learn How to Improve Your Prompts and Get More Detailed and Relevant Responses
November 29, 2024