{"id":16113008,"url":"https://github.com/guilospanck/git-hacks","last_synced_at":"2025-04-06T07:24:03.505Z","repository":{"id":103566902,"uuid":"526234534","full_name":"Guilospanck/git-hacks","owner":"Guilospanck","description":"Some git hacks that everyone should know at some point in their dev lives ","archived":false,"fork":false,"pushed_at":"2022-08-19T16:54:30.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-12T12:55:46.605Z","etag":null,"topics":["git"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Guilospanck.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-18T13:58:29.000Z","updated_at":"2022-08-23T10:34:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"4357a6cc-7e9e-423c-975d-debabc8d4f77","html_url":"https://github.com/Guilospanck/git-hacks","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Guilospanck%2Fgit-hacks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Guilospanck%2Fgit-hacks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Guilospanck%2Fgit-hacks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Guilospanck%2Fgit-hacks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Guilospanck","download_url":"https://codeload.github.com/Guilospanck/git-hacks/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247446875,"owners_count":20940201,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["git"],"created_at":"2024-10-09T20:09:57.473Z","updated_at":"2025-04-06T07:24:03.434Z","avatar_url":"https://github.com/Guilospanck.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Some Git-related hacks for the daily struggles of the developer\n\n[Git PRO related site](https://about.gitlab.com/blog/2018/06/07/keeping-git-commit-history-clean/)\n\n## Git commits\nGit commits are the best friends of developers, either they are working with others or not.\nGit commits must follow a pattern and must be \"atomic\".\n\nSome rules of thumb:\n- Separate subject from body with a blank line\n- Limit the subject line to 50 characters\n- Capitalize the subject line\n- Do not end the subject line with a period\n- Use the imperative mood in the subject line (clean your room, close the door, take out the trash)\n    ```txt\n    if applied, this commit will ________\n    Examples:\n    if applied, this commit will clean your room\n    if applied, this commit will close the door\n    if applied, this commit will take out the trash\n    ```\n    Obs 👉🏻: Remember: Use of the imperative is important only in the subject line. You can relax this restriction when you’re writing the body.\n- Wrap the body at 72 characters\n- Use the body to explain what and why vs. how\n\n## How to git rebase\nFirst, verify your `git logs`:\n```bash\ngit log --all --decorate --oneline --graph \n```\n\nThen, you need to select the commit ID right before the commit you want to change.\n\u003cimg src=\"./docs/img/GitRebase.png\" align=\"center\"\u003e\n\u003ccaption\u003eIn the image above, we want to change the commit \"Page Navigation View\", then we need to rebase to the commit \"Add routes for navigation\" which is the commit right before.\u003c/caption\u003e\n\nAfter that, do:\n```bash\ngit rebase -i \u003ccommit-id\u003e\n```\nThis will launch your default editor (vi/vim/nano) and you'll be presented with some options that look like the ones below:\n```bash\npick 4155df1cdc7 Page Navigation View\npick c22a3fa0c5c Render navigation partial\npick aa0a35a867e Add styles for navigation\n\n# Rebase 8d74af10294..aa0a35a867e onto 8d74af10294 (3 commands)\n#\n# Commands:\n# p, pick = use commit\n# r, reword = use commit, but edit the commit message\n# e, edit = use commit, but stop for amending\n# s, squash = use commit, but meld into previous commit\n# f, fixup = like \"squash\", but discard this commit's log message\n# x, exec = run command (the rest of the line) using shell\n# d, drop = remove Git commit\n#\n# These lines can be re-ordered; they are executed from top to bottom.\n#\n# If you remove a line here THAT COMMIT WILL BE LOST.\n#\n# However, if you remove everything, the rebase will be aborted.\n#\n# Note that empty commits are commented out\n```\nChange from \"pick\" to \"edit\" for the commit you want to change. In our case, it'd be:\n```bash\npick 4155df1cdc7 Page Navigation View  -\u003e edit 4155df1cdc7 Page Navigation View\n```\n\n\u003cquote\u003eTip 💡: In `vi` you can edit a file by pressing `i`. When you've edited what you wanted, press `ESC`. To quit and save the file, `wq`.\u003c/quote\u003e\n\nAfter that,\n```bash\ngit add .\ngit rebase --continue\n```\n\nIt'll ask for some merge changes. Select the ones you want to maintain (fix merge issues) and then keep doing `git rebase --continue` until you find yourself at the HEAD.\nWhen you're there:\n```bash\ngit push --force-with-lease\n```\n\n## How to git squash/fixup\nIt's mainly used to combine commits.\n\nThis is usual when we do a lot of \"fix\" git commit messages and then it becomes a mess.\nStart by calling our well-known `git rebase -i` to some commit-id and then change from \n\"pick\" to either \"squash\" or \"fixup\".\n\nThe difference between them are:\n- `squash`: keeps the gix fix commit messages in the description.\n- `fixup`: forget the commit messages of the fixes and keep the original.\n\nFirst get the commit-id you want to rebase to:\n```bash\n git log --all --decorate --oneline --graph\n git rebase -i \u003ccommit-id\u003e\n```\nExample:\n```txt\npick 4155df1cdc7 Page Navigation View\npick c22a3fa0c5c Render navigation partial\npick aa0a35a867e Add styles for navigation\npick 62e858a322 Fix a typo\npick 5c25eb48c8 Ops another fix\npick 7f0718efe9 Fix 2\npick f0ffc19ef7 Argh Another fix!\n```\n\nNow if you wanna combine the fixes into `c22a3fa0c5c Render navigation partial`, you should:\n- move the fixes up so that they are right below the commit you want to keep in the end;\n- change `pick` to `squash` or `fixup` for each of the fixes.\n\nAnd then you'll end up with something like:\n```txt\npick 4155df1cdc7 Page Navigation View\npick c22a3fa0c5c Render navigation partial\nfixup 62e858a322 Fix a typo\nfixup 5c25eb48c8 Ops another fix\nfixup 7f0718efe9 Fix 2\nfixup f0ffc19ef7 Argh Another fix!\npick aa0a35a867e Add styles for navigation\n```\n\nSave the changes and then `git push --force-with-lease`.\n\nYou'll end up with:\n```txt\npick 4155df1cdc7 Page Navigation View\npick 96373c0bcf Render navigation partial\npick aa0a35a867e Add styles for navigation\n```\n\n## Restarting a git commit history\nWhenever we're working on, usually, a big project, there are some times that our git commit messages don't\nmake sense.\nIf you want to start from scratch while maintaning all your code, you can create a `patch`.\n\nBasically a `patch` is a box containing all your code, but that can be transported to a new location.\n\nImagine you have a branch called `add-page-navigation`. Let's see the step-by-step to do that:\n\n1) Making sure your branch has all the changes present in the `master` and has no conflict with the same.\n    - `git rebase master` while you're in `add-page-navigation` to get all the changes from `master` to your branch;\n\n2) Creating the `patch` file:\n    ```bash\n    git diff master add-page-navigation \u003e ~/add_page_navigation.patch\n    ```\n    Once the command is run and you don't see any errors, the patch file is generated.\n\n3) Deleting the `add-page-navigation` branch:\n    - check out to the `master` branch: `git checkout master`;\n    - delete the `add-page-navigation` branch: `git branch -D add-page-navigation`;\n\n4) Creating a new branch with the same name:\n    - while in the `master` branch, run `git checkout -b add-page-navigation`.\n    At this point this is a fresh new branch with no differences from `master` branch\n\n5) Finally, apply your changes from the patch file:\n    - while in the new created branch, run `git apply ~/add_page_navigation.patch`.\n\n6) Now commit each part of the code as you want.\n    \n    \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguilospanck%2Fgit-hacks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fguilospanck%2Fgit-hacks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguilospanck%2Fgit-hacks/lists"}