Learning Git and GitHub : Part 1
Wait, Git and GitHub are separate?
Now, I have worked with GitHub and their application before when I was one of the artists for an indie game called Blue Revolver, but never with Git itself. I will say this, I never truly understood GitHub but that changes today!
First we go to https://git-scm.com/ and download Git. Install with all the defaults, and then we are good to go!
I open up Git and…
… I am HORRIFIED. Oh no, it is command line.
Well, its time to learn right?
With a Git Repository already made I pulled my address from GitHub right here.
Now what needed to be done is to link the repository with the local folder I want to associate it with so it’s time to get back to Git.
Similar to command line, I can use “cd”(Change Directory) to go through my folders. “ls” is used to used to list what directories are available and so I found my way into cd D:/ cd “Unity Projects” (The “” are used as Git cannot read spaces) and then cd “Version Control Examples” (This is my project folder and you can use TAB to auto complete any folder for you. \ is Gits indicator for spaces.)
(By the way, you can do “git --help” to get a list of commands used in Git and their descriptions!)
With that we do “git init” to initialize an empty git repository locally, and then from there I did “git remote add origin” then my URL I copied earlier from GitHub. Success!! I can check what is in there via “git remote -v”
Now that we are setup, the command states (master) next to our current directory.
The key things to remember when using Git is there is a order to do things.
PULL / COMMIT / PUSH
First we PULL from the GitHub repository so that our local is updated with any changes that anyone else on the team may have made.
Now here is where some of the trouble begins.
As you see “git pull origin master” does not work, why is that? This is because although Git may use master as a default. GitHub now uses main! If we actually go into the repository in the browser…
After redoing the command using “main”, now I had the data from the main repository branch from GitHub locally on my machine.
Next was to make some changes to my local folder by creating a cube and a script within Unity and save it. Now my local files differ from those online.
So how do I make sure my changes reflect on the online repository?
First by doing “git status”, I can see what files and folders differ/have changed in RED text. As it says there. I can use “git add” to tell Git these are the files that I want to COMMIT, sort of like making a to do list. You can add each item one by one, but if you do “git add .” that will just add any changes all at once. (Convenient!)
Now it is time to COMMIT these changes. Lets make that to do list. The command is well “git commit” -m (for message) and then “Your message for the commit”. The message should tell you briefly, what it was you committed.
Oh but wait, this is my first time so when I try, they need to know who I am. Following the commands it gives me for user.email and user.name, I just make sure it matches my GitHub account email and Username, then we can finally commit our changes.
So far so good! Makes sense!
Now all I have to do is PUSH (Upload) the actual files put into my COMMIT to the repository on GitHub.
What would the command be? Well, going by what we have already done, “git push” would be a good guess. “git push origin (the source of everything) main (which is the branch name)”, hit enter, and… Oh God, what was that error?
Well not understanding, I tried to just push to master instead, and well it worked! However, this would come to bite me in the butt later.
I go back to my browser, hoping to see my changes online, but all I see is this.
Then I notice that I somehow now have 2 branches when I should only have one. I should only have a branch called main, which is the default, however if you look at the previous screen cap from Git, you notice in the end that it made a new branch called master because it did not exist and I pushed to master.
The compare and pull request at the top right allows me to, via the website, pull the changes from master into the main branch. Now, this definitely wasn’t the best way to do things, and well after researching and fumbling around a little, I realized that Git and GitHub… are actually not related in terms of who manages the two. Git, uses master as a default branch while GitHub at some point changed it to main. Because of this, I actually need to make changes on the Git side of things.
I needed to figure out how to change my default in Git. But first, did Git even realize there was a main branch, was it even in my computer? I could check what branches there were locally…
… and nope, not there.
Doing some more research and asking my friends who are experienced developers, I finally figured it out.
Using “git checkout -b main” I could create a new branch locally, and at the same time switch to it. From there that one command I ignored previously “git push --set-upstream origin”, could be used to set a branch to be essentially the default in Git.
Now lets see, does it exist now?
That ends Part 1 of me fumbling through the basics of Git. The more I use Git, I realize how actually intuitive it is. It scares me though that I could possibly ruin a repository by mistake… but well we can go over that in Part 2.
If you got to the end of this, thank you and I hope to see you in the next post.
See you next time.