# Git 🔹 *parent* [[⧋ Programming|Programming]] ▫️ *related* [[Git cheat sheet]], [[⏶ Github|Github]], [[✦ Git Essential Training|Git Essential Training]] ## Tasks: - [ ] Read more about the head pointer/current pointer - [ ] Read more about SHA-1 - not necessary, but helps explain how it comes up with IDs - [ ] Read more about atomic commits - [ ] Lookup more about `git checkout -- <file_name>` - [ ] Finish all of the skipped sections - [ ] Watch all of [git branches, merges, and remotes](https://www.linkedin.com/learning/git-branches-merges-and-remotes/branching-overview?u=2106729) - [ ] Configure command prompt for zsh again. Previously did this but I'm not sure exactly how it's configured --- ## Cheatsheet ![500](https://i.imgur.com/uUBbr45.gif) ## Notes: find ID and name of who commited ### Navigate the Commit Tree ### Reference commits SHA-1 Hash - 40 character string Sha - 8 chracters at end, or Head Pointer .git/HEAD - where it searches first .git/refs/heads/master - has SHA that points to current head Finding current head information: git show de14 git show HEAD These will find the parent of each branch, you can find the grandparents with ^^ ```bash de14621f^ HEAD^ master^ ``` Also this will find the great grandparent branch, number says how many levels up it should go. ```bash HEAD~3 ``` ##### Tree listings gives directory list of head ``` git ls-tree HEAD ``` pattern matches to find anything with "assets" in directory HEAD ``` git ls-head HEAD assets ``` ##### Filter and Format the commit log returns the last three commits git log -3 returns everything before the day listed in yyyy-mm-dd format: ```bash git log --since=2019-01-01 ``` returns all commits until the day listed ```bash git log --until=2019-01-01 ``` Other formats: ```bash git log --until="3 days ago" git log --after=2.weeks --before=3.days ``` shows only commits that have changed one file in a directory: ```bash git log explorers.html ``` Show statistics about what was changed ```bash git log --stat ``` ##### git logs ```bash git log ``` ```bash git help log ``` ```bash git log --since=2020-01-01 ``` ```bash git log --until=2020-01-01 ``` ### Branching ##### Staging commits and pushing to remote branch Adds to staging tree ```bash git add -A ``` Adds commit ```bash git commit -m 'this is the commit message' ``` Pushes to remote repository ```bash git push ``` The head pointer - current pointer - look up more on that. SHA-1 cat .git/refs/heads/master git log Tells you the difference - like untracked files - it will show staged and unstaged - if somethng has been commited, it will just say "working clean tree". ```bash git status ``` Shows all differences ```bash git diff ``` Shows differences between staged files ```bash git diff --staged ``` Similar but older way to do above command ```bash git diff --cached ``` Delete a specific file - need to have the correct relative path ```bash git rm file_to_delete.txt ``` If a file is the same content but the name is changed and they're both in the staging area, git will know that is probably just a renamed file. With `git status` it will list it as: ```git renamed: one_file.txt -> renamed_file.txt` ``` ### Reset Branches ##### Reset types Soft reset: - Moves HEAD Pointer - Does not change staging index - Does not change working directory Uses: - Return to an old state and leave code changes staged - Useful for amending one or more commits - Similar to `git commit --amend` - Previous commits will be discarded - Be careful about amending commits which have been shared ```bash git reset --soft <tree-ish> ``` Mixed reset - Moves HEAD pointer - Changes staging index to match repository - Does not change working directory - This is the default reset if soft or hard is not specified ```bash git reset --mixed <tree-ish> ``` Hard Reset: - Moves HEAD pointer - Changes staging index to match repository - Changes working directory to match repository ```bash git reset --hard <tree-ish> ``` ### Atomic commits `Git add` + `git commit -m` will just commit those files you've added (new and previously tracked), but `git commit -am` will commit all changes on tracked files, but it doesn't add new files. ```git git commit -am 'message' ``` ### Undo changes To checkout only one file from the master and apply it to current branch, you can then compare the two (somehow, lookup). ``` git checkout -- <file_name> ``` Find the commit ID ``` git log ``` Grab the id of the previous commit and check out a specific tracked file: ``` git checkout d4450dc7fa7 -- explorers.html ``` Then git status see the difference ``` git status ``` Show the differences between git diff --staged git checkout ### Revert a commit git show \[id\] ## Questions - I feel like the Git Lens plugin on VS code is a lot easier to use than git status - it's good to know how to use git status, but Git Lens is just cleaner ## References git notes from Evernote [Evernote](https://www.evernote.com/l/ARJJqYI3JpdDTqWNLssjo5SJkzV6U-T42hE) LinkedIn Learning, [course](https://www.linkedin.com/learning/git-essential-training-the-basics/)