blog/content/cheat-sheets/git.md

91 lines
2.6 KiB
Markdown
Raw Normal View History

2019-04-22 11:35:20 +00:00
---
2019-04-22 13:44:16 +00:00
Title: Some git tricks
Date: 2019-04-22 17:00
2023-05-14 18:22:31 +00:00
Modified: 2023-05-14 20:00+2:00
2019-04-22 13:44:16 +00:00
Author: Fabrice
Category: cheat sheets
2019-04-22 17:54:15 +00:00
Tags: git, termtosvg
2019-04-22 15:15:19 +00:00
Slug: git-tricks
2019-04-23 13:31:17 +00:00
Header_Cover: images/covers/water.jpg
2019-04-23 09:00:07 +00:00
Summary: A compilation of some `git` tricks I keep forgetting.
2019-04-23 18:04:46 +00:00
Lang: en
2019-04-22 11:35:20 +00:00
---
2023-05-14 18:22:31 +00:00
Some [git](https://git-scm.com/) tricks I use from time to time and that I
forgot everytime…
2019-04-22 11:35:20 +00:00
2023-05-14 18:22:31 +00:00
**Disclaimer:** I'm not the perfect git user, and my way of using it is
especially crude.
Recently, most of my git commits are due to [pass]({filename}/software/pass.md),
therefore most of those commands are here to fix my own mistakes.
2019-04-22 15:27:43 +00:00
2019-04-22 11:35:20 +00:00
Reset `master` to `origin/master`:
```sh
2019-04-23 18:04:46 +00:00
git checkout origin/master -B master
```
Jump back and forth from a commit to another (same behaviour as :
```sh
git checkout -
2019-04-22 11:35:20 +00:00
```
2023-05-14 18:22:31 +00:00
Find back lost commits, especially useful when you are doing dirty things and
want to `cherry-pick` an orphan commit (for instance):
2019-04-22 15:27:43 +00:00
```sh
git log --graph --reflog
```
2023-05-14 18:22:31 +00:00
Some explanations: `--graph` show the commit tree, which is useful to notice the
orphan leafs, and `--reflog` shows the world all the dirtiness you've done.
2019-04-22 15:27:43 +00:00
2023-05-14 18:22:31 +00:00
If you noticed that your folder grows, you can manually cast the garbage
collector on it. It should do it automatically, but not often enough according
to my standards.
2019-04-22 15:27:43 +00:00
```sh
git gc --aggressive
```
2019-04-23 18:04:46 +00:00
To add changes in an atomic fashion on a file (in an interactive way):
2019-04-23 09:00:07 +00:00
```sh
2019-04-23 18:04:46 +00:00
git add -p <file>
2019-04-23 09:00:07 +00:00
```
2023-05-14 18:22:31 +00:00
**Unsafe:** to remove a file from the past (for instance when you commit some
*sensitive* file):
2019-04-24 15:28:08 +00:00
```sh
git filter-branch --prune-empty --tree-filter "rm -f '<file name>'" HEAD
git push --force
```
2023-05-14 18:22:31 +00:00
Another useful trick is `git commit -v`, it allows reviewing your changes before
committing.
2019-04-22 11:35:20 +00:00
2019-04-24 08:56:01 +00:00
<object data="/examples/git-cv.svg" width="100%">Use of git commit -v</object>
2019-04-22 15:27:43 +00:00
2019-04-23 09:00:07 +00:00
A ncurse-based interface for git: [tig](https://jonas.github.io/tig/)
```sh
tig
```
2019-04-23 18:04:46 +00:00
2023-05-14 18:22:31 +00:00
To make `diff`s more readable, I highly recommend to use delta as a diff filter:
<https://github.com/dandavison/delta>.
It makes the display of `git diff` close to what you can get in a browser
repository, highlighting the places where diffs actually happen and is highly
reconfigurable.
Once it is installed in your package manager, you can for instance configure it
as follows (mostly the same as in the GitHub page):
```gitconfig
[core]
pager = delta
[interactive]
diffFilter = delta --color-only
[delta]
navigate = true # use n and N to move between diff sections
light = true # set to true if you're in a terminal w/ a light background
theme=Monokai Extended Light
[merge]
conflictstyle = diff3
[diff]
colorMoved = default
```