A LITTLE CORNER OF MY WORLD

Read a few words, keep a little time

Every moment worth recording has a light of its own.

Back to all posts
Dev notes

Git cheat sheet

Some of the best commands I use everyday.

On this page
  1. Configure Git
  2. Works with GPG
  3. Look all the diff make today
  4. Restore a deleted branch
  5. Create a branch on a commit
  6. Closing Issue on Github
  7. Delete a branch on github
  8. Clean a old Git main or master branch (safe way)
  9. Merge a pull request from another repository
  10. Merge a pull request from Copilot
  11. Delete your previous commit

All the commands, unordered.

Configure Git

REF: https://git-scm.com/book/uz/v2/Git-Tools-Signing-Your-Work

Add Identity

git config --global user.name "John Doe"
git config --global user.email "johndoe@example.com"
git config --global core.autocrlf input
git config --global core.safecrlf true
git config --global core.editor "nvim"

Avoid to convert endline ^M in some files

git config core.autocrlf false

Works with GPG

Adding your GPG key, found your key with: gpg -k | grep -i "\[s\]"

git config --global user.signingkey <ID KEY> # ID Key for Sign

Sign a commit

git commit -a -S -m 'signed commit' # -S for crypt commit

Look all the diff make today

git diff "@{yesterday}"

Restore a deleted branch

git reflog --no-abbrev
fd3...
git checkout -b release-0.4.0 fd3

Create a branch on a commit

git branch NEW_BRANCH_NAME COMMIT_ID

Closing Issue on Github

https://stackoverflow.com/questions/60027222/github-how-can-i-close-the-two-issues-with-commit-message

In a commit message, add any of: close, closes, closed, fix, fixes, fixed, resolve, resolves, resolved

The pull request must be on the default branch.

git commit -a -S -m "resolve #1, closes #2, closes #3; YOUR COMMIT MESSAGE"

Delete a branch on github

git branch -d devel
git push origin --delete devel

Clean a old Git main or master branch (safe way)

git clone https://github.com/szorfein/dotfiles && cd dotfiles
git checkout --orphan newBranch
git add -A
git commit
git branch -D master
git branch -m master
git push -f origin master
git gc --aggressive --prune=all

ref: https://gist.github.com/stephenhardy/5470814

Merge a pull request from another repository

git checkout -b marespiaut-patch-1 master
git pull https://github.com/marespiaut/szorfein.github.io.git patch-1 #
use the branch patch-1
git checkout master
git merge --no-ff marespiaut-patch-1
git branch -d marespiaut-patch-1
git push origin master

Merge a pull request from Copilot

git merge --no-ff origin/build/go-release
git push origin -d build/go-release

Delete your previous commit

Do this before pushing to a remote Git repository

git reset --soft HEAD~1
✿

Thank you for reading. I hope these words were useful.

Szorfein
Szorfein / Author

A little code, a little everyday life. Say hello in the guestbook. Guestbook