> For the complete documentation index, see [llms.txt](https://resources.oskarklintrot.se/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://resources.oskarklintrot.se/cli-cheat-sheets/git.md).

# Git

[Oh shit, git!](http://ohshitgit.com/)

Clear all unstaged changes, including ignored files:

```
> git clean -dfx
```

Find removed file:

```
> git log --all --full-history -- **/thefile.*
```

Git pull and rebase:

```
> git pull --rebase
```

[Config Git to do a stash before pull and rebase automatically:](https://stackoverflow.com/a/30209750/4826084)

```
# Once
> git pull --rebase --autostash
```

```
# Permanent
> git config pull.rebase true
> git config rebase.autoStash true
```

[Undo latest unpushed commit:](http://stackoverflow.com/questions/927358/how-to-undo-last-commits-in-git)

```
> git commit -m "Something terribly misguided"              (1)
> git reset HEAD~                                           (2)
<< edit files as necessary >>                               (3)
> git add ...                                               (4)
> git commit -c ORIG_HEAD                                   (5)
```

[Find and restore a deleted file:](https://stackoverflow.com/questions/953481/find-and-restore-a-deleted-file-in-a-git-repository)

```
> git rev-list -n 1 HEAD -- <file_path>                     (1)
> git checkout <deleting_commit>^ -- <file_path>            (2)
```

[Remove all local branches that have been merged into the branch currently checked out:](https://stackoverflow.com/a/43334157/4826084)

```
> git branch --merged | ? {$_[0] -ne '*'} | % {$_.trim()} | % {git branch -d $_}
```

[Remove untracked branches (replace `-D` with `-d` to only remove fully merged branches:](https://stackoverflow.com/questions/13064613/how-to-prune-local-tracking-branches-that-do-not-exist-on-remote-anymore)

```
> git fetch --prune
> bash
$ git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -D
```

Staging Patches:

```
> git add -i # Choose option "5" or "p" as in "patch"
```

Undo Patches:

```
> git checkout -p <optional filename(s)>
```

Replace remote with local branch:

```
> git checkout BranchToPushTo
> git reset --hard BranchToReplaceWith
> git push --force
```

[Modify a specific commit:](https://stackoverflow.com/a/1186549/4826084)

```
# WARNING! Only do this in your own branches!
> git rebase --interactive 'bbc643cd^'
# NOTE! Modify 'pick' to 'edit' in the line mentioning 'bbc643cd'
> git commit --all --amend --no-edit
> git rebase --continue
```
