Get more things done with git
Git with Linux is a bit of a tango, but you can have a smooth experience with it. Just remember these steps:
- Create your repository
- Create your Unity project
- initialize your local repository
git remote add origin <paste the url you just copied from your github> - add the remote repository
- EXTREMELY IMPORTANT! rename your master branch to main with “git branch -m master main”
- VERY IMPORTANT TOO! git pull origin main
- add files
- commit
- push remote origin
- if you don’t want to keep renaming your branch to main, you can permanently change it with this command: “git config -.- global init.defaultBranch main” (also don’t use the period between the dashes, I put that there because two dashes always turn into a longer dash) Thanks to Siddhat Thakur for the info!
Doing these steps in order will save you potential lost hours of hair pulling googling to figure out why git isn’t connecting with your local repo.
if you ever need a reminder as to what commands are available, just type
git -.- help (without the period)
So you’re making great progress with your coding, and you want to create a save state in git. just:
git pull origin <name of branch>
git add .
git commit -m “descriptive message”
git push origin <name of branch>
BRANCHING:
Let’s say you want to try a new feature in your game or program. This is where branching comes in. You can create a branch for any reason or for any person.
Let’s say we want to create an Inventory system for our game. Let’s create a branch for it.
git branch Inventory
This creates a branch named Inventory, but you’re still not in it, you’ll need to switch to it with the command
git switch Inventory
Now you’re inside the inventory branch and any changes you make to your program will stay inside of the Inventory branch!
Let’s make a few changes.
I’ve added a few cubes into an inventory directory, I thought about pulling in git but Inventory didn’t exist on the server so I just committed and pushed it instead. Afterwhich, it now exists in my main github repository!
Now here’s the cool part. Let’s say I want to go back to my main branch and stop working on my inventory for a while. When I switch to the main branch, unity will then change to the state it was in before I entered the inventory section! In this case, the cubes still exist on screen, but they’ve been deleted from the project. Inventory only exists in its own sandbox! I can safely work on anything I want and when I’m ready to commit,
I can then merge the branch with the command git merge. so if I wanted to merge the Inventory to main, I’d first switch to the branch I want to merge into, in this case main, and then type git merge Inventory
Finally, the main attraction! resetting to an earlier version.
We can get a short summary of our commits by typing git log -.-oneline (no period)
Every commit has a hashtag number, and this is why we describe our commits, so we can know down the line one month, maybe 5 months or more and we won’t exactly know what we did. Descriptive descriptions are essential!
Anyway, let’s say we want to revert back to the point where we added some inventory stuff, you can treat a commit just like a branch! You can checkout the hashtag, but because you are using checkout, you won’t be able to make any permanent changes, however you can create a branch from that point and it will behave like any other branch. You do that by using the -b option with checkout.
here we are in the previous state as a branch. no harm done to main or other branches.
You should now have a good understanding of git. If you want to learn more, there’s plenty of tutorials but one I highly recommend is net ninjas on youtube.