Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions Mastering-Versioning/Git-Best-Practices-Adoption.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ In this lab you will:
just use `cd`:

```console
~ $ cd myrepo
(no output)

~/myrepo $ git status
$ cd /git/myrepo && git status
On branch main
Your branch is up to date with 'origin/main'.

Expand All @@ -34,8 +31,8 @@ In this lab you will:
The `commit-msg` Git hook will be:

```console
~/myrepo $ cat <<EOF > .git/hooks/commit-msg
grep -q '^feat: \|^bug: ' \$1 &> /dev/null
$ cat <<EOF > .git/hooks/commit-msg
echo "\$1" | grep -q '^feat: \|^bug: ' &> /dev/null
if [ \$? -ne 0 ]
then
echo "[ERROR] Commit must start with 'feat:' or 'bug:'!"
Expand All @@ -47,17 +44,17 @@ In this lab you will:
And must be executable:

```console
~/myrepo $ chmod -v +x .git/hooks/commit-msg
$ chmod -v +x .git/hooks/commit-msg
mode of '.git/hooks/commit-msg' changed to 0755 (rwxr-xr-x)
```

3. Removing SSH keys from tracking is just a matter of adding `id_rsa*` into
`.gitignore` and commit the modification:

```console
~/myrepo $ echo 'id_rsa*' >> .gitignore
$ echo 'id_rsa*' >> .gitignore

~/myrepo $ git add .gitignore && git commit -m "feat: ignore id_rsa key files" -m "This applies the best practice to exclude SSH private and public keys."
$ git add .gitignore && git commit -m "feat: ignore id_rsa key files" -m "This applies the best practice to exclude SSH private and public keys."
[main 978602b] feat: ignore id_rsa key files
1 file changed, 1 insertion(+)
```
Expand All @@ -80,29 +77,29 @@ In this lab you will:
Then Git LFS can be enabled with the `git lfs install` command:

```console
~/myrepo $ git lfs install
$ git lfs install
Updated Git hooks.
Git LFS initialized.
```

Then to track the `*.zip` files `git lfs track` should be used as follows:

```console
~/myrepo $ git lfs track "*.zip"
$ git lfs track "*.zip"
Tracking "*.zip"
```

This will create `.gitattributes`:

```console
~/myrepo $ cat .gitattributes
$ cat .gitattributes
*.zip filter=lfs diff=lfs merge=lfs -text
```

And a commit will add it to the repo:

```console
~/myrepo $ git add .gitattributes && git commit -m "Enable LFS for zip files" -m "This is made to avoid large files to be stored in the repo"
$ git add .gitattributes && git commit -m "Enable LFS for zip files" -m "This is made to avoid large files to be stored in the repo"
[main 308e38d] Enable LFS for zip files
1 file changed, 1 insertion(+)
create mode 100644 .gitattributes
Expand All @@ -111,19 +108,19 @@ In this lab you will:
Now to check that everything is working, add a 10MB file sample:

```console
~/myrepo $ dd if=/dev/zero of=myarchive.zip bs=1024 count=10000
$ dd if=/dev/zero of=myarchive.zip bs=1024 count=10000
10000+0 records in
10000+0 records out
10240000 bytes (9.8MB) copied, 0.058306 seconds, 167.5MB/s

~/myrepo $ du -sh myarchive.zip
$ du -sh myarchive.zip
9.8M myarchive.zip
```

And check the actual size of the `.git` directory:

```console
~/myrepo $ du -sh .git
$ du -sh .git
864.0K .git
```

Expand All @@ -132,7 +129,7 @@ In this lab you will:
the path where the object is stored is different:

```console
~/myrepo $ find .git -size +50
$ find .git -size +50
.git/lfs/objects/31/e0/31e00e0e4c233c89051cd748122fde2c98db0121ca09ba93a3820817ea037bc5
```

Expand All @@ -141,21 +138,21 @@ In this lab you will:
5. To test the `commit-msg` Git hook:

```console
~/myrepo $ echo "A test for the commit-msg hook" >> First.txt
$ echo "A test for the commit-msg hook" >> First.txt
(no output)

~/myrepo $ git add First.txt
$ git add First.txt
(no output)

~/myrepo $ git commit -m "Test commit-msg hook" -m "Extended description of the commit"
$ git commit -m "Test commit-msg hook" -m "Extended description of the commit"
[ERROR] Commit must start with 'feat:' or 'bug:'!
```

And to test the `id_rsa` file exclusion first create a SSH keypair inside
`myrepo`:

```console
~/myrepo $ ssh-keygen -f ./id_rsa
$ ssh-keygen -f ./id_rsa
Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Expand All @@ -181,7 +178,7 @@ In this lab you will:
return a list that is excluding `id_rsa*` files:

```console
~/myrepo $ git status
$ git status
On branch main
Your branch is up to date with 'origin/main'.

Expand All @@ -195,12 +192,12 @@ In this lab you will:
hook preventing it, and since we don't care, we can safely `checkout` it:

```console
~/myrepo $ git reset && git checkout First.txt
$ git reset && git checkout First.txt
Unstaged changes after reset:
M First.txt
Updated 1 path from the index

~/myrepo $ git status
$ git status
On branch main
Your branch is up to date with 'origin/main'.

Expand Down
2 changes: 1 addition & 1 deletion Mastering-Versioning/Git-Hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ In this lab you will:

```console
$ cat <<EOF > .git/hooks/commit-msg
grep -q '^feat: \|^bug: ' \$1 &> /dev/null
echo "\$1" | grep -q '^feat: \|^bug: ' &> /dev/null
if [ \$? -ne 0 ]
then
echo "[ERROR] Commit must start with 'feat:' or 'bug:'!"
Expand Down
Loading