|
10 | 10 | - [Git HTTPS Authentication](#git-https-authentication)
|
11 | 11 | - [CLIs](#clis)
|
12 | 12 | - [GitHub Badges](#github-badges)
|
13 |
| -- [Git LFS](#git-lfs) |
14 |
| - - [Why You Need Git LFS for Large Files](#why-you-need-git-lfs-for-large-files) |
15 |
| - - [Git LFS on other hosting providers](#git-lfs-on-other-hosting-providers) |
16 |
| -- [Tips & Tricks](#tips--tricks) |
| 13 | +- [Basic Tips](#basic-tips) |
| 14 | +- [Advanced Tips & Tricks](#advanced-tips--tricks) |
17 | 15 | - [Git Clone using a specific SSH Key](#git-clone-using-a-specific-ssh-key)
|
18 | 16 | - [Show files not being tracked due to global & local `.gitignore` files](#show-files-not-being-tracked-due-to-global--local-gitignore-files)
|
19 | 17 | - [Find line in `.gitignore` which is causing a given file to be ignored](#find-line-in-gitignore-which-is-causing-a-given-file-to-be-ignored)
|
|
40 | 38 | - [Push New Branch and Raise Pull Request in One Command](#push-new-branch-and-raise-pull-request-in-one-command)
|
41 | 39 | - [On GitHub](#on-github)
|
42 | 40 | - [On GitLab](#on-gitlab)
|
| 41 | +- [Git LFS](#git-lfs) |
| 42 | + - [Why You Need Git LFS for Large Files](#why-you-need-git-lfs-for-large-files) |
| 43 | + - [Git LFS on other hosting providers](#git-lfs-on-other-hosting-providers) |
43 | 44 |
|
44 | 45 | <!-- INDEX_END -->
|
45 | 46 |
|
@@ -132,143 +133,45 @@ pip install --user bitbucket-cli
|
132 | 133 |
|
133 | 134 | <https://github.com/commonality/architecture-decision-records/wiki/GitHub-repository-status-badges>
|
134 | 135 |
|
135 |
| -## Git LFS |
136 |
| - |
137 |
| -<https://git-lfs.com/> |
138 |
| - |
139 |
| -Store big files in GitHub repos (1GB limit for free accounts). |
140 |
| - |
141 |
| -I used this to store old training materials like videos, PDFs, zip files etc. so they are safe and I can then save space locally. |
142 |
| - |
143 |
| -GitHub will block files over 100MB from being `git push` otherwise. |
144 |
| - |
145 |
| -Install Git LFS extension on Mac: |
146 |
| - |
147 |
| -```shell |
148 |
| -brew install git-lfs |
149 |
| -``` |
150 |
| - |
151 |
| -Install the Git LFS config into your `$HOME/.gitconfig`: |
152 |
| - |
153 |
| -```shell |
154 |
| -git lfs install |
155 |
| -``` |
156 |
| - |
157 |
| -In your repo add the big file types to be tracked in your local repo (configures `.gitattributes`): |
158 |
| - |
159 |
| -```shell |
160 |
| -git lfs track '*.mp4' |
161 |
| -``` |
162 |
| - |
163 |
| -Commit the `.gitattributes` changes: |
164 |
| - |
165 |
| -```shell |
166 |
| -git add .gitattributes |
167 |
| -git commit -m "add mp4 file type to be tracked by Git LFS in .gitattributes" |
168 |
| -``` |
169 |
| - |
170 |
| -Override global extensive [.gitignore](https://github.com/HariSekhon/DevOps-Bash-tools/blob/master/.gitignore) |
171 |
| -if you've copied it from or installed [DevOps-Bash-tools](https://github.com/HariSekhon/DevOps-Bash-tools#readme) using a local repo `.gitignore`. |
172 |
| - |
173 |
| -See [files being ignored](#show-files-not-being-tracked-due-to-global--local-gitignore-files). |
174 |
| - |
175 |
| -```shell |
176 |
| -echo '!*.mp4' >> .gitignore |
177 |
| -git commit -m "allowed .mp4 files to be git committed in .gitignore" |
178 |
| -``` |
179 |
| - |
180 |
| -```shell |
181 |
| -git add *.mp4 |
182 |
| -``` |
183 |
| - |
184 |
| -```shell |
185 |
| -git commit -m "added mp4 videos" |
186 |
| -``` |
187 |
| - |
188 |
| -Automatically uploads the files to GitHub or whatever is configured as your upstream Git server using LFS storage: |
189 |
| - |
190 |
| -```shell |
191 |
| -git push |
192 |
| -``` |
193 |
| - |
194 |
| -See LFS details: |
195 |
| - |
196 |
| -```shell |
197 |
| -git lfs env |
198 |
| -``` |
199 |
| - |
200 |
| -On another computer you must install Git LFS before you clone. |
201 |
| - |
202 |
| -Otherwise you'll get a checkout with 4K pointer files instead of the actual file contents because Git LFS smudges the checkout to |
203 |
| -download the file blobs to replace in your checkout. |
204 |
| - |
205 |
| -### Why You Need Git LFS for Large Files |
206 |
| - |
207 |
| -You'll get errors like this trying to push large files to GitHub: |
208 |
| - |
209 |
| -```shell |
210 |
| -$ git push -u origin master |
211 |
| -Enumerating objects: 52, done. |
212 |
| -Counting objects: 100% (52/52), done. |
213 |
| -Delta compression using up to 4 threads |
214 |
| -Compressing objects: 100% (52/52), done. |
215 |
| -Writing objects: 100% (52/52), 584.76 MiB | 2.05 MiB/s, done. |
216 |
| -Total 52 (delta 0), reused 0 (delta 0) |
217 |
| -remote: warning: File COURSE.pdf is 69.89 MB; this is larger than GitHub's recommended maximum file size of 50.00 MB |
218 |
| -remote: warning: File COURSE2.pdf is 64.61 MB; this is larger than GitHub's recommended maximum file size of 50.00 MB |
219 |
| -remote: error: Trace: 98e304dad85b91bcb5f726886ddce1b51a5b450523fc93f44ea51e64b444b69c |
220 |
| -remote: error: See https://gh.io/lfs for more information. |
221 |
| -remote: error: File COURSE.mp4 is 100.27 MB; this exceeds GitHub's file size limit of 100.00 MB |
222 |
| -remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com. |
223 |
| -To github.com:HariSekhon/training-old.git |
224 |
| - ! [remote rejected] master -> master (pre-receive hook declined) |
225 |
| -error: failed to push some refs to '[email protected]:HariSekhon/training-old.git ' |
226 |
| -``` |
227 |
| -
|
228 |
| -### Git LFS on other hosting providers |
229 |
| -
|
230 |
| -GitLab works fine for both push and clone. |
231 |
| -
|
232 |
| -Azure DevOps seems to work for push but doesn't show the file contents in the web UI preview and cloning resulted in a checkout error after clone and neither `git restore --source=HEAD :/` nor `git reset HEAD --hard` worked to get Git LFS to smudge and download the large files. |
233 |
| - |
234 |
| -Unfortunately GitHub limits large files to only 1GB for free accounts, so only use it for files over 100MB. GitHub will disable your LFS after you go over the thresholds for storage or bandwidth usage for the month. |
235 |
| - |
236 |
| -Bitbucket is useless because the [free tier](https://bitbucket.org/harisekhon/workspace/settings/plans) only gives 1GB storage (use GitHub instead): |
237 |
| - |
238 |
| -```shell |
239 |
| -$ git push bitbucket |
240 |
| -Uploading LFS objects: 0% (0/51), 0 B | 0 B/s, done. |
241 |
| -batch response: |
242 |
| -******************************************************************************** |
243 |
| -[ERROR] Your LFS push failed because you're out of file storage |
244 |
| -[ERROR] Change your plan to get more file storage: |
245 |
| -[ERROR] https://bitbucket.org/account/user/harisekhon/plans |
246 |
| -******************************************************************************** |
247 |
| -
|
248 |
| -error: failed to push some refs to '[email protected]:HariSekhon/training-old.git' |
249 |
| -``` |
250 |
| -
|
251 |
| -Bitbucket also requires disabling locking: |
252 |
| -
|
253 |
| -```shell |
254 |
| -git config lfs.https://ssh.dev.azure.com/v3/<user>/<project>/<repo>.git/info/lfs.locksverify false |
255 |
| -``` |
256 |
| -
|
257 |
| -[Multi-origin](#multi-origin-remotes) pushes fail and require individual remote pushes: |
258 |
| -
|
259 |
| -```shell |
260 |
| -$ git push |
261 |
| -... |
262 |
| -remote: GitLab: LFS objects are missing. Ensure LFS is properly set up or try a manual "git lfs push --all". |
263 |
| -To gitlab.com:HariSekhon/training-old.git |
264 |
| - ! [remote rejected] master -> master (pre-receive hook declined) |
265 |
| -error: failed to push some refs to '[email protected]:HariSekhon/training-old.git' |
266 |
| -... |
267 |
| -
|
268 |
| -$ git push gitlab |
269 |
| -``` |
270 |
| -
|
271 |
| -## Tips & Tricks |
| 136 | +## Basic Tips |
| 137 | + |
| 138 | +- Commit frequently: |
| 139 | + - make small, atomic commits for easier tracking and diffs |
| 140 | +- Write clear commit messages: |
| 141 | + - summarize what changed and more importantly why |
| 142 | + - some companies prefix Jira ticket numbers to their commits or Pull Requests |
| 143 | + - Jira can be configured to automatically link a field in each ticket to such PRs |
| 144 | +- Use Comments to explain any clever tricks in code: |
| 145 | + - it's better than `git blame` which may get covered up on subsequent edits |
| 146 | +- Use branches: |
| 147 | + - isolate new features, bug fixes, or experiments |
| 148 | + - see [Git Workflow Branching Strategies](git-workflow-branching-strategies.md) |
| 149 | + - TL;DR just use simple [GitHub flow](https://docs.github.com/en/get-started/using-github/github-flow) to get started, you can change it later |
| 150 | +- Keep branches short-lived: |
| 151 | + - merge back to the main branch quickly to avoid large hard to review Pull Requests or merge conflicts |
| 152 | + - see [Why You Shouldn't Use Long Lived Branches](git-workflow-branching-strategies.md#why-you-shouldnt-use-long-lived-feature-branches) |
| 153 | +- Pull and merge to your branch frequently: |
| 154 | + - this minimizes merge conflicts |
| 155 | + - resolve merge conflicts early |
| 156 | + - they will get bigger over time if you don't merge and resolve conflicts reguarly |
| 157 | +- Don't commit large files |
| 158 | + - it'll slow down your repo cloning and local checkout size |
| 159 | + - use [.gitignore](https://github.com/HariSekhon/DevOps-Bash-tools/blob/master/.gitignore) |
| 160 | + to exclude unnecessary files like large or binary files |
| 161 | + - large files are better handled in external blob storage |
| 162 | + - if you really need large files in Git, see [Git LFS](#git-lfs) further down |
| 163 | +- Don't Rebase - read [The Evils of Git Rebasing](git-workflow-branching-strategies.md#the-evils-of-rebasing) |
| 164 | + - it's not the default for good reason, it causes more problems |
| 165 | +- Use tags for releases: |
| 166 | + - mark important commits like version, stable releases |
| 167 | + - before and after big refactors |
| 168 | +- Use Pull Requests for better tracking |
| 169 | +- Use Branch protections to: |
| 170 | + - ensure your important branches like trunk / master / main don't get accidentally deleted |
| 171 | + - enforce code peer reviews on Pull Requests before merging |
| 172 | +- Push frequently to your remote origin as a backup in case you have a local disaster |
| 173 | + |
| 174 | +## Advanced Tips & Tricks |
272 | 175 |
|
273 | 176 | ### Git Clone using a specific SSH Key
|
274 | 177 |
|
|
642 | 545 | pushup # to push and raise the PR
|
643 | 546 | ```
|
644 | 547 |
|
| 548 | +## Git LFS |
| 549 | + |
| 550 | +<https://git-lfs.com/> |
| 551 | + |
| 552 | +Store big files in GitHub repos (1GB limit for free accounts). |
| 553 | + |
| 554 | +I used this to store old training materials like videos, PDFs, zip files etc. so they are safe and I can then save space locally. |
| 555 | + |
| 556 | +GitHub will block files over 100MB from being `git push` otherwise. |
| 557 | + |
| 558 | +Install Git LFS extension on Mac: |
| 559 | + |
| 560 | +```shell |
| 561 | +brew install git-lfs |
| 562 | +``` |
| 563 | + |
| 564 | +Install the Git LFS config into your `$HOME/.gitconfig`: |
| 565 | + |
| 566 | +```shell |
| 567 | +git lfs install |
| 568 | +``` |
| 569 | + |
| 570 | +In your repo add the big file types to be tracked in your local repo (configures `.gitattributes`): |
| 571 | + |
| 572 | +```shell |
| 573 | +git lfs track '*.mp4' |
| 574 | +``` |
| 575 | + |
| 576 | +Commit the `.gitattributes` changes: |
| 577 | + |
| 578 | +```shell |
| 579 | +git add .gitattributes |
| 580 | +git commit -m "add mp4 file type to be tracked by Git LFS in .gitattributes" |
| 581 | +``` |
| 582 | + |
| 583 | +Override global extensive [.gitignore](https://github.com/HariSekhon/DevOps-Bash-tools/blob/master/.gitignore) |
| 584 | +if you've copied it from or installed [DevOps-Bash-tools](https://github.com/HariSekhon/DevOps-Bash-tools#readme) using a local repo `.gitignore`. |
| 585 | + |
| 586 | +See [files being ignored](#show-files-not-being-tracked-due-to-global--local-gitignore-files). |
| 587 | + |
| 588 | +```shell |
| 589 | +echo '!*.mp4' >> .gitignore |
| 590 | +git commit -m "allowed .mp4 files to be git committed in .gitignore" |
| 591 | +``` |
| 592 | + |
| 593 | +```shell |
| 594 | +git add *.mp4 |
| 595 | +``` |
| 596 | + |
| 597 | +```shell |
| 598 | +git commit -m "added mp4 videos" |
| 599 | +``` |
| 600 | + |
| 601 | +Automatically uploads the files to GitHub or whatever is configured as your upstream Git server using LFS storage: |
| 602 | + |
| 603 | +```shell |
| 604 | +git push |
| 605 | +``` |
| 606 | + |
| 607 | +See LFS details: |
| 608 | + |
| 609 | +```shell |
| 610 | +git lfs env |
| 611 | +``` |
| 612 | + |
| 613 | +On another computer you must install Git LFS before you clone. |
| 614 | + |
| 615 | +Otherwise you'll get a checkout with 4K pointer files instead of the actual file contents because Git LFS smudges the checkout to |
| 616 | +download the file blobs to replace in your checkout. |
| 617 | + |
| 618 | +### Why You Need Git LFS for Large Files |
| 619 | + |
| 620 | +You'll get errors like this trying to push large files to GitHub: |
| 621 | + |
| 622 | +```shell |
| 623 | +$ git push -u origin master |
| 624 | +Enumerating objects: 52, done. |
| 625 | +Counting objects: 100% (52/52), done. |
| 626 | +Delta compression using up to 4 threads |
| 627 | +Compressing objects: 100% (52/52), done. |
| 628 | +Writing objects: 100% (52/52), 584.76 MiB | 2.05 MiB/s, done. |
| 629 | +Total 52 (delta 0), reused 0 (delta 0) |
| 630 | +remote: warning: File COURSE.pdf is 69.89 MB; this is larger than GitHub's recommended maximum file size of 50.00 MB |
| 631 | +remote: warning: File COURSE2.pdf is 64.61 MB; this is larger than GitHub's recommended maximum file size of 50.00 MB |
| 632 | +remote: error: Trace: 98e304dad85b91bcb5f726886ddce1b51a5b450523fc93f44ea51e64b444b69c |
| 633 | +remote: error: See https://gh.io/lfs for more information. |
| 634 | +remote: error: File COURSE.mp4 is 100.27 MB; this exceeds GitHub's file size limit of 100.00 MB |
| 635 | +remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com. |
| 636 | +To github.com:HariSekhon/training-old.git |
| 637 | + ! [remote rejected] master -> master (pre-receive hook declined) |
| 638 | +error: failed to push some refs to '[email protected]:HariSekhon/training-old.git ' |
| 639 | +``` |
| 640 | +
|
| 641 | +### Git LFS on other hosting providers |
| 642 | +
|
| 643 | +GitLab works fine for both push and clone. |
| 644 | +
|
| 645 | +Azure DevOps seems to work for push but doesn't show the file contents in the web UI preview and cloning resulted in a checkout error after clone and neither `git restore --source=HEAD :/` nor `git reset HEAD --hard` worked to get Git LFS to smudge and download the large files. |
| 646 | + |
| 647 | +Unfortunately GitHub limits large files to only 1GB for free accounts, so only use it for files over 100MB. GitHub will disable your LFS after you go over the thresholds for storage or bandwidth usage for the month. |
| 648 | + |
| 649 | +Bitbucket is useless because the [free tier](https://bitbucket.org/harisekhon/workspace/settings/plans) only gives 1GB storage (use GitHub instead): |
| 650 | + |
| 651 | +```shell |
| 652 | +$ git push bitbucket |
| 653 | +Uploading LFS objects: 0% (0/51), 0 B | 0 B/s, done. |
| 654 | +batch response: |
| 655 | +******************************************************************************** |
| 656 | +[ERROR] Your LFS push failed because you're out of file storage |
| 657 | +[ERROR] Change your plan to get more file storage: |
| 658 | +[ERROR] https://bitbucket.org/account/user/harisekhon/plans |
| 659 | +******************************************************************************** |
| 660 | +
|
| 661 | +error: failed to push some refs to '[email protected]:HariSekhon/training-old.git' |
| 662 | +``` |
| 663 | +
|
| 664 | +Bitbucket also requires disabling locking: |
| 665 | +
|
| 666 | +```shell |
| 667 | +git config lfs.https://ssh.dev.azure.com/v3/<user>/<project>/<repo>.git/info/lfs.locksverify false |
| 668 | +``` |
| 669 | +
|
| 670 | +[Multi-origin](#multi-origin-remotes) pushes fail and require individual remote pushes: |
| 671 | +
|
| 672 | +```shell |
| 673 | +$ git push |
| 674 | +... |
| 675 | +remote: GitLab: LFS objects are missing. Ensure LFS is properly set up or try a manual "git lfs push --all". |
| 676 | +To gitlab.com:HariSekhon/training-old.git |
| 677 | + ! [remote rejected] master -> master (pre-receive hook declined) |
| 678 | +error: failed to push some refs to '[email protected]:HariSekhon/training-old.git' |
| 679 | +... |
| 680 | +
|
| 681 | +$ git push gitlab |
| 682 | +``` |
| 683 | +
|
645 | 684 | **Partial port from private Knowledge Base page 2012+**
|
0 commit comments