# Git #typ/Cheatsheet for the git version control system. Note for Students: ![[Git#^cwbmwh]] ## Sensible Defaults - [.gitignore](https://github.com/github/gitignore) templates - [.gitattributes](https://github.com/gitattributes/gitattributes) templates - note: you shall not edit this file with uncommited files! it fill fuck shit up - if you do anyway, delete `.git/index.lock` - if you use worktrees, delete `.git/worktrees/*/index.lock` as well - copy/paste: `find -name "*.lock" -exec xargs rm {} \;` <!-- ```gitconfig [user] name = Livia email = [email protected] ``` --> ## Commands - **commit** only changed or deleted files: `git commit -a` - add message: `git commit -am` - view **history** with `git log --online` - check files modified in a commit with `git log --name-status` - check what a certain commit did with `git show <commit-hash>` - check history of a file `git log --<path-to-file>` - **push** to orgin main:`git push -u origin main` - **throw away** local unstaged changes `git reset --hard` ### maintenance - **rename** the branch (e.g. from **master to main**) - switch to the branch using `git checkout master` - then rename it with `git branch -m main`. - use `git maintenance start` on **large and/or frequently used repos**! - adds a cronjob that will make things faster. - probably not necessary if you dont use the repo reguarly. - `git switch -c new_branch` creates **new branch** and checkout - `git restore -p file.txt` lets you **restore** only parts of a file ### select only some files - stage with `git add file1 file2 file5` - you can also use the usual patterns like `directory/*` or `*.png` - check whats staged with `git status` - commit as usual oneliner: ``` git commit file1 file2 file5 -m "commit message" ``` ### safe force push when force pushing, use `–-force-with-lease` instead of `--force`, that checks for any changes in remote. If they do not differ from the local ones, its the same, if there are changes, you dont fuck shit up! ### blame people - `git blame -w` ignores whitespace - `git blame -w -C` and detect lines moved or copied in the same commit - `git blame -w -C -C` …or the commit that created the file - `git blame -w -C -C -C` …or any commit at all! ## Modifyers ### -L only outputs a little - `git blame -L 15,26:path/to/file` → outputs only blame for lines 15 to 26 - works for git log as well: `git log -L 15,26:path/to/file` - that works for functions or classes too: `git log -L 15,26 :Function:path/to/file` ## Config ### Conditional Config This example matches all repositories that reside below a folder called „Academia“, wherever that is (in my case encrypted cloud storage): ``` [includeIf "gitdir:**/Academia/"] path = ~/.gitconfig_uni ``` ### Sort brances - `git config --global column.ui auto` gives you multiple columns side by side - `git config --global branch.sort -comitterdate` sorts them by comit date! ## Forges ### Tasks and Projects If you are using Gitlab projects or Forgejo, you can sync the tasks assigned to you to a neat local app: [super productivicy](https://github.com/johannesjo/super-productivity). Neat! - Super Productivity is an advanced todo list app with integrated Timeboxing and time tracking capabilities. It also comes with integrations for Jira, Gitlab, GitHub and Open Project. ### GitHub Alternatives - Gitlab: https://framagit.org/ - Forgejo: https://codeberg.org/ ## Resources - I highly recommend students to watch those two videos. Its half an hour of your life, that will save you headache for your WHOLE life ^cwbmwh - [YouTube: „Git for Academics“ by Zettlr](https://www.youtube.com/watch?v=PBRmtABqC9U) - [YouTube: „Git For Dummies“ by Nick White](https://www.youtube.com/watch?v=mJ-qvsxPHpY) - includes working with remotes and branches (great for experimenting and collaboration) - instead of using github, use www.codeberg.org (you dont want your stuff on US servers right now, trust me) - If you are in an academic career or a programmer, some of the following will probably make your life a WHOLE LOT easier - [YouTube: „So You Think You Know Git“ by Scott Chacon](https://www.youtube.com/watch?v=aolI_Rz0ZqY) - Slides: [So You Think You Know Git? - Speaker Deck](https://speakerdeck.com/schacon/so-you-think-you-know-git) - [YouTube: „So You Think You Know Git Part 2“ by Scott Chacon](https://www.youtube.com/watch?v=Md44rcw13k4) - slides: [So You Think You Know Git - Part 2 - Speaker Deck](https://speakerdeck.com/schacon/so-you-think-you-know-git-part-2) - details on worktrees: [Git Worktrees and GitButler](https://blog.gitbutler.com/git-worktrees/) - [Pro Git book, written by Scott Chacon and Ben Straub](https://git-scm.com/book/en/v2) - [Git Worktree - GeeksforGeeks](https://www.geeksforgeeks.org/git-worktree/)