DEV Community

zankyr
zankyr

Posted on • Edited on

4 1

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay