Unity VCS with Git and GitHub

When keeping backups of our files and folders and/or when working on a team, we need a tool to avoid unnecessary disk space usage, keep files updated in an organized way and make team working easier.
Git is that tool
In accordance with Wikipedia, Git is a software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development.
Installing Git
For installation, visit the Git official webpage.

Git is not GitHub
Git is a VCS (Version Control System), it means that Git can create a historic record of all changes made in our files (like a snapshot) and create versions of them.
Git works on the local machine (for instance, my laptop) and we can use its CLI (Command Line Interface) or others like VS Code (Visual Studio Code) to execute commands.
GitHub, Bitbucket and others similarly provide a remote (cloud) hosting service to make source files available for teamwork remotely. They can be used with the Git CLI or with their own web or desktop applications
Some git commands for local folders and files:
- ls: shows the available files and folders in the actual path. The actual path is displayed at top of the Git CLI window
- cd: change directory
- cd gi+ Tab: when typing some characters (for instance gi), we can use autocomplete with Tab key (for example, the CLI returns cd GitHub/). Autocomplete is also executed if the folder name contains spaces. As an example, if there is a folder called My Folder and the user enters cd my + Tab, the CLI shows $ cd My\ folder/





Git workflow
The following is a general view of the Git workflow

Working directory, Staging area, Local repository
Stack overflow: What’s the difference between working directory and local repository?, answer 2
Working directory: the root path which contains the files and folders that the user will work on.
Staging area: It is the index file in .git folder. It is like a temporary area between working directory and local repository. Here, after adding files to Staging area (using the add command), the files and folder will be put in the index file, waiting to be committed to local repository.
Local repository: It is the .git folder. It keeps something like a “snapshot”, a “version” with all the changes done in the files that were committed previously.
Remote repository: It’s a repository hosted in a cloud service like GitHub or Gitbucket that keeps all the “versions” pushed from the local repository.
Branches
A branch is an independent line of development. It’s really useful when we are team working.

Branch commands
git branch: list local branches
git branch -r: list remote branches
git branch -a: list local and remote branches
git branch <branch_name>: create a new branch
git checkout <branch_name> or git switch <branch_name>: switch branch
Create a new remote repository in GitHub
Go to GitHub webpage and login. (If you don’t have an account, sign up).
Create a new repository as follows:

Options:
Public: anybody with the link can see the repository
Private: only the owner and those who have been invited can see the repository. Checkout Repository visibility settings
Add .gitignore file: some kind of projects like Unity ones have a group of files and folders that aren’t mandatory when teamworking. This file has the settings for excluding those files and folders from being committed.
Initializing local repository
- Go to folder that contains the Unity project files, e.g.:
C:\Users\UserName\Documents\Unity Projects\VCS Project
- Right click, Git Bash Here

- In Git Bash window, write git init and press enter. git init is used for initializing a local repository in actual folder

Linking to remote repository
- In GitHub, click on Code and copy repository url

- Go to VCS Project folder, Right click, Git bash here. Write
git remote add origin <remote_repository_url>
replacing <remote_repository_url> for the GitHub url link using right click, paste in Git Bash window. Later write
git remote -v
to verify connection and permission with remote repository to fetch and push to origin

Now that connections is established, the suggested way to make the push for avoiding issues is: pull, commit, push
git pull
git pull origin main : pull changes from origin into main branch

git commit
Before making the commit, check the git status
git status

filenames in red: untracked files or folders
filenames in green: tracked files or folders, ready for commit.
git add: add file or folder to commit. Let’s add Assets folder
git add . : add all files or folders to commit

Commit: send tracked files to Staging Area
git commit -m “<commit_message>”

git log : show all commits with their info

git push
git push : push commit to remote repository

If you write git status and see green files and a message changes to be committed, it means that you haven’t made the commit yet. So first make the commit and then make the push.
git help : displays git commands. Look at the collaborate commands

fetch: download all from the server
pull: merge changes on the server with my project changes
push: push my commits

And the files in the Laptop are:

Maybe you have already noticed the difference: Where are the folders missing in GitHub?
Here is where the .gitignore files works
If you open it with a program like Notepad, you’ll find the following:

Folders and files with this configuration (this .gitignore file has another types of files and settings) will be ignored, it is, they won’t be tracked and neither sent to any push to GitHub.