|
| 1 | +# Using Git in our daily workflow |
| 2 | + |
| 3 | +Git is an essential tool in our day-to-day coding life. Below you will find Git hints and best |
| 4 | +practices and conventions that we aim to follow at ML6. |
| 5 | + |
| 6 | + |
| 7 | +## Naming conventions |
| 8 | + |
| 9 | +* Always start development with creating a new branch. |
| 10 | +* Name the branch according to our conventions. |
| 11 | +* Use feature/ prefix for a new feature and `bugfix/` for fixing a bug. |
| 12 | +* Separate the words in the branch name with dash. |
| 13 | + |
| 14 | +##### Examples: |
| 15 | + |
| 16 | +* `feature/docs` |
| 17 | +* `bugfix/data-pipeline` |
| 18 | + |
| 19 | + |
| 20 | +## Commit messages |
| 21 | + |
| 22 | +* Write clear commit messages. A commit message should be a short summary of your changes. |
| 23 | +* Use imperative, present tense statements (e.g. 'change', and not |
| 24 | +'changed' or 'changes'). |
| 25 | +* Try to limit the message to about 50 characters or less to keep it clear and readable. |
| 26 | + |
| 27 | +#### Examples: |
| 28 | + |
| 29 | +* `Add io module documentation` |
| 30 | +* `Delete preprocessing step in data pipeline` |
| 31 | + |
| 32 | + |
| 33 | +## Clean commit history |
| 34 | + |
| 35 | +* Only commit the code you tested. |
| 36 | +* Make sure the change is a logical chunk that is completed and has no side effects. |
| 37 | +* Keep separate parts of your change in separate commits. For example, fixing two different bugs |
| 38 | +should produce two separate commits. This will make your commit history clean and structured. |
| 39 | +In addition, it would be easier to possibly roll back if something goes wrong, and will also help |
| 40 | +reviewers to understand the changes faster. |
| 41 | +* Before merging, small commits representing intermediate steps in context of a bigger change or |
| 42 | +made in response to PR comments can be cleaned up by squashing them. |
| 43 | + |
| 44 | +### Some useful Git commands |
| 45 | + |
| 46 | +#### `git commit` |
| 47 | + |
| 48 | +* `--amend` |
| 49 | +Replaces the last commit done with a new one. |
| 50 | + |
| 51 | +> Use the `--no-edit` flag if you want to keep the commit message. |
| 52 | +
|
| 53 | +> Also make sure to to check what your last commit was before amending. |
| 54 | +
|
| 55 | +* `--fixup HASH` |
| 56 | +Creates special commit with prefix linked to the specified commit. This commit can be used in |
| 57 | +combination with `rebase --autosquash` to correct past commits. |
| 58 | + |
| 59 | +#### `git rebase` |
| 60 | + |
| 61 | +* `--onto` |
| 62 | +Allows the user to specify the starting point of the rebase. |
| 63 | + |
| 64 | +* `-i` |
| 65 | +Interactive rebase - allows the user the select which commits to rebase. |
| 66 | + |
| 67 | +* `--autosquash` |
| 68 | +Can only be used with the interactive rebase. Will automatically squash commits with the prefix |
| 69 | +`squash!` or `fixup!` with linked list while rebasing. |
| 70 | + |
| 71 | +> Pushing to remote: don’t forget that the above commands changes the commit. This means that to push |
| 72 | +it to a remote branch you’ll need to use the `--force` flag or `--force-with-lease` flag. |
| 73 | +`--force-with-lease` is a safer option than `--force` as it prevents overwriting the remote if |
| 74 | +additional commits have been added to the branch. |
| 75 | + |
| 76 | +##### Example: drop commit with `git rebase -i` |
| 77 | +1. |
| 78 | +```bash |
| 79 | + C -- D -- E (feature/my-branch, HEAD) |
| 80 | + / |
| 81 | +A -- B (main) |
| 82 | +``` |
| 83 | + |
| 84 | +2. `git rebase -i main` |
| 85 | + |
| 86 | +```bash |
| 87 | +pick 4e37e38 commit C |
| 88 | +# pick c78980a commit D |
| 89 | +pick fbcfda8 commit E |
| 90 | +``` |
| 91 | + |
| 92 | +3. |
| 93 | +```bash |
| 94 | + C’ -- E’ (feature/my-branch, HEAD) |
| 95 | + / |
| 96 | +A -- B (main) |
| 97 | + |
| 98 | +``` |
| 99 | + |
| 100 | +##### Example: `git commit fixup` + `git rebase` |
| 101 | +1. `A -- B -- C -- D -- E (HEAD)` |
| 102 | +2. `A -- B -- C -- D -- E -- F (HEAD)` |
| 103 | +3. `A -- B’ -- C -- D -- E (HEAD)` |
| 104 | + |
| 105 | +#### `reset` vs `reverse` |
| 106 | + |
| 107 | +* `git reset HASH` or `git reset HEAD~N` |
| 108 | +Undo local changes by moving pointer back to the specific commit. This means that it changes the |
| 109 | +commit history and is not recommend to be used for public commits. |
| 110 | + |
| 111 | +`--mixed` (default option) makes it so that only the pointer is moved back. This can be used to |
| 112 | +squash your latest commits. |
| 113 | + |
| 114 | +`--hard` flag makes it so that the working directory is also updated. |
| 115 | + |
| 116 | +* `git revert HASH` or `git revert HEAD~N` |
| 117 | +Undo changes by adding a new commit that undoes all changes. This means that it does not change the |
| 118 | +commit history and is the recommended way of correcting public commits. |
| 119 | + |
| 120 | +> Both of these commands can also be executed on a file level. |
| 121 | +
|
| 122 | +#### `git rev-list HASH..HEAD` |
| 123 | + |
| 124 | +Lists commit objects in reverse chronological order since commit hash. |
| 125 | + |
| 126 | + |
| 127 | +## Useful resources |
| 128 | + |
| 129 | +Want some tips on how to fix your Git mistakes and keep your commit history clean? |
| 130 | +Check [ohshitgit](https://ohshitgit.com). |
0 commit comments