# 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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://resources.oskarklintrot.se/cli-cheat-sheets/git.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
