Starting with a new project? main branch for presentation, other branches for drafts. Maintaining a website? main branch for live, other branches for testing. Coding a project? main branch for presentation, other branches for development.
Skip to the bottom of document for info on reverting to a prior git revision.
C:\Users\x>cd "documents/unity projects/version control example"
C:\..>git init
Reinitialized existing Git repository in C:/Users/x/Documents/Unity Projects/Version Control Example/.git/
C:\..>git status
On branch main
nothing to commit, working tree clean
C:\..>git branch
* main
C:\..>git branch dev
C:\..>git branch
dev
* main
C:\..>git switch dev
Switched to branch 'dev'
C:\..>git branch
* dev
main
C:\..>git checkout main
Switched to branch 'main'
C:\..>git branch
dev
* main
Simple enough three git commands. Now to:
Add several branches locally, make development changes, in different branches, merge the various changes to the development branch, push the new branches to the github server so team members can access branches.
Start in branch dev.
C:\Users\x\Documents\Unity Projects\Version Control Example>git branch
* dev
inventory
main
quest
Opening my Unity project I have a cube and a ChallengeTest.
Unity project with added cube and ChallangeTest Asset
The git ‘dev’ branch is the current branch and is marked with an asterisk ‘*’.
Add a new C-sharp script ‘DevBranch’ and save.
Unity with added script DevBranch
Look at the dev and main git branches:
C:\..>git status
On branch dev
Changes not staged for commit:
modified: Assets/Scenes/SampleScene.unity
Untracked files:
Assets/DevBranch.cs
Assets/DevBranch.cs.meta
ProjectSettings/SceneTemplateSettings.json
no changes added to commit (use "git add" and/or "git commit -a")
C:\..>git switch main
M Assets/Scenes/SampleScene.unity
Switched to branch 'main'
C:\..>git status
On branch main
Changes not staged for commit:
modified: Assets/Scenes/SampleScene.unity
Untracked files:
Assets/DevBranch.cs
Assets/DevBranch.cs.meta
ProjectSettings/SceneTemplateSettings.json
no changes added to commit (use "git add" and/or "git commit -a")
Step 1: add, commit using the ‘dev’ branch
C:\..>git switch dev
M Assets/Scenes/SampleScene.unity
Switched to branch 'dev'
C:\..>git add .
warning: in the working copy of 'Assets/Scenes/SampleScene.unity', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'Assets/DevBranch.cs.meta', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'ProjectSettings/SceneTemplateSettings.json', LF will be replaced by CRLF the next time Git touches it
C:\..>git status
On branch dev
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: Assets/DevBranch.cs
new file: Assets/DevBranch.cs.meta
modified: Assets/Scenes/SampleScene.unity
new file: ProjectSettings/SceneTemplateSettings.json
C:\..>git commit -m "DevBranch Script created"
[dev fec8670] DevBranch Script created
4 files changed, 321 insertions(+), 15 deletions(-)
create mode 100644 Assets/DevBranch.cs
create mode 100644 Assets/DevBranch.cs.meta
create mode 100644 ProjectSettings/SceneTemplateSettings.json
Now notice that Unity project “Version Control Example” is updated with the [local] git. Here is the before:
Unity project with DevBranch script before switching git branches
C:\..>git checkout main
Switched to branch 'main'
Here is the Unity project after switching from git branch dev to branch main after time to update and click “reload scene”:
Project reloaded after git switch to branch ‘main’
‘DevBranch’ does not appear in the reloaded Unity after switch to branch main.
Now how to allow the ‘inventory’ branch to match the ‘dev’ branch:
C:\..>git switch inventory
Switched to branch 'inventory'
C:\..>git merge dev
Updating 9277948..fec8670
Fast-forward
Assets/DevBranch.cs | 18 +++
Assets/DevBranch.cs.meta | 11 ++
Assets/Scenes/SampleScene.unity | 186 ++++++++++++++++++++++++++---
ProjectSettings/SceneTemplateSettings.json | 121 +++++++++++++++++++
4 files changed, 321 insertions(+), 15 deletions(-)
create mode 100644 Assets/DevBranch.cs
create mode 100644 Assets/DevBranch.cs.meta
create mode 100644 ProjectSettings/SceneTemplateSettings.json
After making a change in Unity to the project, allow [the local] git to recognize the changes. Here a script was added named ‘Inventory’:
‘git add .’ followed by ‘git commit -m “add Inventory script”
C:\..>git status
On branch inventory
Untracked files:
(use "git add ..." to include in what will be committed)
Assets/Inventory.cs
Assets/Inventory.cs.meta
nothing added to commit but untracked files present (use "git add" to track)
C:\..>git add .
warning: in the working copy of 'Assets/Inventory.cs.meta', LF will be replaced by CRLF the next time Git touches it
C:\..>git status
On branch inventory
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: Assets/Inventory.cs
new file: Assets/Inventory.cs.meta
C:\..>git commit -m "add Inventory script"
[inventory d8bcb52] add Inventory script
2 files changed, 29 insertions(+)
create mode 100644 Assets/Inventory.cs
create mode 100644 Assets/Inventory.cs.meta
Branch ‘main’ lacks scripts ‘DevBranch’ and ‘Inventory’. Branch ‘dev’ lacks script ‘Inventory’. After ‘git checkout’ dev, and then ‘git merge Inventory’. Now the remote github versions need to be updated.
C:\..>git status
On branch dev
nothing to commit, working tree clean
First, there are no added branches to the Github repository
Github repository only has branch main
Use ‘git init’ and then ‘git push origin dev’ (Maybe need to login to Github). Do refresh the Github website page.
C:\..>git init
Reinitialized existing Git repository in C:/../.git/
C:\..>git push origin dev
Enumerating objects: 18, done.
Counting objects: 100% (18/18), done.
Delta compression using up to 4 threads
Compressing objects: 100% (13/13), done.
Writing objects: 100% (13/13), 3.04 KiB | 311.00 KiB/s, done.
Total 13 (delta 6), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (6/6), completed with 3 local objects.
remote:
remote: Create a pull request for 'dev' on GitHub by visiting:
remote: https://github.com/xcvvc/Version-Control-Example/pull/new/dev
remote:
To https://github.com/xcvvc/Version-Control-Example.git
* [new branch] dev -> dev
Github repository with added branches
Now, the Github repository ‘main’ branch does not have the added scripts which are on the ‘dev’ and ‘inventory’ branches. After the scripts are ready to add. then do the merge, add, commit and push, with a Unity project update between the merge and the add.
Github Assets list before push
After the clear status of local git branch main, the Github receives the added Assets after the push.
C:\..>git status
On branch main
nothing to commit, working tree clean
C:\..>git push origin main
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/xcvvc/Version-Control-Example.git
9277948..d8bcb52 main -> main
Now a look at the Github repository
C:\..>git status
On branch dev
nothing to commit, working tree clean
Github repo after push to branch ‘main’
How to back track to a prior revision of the git.
C:\..>git log
Welcome to text editor VIM. press ‘q’ at the lower left semicolon to exit.
commit 92779486577695cd5bb831eacbc2abfe94cff66f (quest)
Author: Lance Gold
Date: Sun Feb 9 00:01:31 2025 -0800
Added cube and Challenge script
commit 5f827ed4ae0ec37257a4f36044beca97932313a1
Author: Lance Gold
Date: Sat Feb 8 23:26:23 2025 -0800
Created new Unity project
commit 8db83ca6f0ea5a1436af609a0569130502ddd304
Author: Lance Gold
Date: Sat Feb 8 16:31:03 2025 -0800
Initial commit
:
Do not commit a prior version to its branch. Treat prior versions like added branches. Pick a desired branch, or a branch suspected to be the desired branch and highlight, copy, and paste its long commit ID.
Highlight, copy and paste the desired revision.
You may need to use cntl-v for the paste. The long ID is the name of a branch, so the ‘git switch’ command is used to temporarily take a look at the Unity project at the time of that branch.
Use ‘git checkout’; ‘git switch’ doesn't work.
C:\..>git switch fec8670a39e7c332c77790c7dd66f06fac94e25c
fatal: a branch is expected, got commit 'fec8670a39e7c332c77790c7dd66f06fac94e25c'
hint: If you want to detach HEAD at the commit, try again with the --detach option.
C:\..>git checkout fec8670a39e7c332c77790c7dd66f06fac94e25c
Note: switching to 'fec8670a39e7c332c77790c7dd66f06fac94e25c'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
Now reload the Unity project on that temporary branch to see if it is where you want to rebuild from.
Revert: adjust something in the past.
Reset: go back in time and continue.
A helpful name can be assigned to the ID.
C:\..>git checkout -b old-project-state fec8670a39e7c332c77790c7dd66f06fac94e25c
Switched to a new branch 'old-project-state'
This is a new branch with the useful name ‘old-project-state’
C:\..>git branch
dev
inventory
main
* old-project-state
quest
Resetting the Main branch to a prior version.
Because ‘git log’ might show a lot more entries than helpful, take a look at the Github's repository main branch history. From the repository's profile, looking at the row with the ‘main’ drop down menu at left and the ‘code’ drop down menu at right, click the ‘branch’ icon left of the ‘tags’ icon.
Github repo clicked on three-dots of branch main for Activity (for commits)
Click on Activity from the three-dot icon of the branch ‘main’s row.
> Pick an earlier version and again click the three-dot icon to choose ‘Compare changes’.
Choosing commit and click three-dot icon: Compare changes
Click the hash code to ‘View commit details’
Click hash code icon: View commit details
Now there is an icon at the right to copy the full commit ID hash code
Click hash code: Copy full SHA for selected commit.
With the long commit ID copied to clipboard, return to command line to paste into ‘git reset’ — hard command. May need to use ctrl-v for paste.
C:\..>git reset --hard 5f827ed4ae0ec37257a4f36044beca97932313a1
HEAD is now at 5f827ed Created new Unity project
With the commit as the HEAD, reload the Unity project to see the status at the time of the reset.
State of Unity project after hard reset
Notice the state of the project with no added cube asset, and no added C-sharp scripts. This is the local process. To get the Github server to match, we need to push the main to origin.
C:\..>git push --force origin main
Everything up-to-date