# Git for Busy Engineers
A critical tool for engineers in many fields, git keeps track of changes to source code.
## Terminology
Repo — repository, another name for a folder that is being tracked by git.
## Step 1: Download Git
Visit [https://git-scm.com/downloads](https://git-scm.com/downloads) and download the most recent version for your operating system.

## Step 2: Download Github Desktop
Visit [https://github.com/apps/desktop](https://github.com/apps/desktop) and download the GitHub Desktop GUI.

This makes viewing git histories etc. slightly easier for those unfamiliar with the command line. It should be noted that git is made for the command line and you should become acquainted with the common commands such as `git add`, `git commit`, `git push` /`git pull` etc.
## Step 3: Setup your global gitconfig file
Set your username and email using github. The easiest way to authenticate is to sign in to a github account.
## Step 4: Setup your global gitignore file
Git should be used exclusively for source code — a gitignore file allows you to only track the source code in a folder (even though there might be lots of other stuff in there). Ignoring large files is also crucial, as git does not perform well above 1GB in the repo. A global gitignore file should be used for files specific to your operating system, code editor and anything else not related to the project: [^global-gitignore]
- System files (e.g. desktop.ini)
- Code editor configuration files (e.g. .vscode)
- Credentials (e.g. usernames & passwords)
To create a global gitignore file (which we call .gitignore_global) in your home directory, run the following commands (all should achieve the same thing).
Git bash:
```bash
git config --global core.excludesfile ~/.gitignore_global
```
Windows command prompt:
```cmd
git config --global core.excludesfile %USERPROFILE%\.gitignore_global
```
Windows Powershell: [^global-gitignore-windows-powershell]
```PowerShell
git config --global core.excludesfile %USERPROFILE%\.gitignore_global
```
To verify that the file is in the right location, paste the following into your terminal of choice:
```
git config --global core.excludesFile
```
> [!example] Output
> You should see the path to the gitignore_global file:
> 
> If not, try the above commands again.
From here, add any system files. A great place to start is the ["Global" folder of GitHub's gitignore templates](https://github.com/github/gitignore/tree/main/Global). In the very least, add the [macOS](https://github.com/github/gitignore/blob/main/Global/macOS.gitignore) or [Windows](https://github.com/github/gitignore/blob/main/Global/Windows.gitignore) templates. [Visual Studio Code](https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore) too if you use it.

## Step 5: Setup your project (local) gitignore file
Use the local gitignore file (found in the root of the project folder where your git repo lives) for any *project* related ignores. [^global-gitignore-source-2]
Github maintains a collection of gitignore templates for each programming language. [^github-gitignore] These can be easily added to a project by running `npx gitignore <template>` (provided you have node package manager — npm — installed). [^gitignore-npx]

## Step 6: Setup gitattributes file
[^gitattributes-template]
```git
###############################
# Git Large File System (LFS) #
###############################
# Engineering
*.csv filter=lfs diff=lfs merge=lfs -text # if very large or auto-generated
*.tsv filter=lfs diff=lfs merge=lfs -text
*.h5 filter=lfs diff=lfs merge=lfs -text # HDF5
*.npy filter=lfs diff=lfs merge=lfs -text # NumPy
*.mat filter=lfs diff=lfs merge=lfs -text # MATLAB
*.rdata filter=lfs diff=lfs merge=lfs -text # R
*.dat filter=lfs diff=lfs merge=lfs -text
# Archives
*.7z filter=lfs diff=lfs merge=lfs -text
*.br filter=lfs diff=lfs merge=lfs -text
*.gz filter=lfs diff=lfs merge=lfs -text
*.tar filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text
# Documents
*.pdf filter=lfs diff=lfs merge=lfs -text
# Images
*.gif filter=lfs diff=lfs merge=lfs -text
*.ico filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text
*.pdf filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.psd filter=lfs diff=lfs merge=lfs -text
*.webp filter=lfs diff=lfs merge=lfs -text
# Fonts
*.woff2 filter=lfs diff=lfs merge=lfs -text
# Other
*.exe filter=lfs diff=lfs merge=lfs -text
###############################
# Git Line Endings #
###############################
# Set default behaviour to automatically normalize line endings.
* text=auto
# Force batch scripts to always use CRLF line endings so that if a repo is accessed
# in Windows via a file share from Linux, the scripts will work.
*.{cmd,[cC][mM][dD]} text eol=crlf
*.{bat,[bB][aA][tT]} text eol=crlf
# Force bash scripts to always use LF line endings so that if a repo is accessed
# in Unix via a file share from Windows, the scripts will work.
*.sh text eol=lf
```
## Step 7: git-lfs
As simulations become more complex, the file size increases. For example, a simulink file can easily grow to
Github does not allow upload of files larger than 100MiB (~104MB). [^github-file-size-limitations] To store source code larger than this, git-lfs (“large file storage”) should be used.
Instead of storing the files directly, git-lfs stores a pointer to the file.
[^global-gitignore]: [https://sebastiandedeyne.com/setting-up-a-global-gitignore-file/](https://sebastiandedeyne.com/setting-up-a-global-gitignore-file/)
[^github-gitignore]: [https://github.com/github/gitignore](https://github.com/github/gitignore)
[^global-gitignore-source-2]: [https://medium.com/self-modifying-code/create-a-global-gitignore-step-by-step-for-macos-and-windows-31a765291409](https://medium.com/self-modifying-code/create-a-global-gitignore-step-by-step-for-macos-and-windows-31a765291409)
[^gitignore-npx]: https://medium.com/@ericapisani/create-a-comprehensive-gitignore-file-in-seconds-3f65e8b437e
[^github-file-size-limitations]: https://docs.github.com/en/repositories/working-with-files/managing-large-files/about-large-files-on-github
[^global-gitignore-windows-powershell]: https://gist.github.com/subfuzion/db7f57fff2fb6998a16c
[^gitattributes-template]: https://rehansaeed.com/gitattributes-best-practices/