## Commits ### Jan Kozubowski --- ## .gitignore `git check-ignore -v path/to/file` `git status --ignored` --- <!-- .slide: style="font-size: 0.85em;" --> ## Git restore Restore file content from a specific commit (uncommited changes in the restored files will be lost!). This does not move HEAD - you stay on the current commit. ``` # Last commit: git restore <file-name> # or discard all unstaged changes in directory git restore . # One commit before: git restore --source HEAD~1 <file-name> # Old approach: git checkout HEAD~1 <file-name> ``` Unstage files: ``` git restore --staged <file> # or discard all staged changes in directory git restore --staged . ``` --- ## Amend commit 1. Change commit message: `git commit --amend -m "New message"` 2. Add forgotten files/changes: stage the changes (`git add <file>`), then run `git commit --amend --no-edit` - Then run: `git show` or `git show --name-only --pretty=""` --- <!-- .slide: style="font-size: 0.90em;" --> ## Exercise 1. Create RR_git repository 2. Add sample txt file and commit 3. Create simple txt file and .env file with "SECRET=123" 4. Stage changes 5. Unstage .env 6. Create .gitignore file that ignores only .env file 7. Stage all the files 8. Check git status 9. Commit changes 10. Change the commit message for the last commit with git amend --- <!-- .slide: style="font-size: 0.75em;" --> ## Git diff 1. `git diff`: working directory vs index 2. `git diff --staged`: staging vs HEAD 3. `git diff HEAD`: working directory vs HEAD More specific diffs: 1. `git diff commit1 commit2` 2. `git diff branch1 branch2` 3. `git diff --color-words`: side by side diff 4. `git diff main...feature-branch`: compares what feature branch changed since it split from main (merge-base). --- <!-- .slide: style="font-size: 0.75em;" --> ## Revert commit vs "go back in time" 1. Keep history (safest): `git revert HEAD` - Creates a new commit that undoes the last commit. - You can revert a range of commits: `git revert HEAD~2..HEAD` (creates three new commits each undoing a specific commit for the past) 2. Go back one commit (keep changes staged): `git reset --soft HEAD~1` 3. Go back One commit (keep changes unstaged/in a working directory): `git reset --mixed HEAD~1` 4. Permanently delete the last commit and all changes: `git reset --hard HEAD~1` Important: git reset rewrites history, git revert does not. Use revert for collaboration and reset for local cleanup. --- <div style="transform: scale(0.80); transform-origin: top;"> ## git reset ![image](https://hackmd.io/_uploads/ryXuL1HKbx.png) [Source](https://www.scaler.com/topics/git/git-revert-checkout-reset/) </div> --- ## Exercise 1. Open RR_git repository 2. Create sample txt file 3. Stage and commit 4. Create second sample txt file 3. Stage and commit 4. Go back to the first commit with `git reset --soft HEAD~2` 5. Check diff between index and HEAD with `git diff --staged` --- <div style="transform: scale(0.65); transform-origin: top;"> ## git restore (old: checkout), revert and reset ![image](https://hackmd.io/_uploads/HJ-SDySFZe.png) [Source](https://www.scaler.com/topics/git/git-revert-checkout-reset/) </div> --- ## Commits best practices --- ## Keep your commits atomic Don't run `git add .` if you have worked on many different things. --- ## Bigger commits: 50/72 rule 1. The first line of your commit message must be maximum 50 characters long. No more, and (ideally), no less. 2. Leave a blank line 3. Start writing your description. The description can be as verbose as it suits you. Each line in your description should though wrap at the 72nd mark. Try `git shortlog` and `git show [hash]` [Source](https://preslav.me/2015/02/21/what-s-with-the-50-72-rule/) --- ## Example <!-- .slide: style="font-size: 0.85em;" --> ``` Add user authentication feature - Implemented user login functionality - Added password hashing using bcrypt - Integrated user session management - Updated tests to cover new authentication logic This commit introduces a new user authentication feature, allowing users to securely log in and manage their sessions. The changes include implementing the backend logic for handling user credentials, encrypting passwords, and maintaining session data. Additionally, tests have been updated to ensure the new functionality is properly covered. ``` [Source](https://deviq.com/practices/50-72-rule) --- ## Imperative or past tense? 1. [Example](https://medium.com/@corrodedlotus/which-tense-should-be-used-on-a-git-commit-message-121cb641134b) 2. [Discussion](https://stackoverflow.com/questions/3580013/should-i-use-past-or-present-tense-in-git-commit-messages) --- <!-- .slide: style="font-size: 0.75em;" --> ## Be specific ## ``` 6904a37 adds test (4 hours ago) <justin> ``` ``` 6904a37 test summary_client rate limit backoff behavior (4 hours ago) <justin> ``` [Source](https://justinjoyce.dev/git-commit-and-commit-message-best-practices/) --- <!-- .slide: style="font-size: 0.90em;" --> ## Message body should explain ["why"](https://victoria.dev/posts/building-code-quality-culture-through-commit-standards/) the change was implemented ``` ## If applied, this commit will... ## [Add/Fix/Remove/Update/Refactor/Document] [issue #id] [summary] ## Why is it necessary? (Bug fix, feature, improvements?) - ## How does the change address the issue? - ## What side effects does this change have? - ``` `git config --local commit.template ~/.gitmessage` `git config --local commit.status false` --- ## Conventional Commits 1. [Cheatsheet](https://gist.github.com/qoomon/5dfcdf8eec66a051ecd85625518cfd13) 2. [Example](https://www.conventionalcommits.org/en/v1.0.0/) ``` feat(payment): implement PayPal integration Add PayPal as a payment option for checkout process. This change includes: - PayPal SDK integration - Payment verification flow - Success/failure handling BREAKING CHANGE: Updates payment processor interface Closes #123 ``` [Source](https://dev.to/sivantha96/the-art-of-git-commits-writing-messages-that-your-future-self-will-thank-you-for-5bp7)
{"title":"Commits","description":"git check-ignore -v path/to/file","contributors":"[{\"id\":\"9b02b446-e66a-4b87-ad0b-21574e73f20f\",\"add\":16590,\"del\":10432,\"latestUpdatedAt\":1773620862730}]"}
    123 views