Git

Basics

Bare basics

semantic versioning

https://semver.org/

clone

add

commit

checkout -> switch

remotes

log

git log

alias in ~/.gitconfig:

[alias]
lg1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(auto)%d%C(reset)' --all
lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(auto)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)'
lg = lg1

config

local

git config user.email "your_email@abc.example"
git config user.name "Example Name"

filemode

ignore filemod: git config (--global) core.fileMode false

lineend

on linux: git config --global core.autocrlf input

on windows: git config --global core.autocrlf true

to keep LF as end of line in the repo. check git docs

store credentials

others

Stash / Worktree

git stash

git worktree

git gitkraken use

add new worktree

git worktree add <path> <branch>

list worktrees

git worktree list

remove unused worktree

git worktree remove [-f] <worktree>

Merge, Rebase, Reset, Clean

merge vs rebase

https://www.atlassian.com/git/tutorials/merging-vs-rebasing

Make the current Git branch a master branch

git checkout better_branch
git merge --strategy=ours master    # keep the content of this branch, but record a merge
git checkout master
git merge better_branch             # fast-forward master up to the merge

rename branch (local and remote)

reset last commit(s)

merge (force remote)

git fetch --all
git reset --hard origin/master

mergetool

[diff]
    tool = meld
[difftool]
    prompt = false
[difftool "meld"]
    cmd = meld "$LOCAL" "$REMOTE"
    
[merge]
    tool = meld
[mergetool "meld"]
    # Choose one of these 2 lines (not both!) explained below.
    cmd = meld "$LOCAL" "$MERGED" "$REMOTE" --output "$MERGED"
 #   cmd = meld "$LOCAL" "$BASE" "$REMOTE" --output "$MERGED"

clean up

git clean -d -x ( -n | -i | -f )

will remove untracked files

Hooks

git hooks

pre-commit-msg

This example adds the branch name as prefix to the commit message:

pre-push example

GUI / visualize

CLI ui

show logs in a graph

git log --oneline --graph --color --all --decorate

for linux use alias gg="git log --oneline --graph --color --all --decorate" in ~/.bashrc

lazygit

lazygit on Github

GUI

Github / Gitlab

Common

create patch from commit

add

as a suffix onto commit links or merge request links.

examples:

Gitlab special

gitlab personal access token

Special usecases

rename branch

clone repo in existing folder

git clone https://myrepo.com/git.git temp
mv temp/.git code/.git
rm -rf temp
git init
git remote add origin {{url_of_clone_source}}
git fetch origin
git checkout master # delete files that are blocking