Git
custom tool:
bash <(curl -fsSL https://git-ssh-quick.leanderziehm.com) git@github.com:leanderziehm/devops.git
list recent commit messges:
git log --oneline
show most recent commit file diffs:
git show
git uses less to paginate text. shortcus: jump to the G, search / quit q
undo git commit:
git reset --soft HEAD~1
if the change was already pushed then you can use git push -f but its quite dangerous because it could delete progress of other contributors so be careful when using force push.
git reset --hard HEAD~1
git logs
pull new commits without conflict:
git pull --rebase
get stats about the size of your clone github repo:
git count-objects -vH
setup username and email:
git config --global user.name "Your Name"
git config --global user.email "yourname@emailexample.com"
basics:
git add .
git commit -m "write commit message here"
git push
setup:
git config --global user. email "you@example.com"
git config --global user.name "yourusername"
Purge Files from history:
git filter-repo --path-glob '*.csv' --invert-paths
File History:
git log --follow --patch -- path/to/file
Delete Branch
delete branch locally
# Delete the branch safely (only works if changes are merged)
git branch -d branch_name
delete branch from the remote repository
# Delete the branch from the remote repository
git push origin --delete branch_name
Git ssh
input: git@github.com:leanderziehm/docusaurus_obsidian.git
cd ~/.ssh/github ssh-keygen filname: docusaurus_obsidian
cd ~/dev/ git clone
GIT_SSH_COMMAND='ssh -i ~/.ssh/github/docusaurus_obsidian -o IdentitiesOnly=yes' git clone git@github.com:leanderziehm/docusaurus_obsidian.git
cd project folder
git config core.sshCommand "ssh -i ~/.ssh/github/docusaurus_obsidian -o IdentitiesOnly=yes"
Vscode Problems
sometimes vscode does a bunch of requests to the git server with the wrong credentials and then the firewall blocks you for some hours. So better to use git mostly over cli with ssh. TODO research more.
Tips
when using ssh with passphrase ssh-add can save time. ssh
.gitignore
priority order
Git checks multiple sources, and the last matching rule wins:
- Command-line ignore rules (if used)
.gitignorecan exist in many folders.git/info/exclude(local repo-only ignores)- Global ignore file (
core.excludesFile, often~/.config/git/ignore)
Pattern basics
#: comments!: re-include a file that was previously ignored\: escape special characters/: directory separator
Matching rules
*→ matches any characters except/?→ matches a single character[]→ character ranges (e.g.[a-z])- Patterns can match:
- files
- directories (depending on trailing
/)
Special cases
/pattern→ matches only from repository root (or current.gitignore)pattern/→ matches directories only**→ powerful recursive matching:**/file→ file anywheredir/**→ everything inside a directorya/**/b→ matches multiple nested levels
Important behavior notes
-
Git does not track ignored files if they are already tracked
-
To stop tracking a file:
git rm --cached file -
Ignored directories are not fully scanned for performance reasons.