---
title: "Uncommon but useful Git commands"
date: 2022-05-17
description: "A cheatsheet for Git commands Molecule.dev uses internally on a regular basis."
tags: [engineering, git, assemble, mlcl, cli, tools, cheatsheet]
canonical: https://blog.molecule.dev/useful-uncommon-git-commands
---
This is a cheatsheet for useful Git commands that we use semi-regularly, mostly for internal use as a quick future reference but I figured I would share.

### Change commit author date

```sh
git commit --amend --no-edit --date="Wed Mar 30 22:45:55 2022 +0200"
```

### Get last commit to modify file

```sh
git log -n 1 --format="%H %s" -- path/to/file
```

### Get first commit

```sh
git log --reverse --format="%H %s" | head -1
```

### Find commits with modified code matching some string

```sh
git log --branches -S'some code you need to fix'
```

### Find commits with modified code matching some regular expression

```sh
git log --branches -G'some code you need to fix'
```

### Create a commit message with a trailer

```sh
git commit -am 'add something' --trailer="Molecule: apiPackageManager=NPM, apiLanguage=TypeScript"
```

### Find dangling commits

```sh
git fsck --lost-found 2>&1 | grep "dangling commit"
```

### Wipe unused history

```sh
git for-each-ref --format="delete %(refname)" refs/original | git update-ref --stdin
git reflog expire --expire=now --expire-unreachable=now --all
git gc --prune=now --aggressive
```

### Remove untracked files and directories

```sh
git clean -f -d
```

### Delete all remote branches:

```sh
git branch -r | grep origin/ | grep -v 'index$' | grep -v HEAD| cut -d/ -f2 | while read line; do git push origin :$line; done;
```
