pylapp

CLI

My Git cheatsheet

๐Ÿ‡บ๐Ÿ‡ธ โ€“ Friday, September 27th 2024

Keywords: #Git, #commits, #GitHub, #GPG, #CLI

A bit of context

Sometimes I need to get back some commands I used in my terminal so as to work on my Git repositories of GitHub / GitLab projects. But I can have often some doubts about them, so I decided to list here some useful commands as a cheatsheet or a simple reminder to keep and share. I will try to keep it updated. There are nice resources in the bottom of this document!

Applying some common Git configurations

Use also the โ€”global option if you want to apply this configuration everwyhere.

Apply 'git config' command with parameters 'user.name', 'user.email' and 'user.signingKey' and after the value

Showing GPG keys to get ID to use for commits

Useful to remember the ID of the GPG key to use to sign commits. More details in this other publication (in french).

Apply GPG command 'gpg --list-keys --keyid-format=short'

Verify GPG signature of commits

Keep in mind the software forges like GitHub and GitLab provide also some verification process for commits.

'git verify-commit' or 'git log --show-signature'

Display contributors of a commit

You can also filter to get other metadata in commits, find here for example some ideas.

Use options of command 'git show'

Create branch from another and switch

That's not the only command of course, but I like this one.

Apply command 'git checkout -b new-branch source-branch'

Get rid of the last N commits

You can use of course other things instead of HEAD~N.

Apply command 'git reset --hard HEAD~N' then push with option '--force-with-lease'

Reword, reorder or squash last N commits with interactive rebease

You can use of course other things instead of HEAD~N. Beware of fixup and squash, not the same uses with the commit messages.

git rebase -i HEAD~N

Create a Git patch

Allows to generate a Git patch from a diff to apply or send it later.

Use commands 'git diff' or 'git format-patch' then 'git apply'

Keeping somewhere changes temporary

Useful for drafts or temporary changes you don't want to version.

Use variants of command 'git stash' with actions 'push', 'pop', 'apply'

Find someone to blame (maybe you!)

The aim is to display who modified lines of files.

Use command 'git blame' with -p and -e options

Clean the repository

Clean the project, get rid of files, but beware, you may loose things!

Use 'clean -fd' to get rid of unnsaved files and directories, 'reset --hard' to go back to last commit, 'rm --cached' to remove modifications but keeping file and 'checkout' to make a file be reset to last commit

Work on a fork and submit

The workflow is simple: fork, create local develop branch with your dev stuff, make meaningful commits, then cherry-picks from this branch to a virgin one so as to submit a nice and simple pull request.

Add upstream remote, checkout dev branch and rebase, make cherry-pick from working branch to pull request branch

Resources

You have also some online resources like git-cheat-sheet.readthedocs.io.

But, maybe most important, you can refer also to this amazing comic of Julia Evans, available online and also buy her book. Her comic bellow is licensed under CC-BY-NC-SA 4.0.

Add upstream remote, checkout dev branch and rebase, make cherry-pick from cworking branch to pull request branch [Code samples rendered with carbon.now.sh]

โ€” Last update: Wednesday, October 23th 2024 โ€”

Did you enjoy reading this blog? Give me a beer ๐Ÿบ or use something else โค๏ธโ€๐Ÿ”ฅ Licensed under CC-BY-SA 4.0. Opinions are my own. To contact me, feel free to choose the most suitable medium for you, or for example Mastodon.