Programming

I love Git, and I think you will too.

Imagine this scenario, you’ve been working on a project for quite a while. You’ve been coding for two hours and all of sudden your laptop crashes. You turn it back on and while you have most of your code, you realized you’ve lost two hours of progress.

via GIPHY

You have a way to not lose your progress though, and that’s with version control.

Version control

When writing code, you’ll want to use version control software to save your changes.

Version control is a type of software that will do record keeping for your changes. This allows you to gradually push your code to have as an online backup and to share with others if you’re on a team. I prefer Git as it’s the most popular and the one I use the most.

How to use Git

In these examples, I’m using the terminal instead of a text editor for Git because the commands are the same in most terminals and the editors need to be configured differently.

I recommend using Gitlab or Github to host your changes. You can setup your project there.

Here’s a small example of how to use Git.

Go to a code repository hosting site like Github or Gitlab. Create a new project and it’ll ask you to set up your repo. Follow the steps and you should have a new project created. I will create a post on this in the future of how to do this step.

In your project type in

git branch

and it’ll show you your branch name. It will probably say main or master. You can create new branches from your current branch by going to your repository in the terminal and typing in

git checkout -b "new branch name"

where “new branch name” is your new name. This is how you can create features to then be able to merge back into your main branch. This way if you screw something up, it’s not in your main branch.

git add .

This will add all of the untracked files. This is normally not recommended because you could add files that you didn’t mean to. Only use this if you know what you’re doing.

If you do want to add many files, you can be in your root folder of your project and can type something like this:

git add "**/*.gd"

This will add all of your GDScript files that you’ve modified.

Then you should commit your changes. This stores it locally. If you leave off the “-m” option, it can open your set text editor where you can type in the message there.

git commit -m "Added the ability to save the game"

When you’re ready to push your changes. type in

git push

and this will either create a remote branch if it doesn’t exist or push your changes to an existing branch. You can set up a pipeline on Github or Gitlab that can run tests or have other things occur after this step like it can pull your changes, run your tests, and push to a test environment or to production depending on the branch.

.gitignore

A git ignore file is for telling Git to ignore files so you can’t accidentaly check in files that you don’t need or want to have in version control. An example could be ini files on Windows that is just an initialization file that isn’t necessary for your game to run.

Here’s my global .gitignore file that I’m using in my project. You could have this in your project locally, but I like this to affect every Godot project I’m working on. Feel free to copy what you want. Hashtags are comments that you can enter that is ignored by Git.

# On my Mac, I want to  ignore this
.DS_Store

# Godot 4+ specific ignores
.godot/

# Godot-specific ignores
.import/
*.import
export.cfg
export_presets.cfg
addons/*/*.ini
*.ini
# Dummy HTML5 export presets file for continuous integration
!.github/dist/export_presets.cfg

# Imported translations (automatically generated from CSV files)
*.translation

# Machine Object files aren't read by Godot
*.mo

# Mono-specific ignores
.mono/
data_*/
mono_crash.*.json

# System/tool-specific ignores
.directory
*~

# Editors
.vscode

git-lfs

Git-LFS stands for Git Large File Storage. It is used when you have large files such as audio samples, videos, datasets, and graphics where it has a text pointer to the file that is stored in Github, Gitlab, etc. It a plugin to Git that has to be separately installed. When you have large files, you’ll want to use Git-LFS.

git lfs track "level1.ogg"

This would then track the level1 Ogg Vorbis file where you could then do git add, etc and treat it like normal Git.

gitattribute

GitAttribute speil

Here’s my global .gitattribute file

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.cpp text
*.c text
*.h text
*.gd text
*.cs text

*.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
*.ico filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text

.gitconfig

Finally, you can configure Git with permanent changes per project or globally. The file is called gitconfig, and this file is how I have configured Git globally. It’s located in my home directory of ~/.gitconfig. You can have your gitconfig file in your project or you can have it in your home directory for it to be global.

[user]
 name = Your name
 email = abc123@gmail.com
[push]
 default = simple
[alias]
 grog = log --graph --abbrev-commit --decorate --all --format=format:\"%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(dim white) - %an%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n %C(white)%s%C(reset)\"
[merge]
 tool = vscode
[mergetool "vscode"]
 cmd = "code --wait "
[diff]
 tool = vscode
[difftool "vscode"]
 cmd = "code --wait --diff  "
[init]
 defaultBranch = main
[filter "lfs"]
 clean = git-lfs clean -- %f
 smudge = git-lfs smudge -- %f
 process = git-lfs filter-process
 required = true
[core]
 autocrlf = input
[pull]
 rebase = true