GitHub: Pull 🡆 Commit 🡆 Push
Continuing from my last Medium article, “Setting up Git & GitHub (for Unity Projects)”, we’re now moving into the 3 most important regular tasks of using GitHub:
Pulling, Committing, and Pushing:
Git Pull: “git pull” updates your current local working branch, and all of the remote tracking branches. It's a good idea to run
git pullregularly on the branches you are working on locally. Without “git pull”, (or the effect of it,) your local branch wouldn't have any of the updates that are present on the remote.
Git Commit: “git commit” creates a commit, which is like a snapshot of your entire repository at specific time. You should make new commits often, based around logical units of change. Commits should tell a story of the history of your repository and how it became what it is.
Git Push: “git push” uploads all local branch commits to the corresponding remote branch.
With Git Bash open and the directory set to the Unity project folder (in my case, “Version Control Example”), enter the command “git pull” to download the data from the connected GitHub repository. Now we need to specify which branch to merge the downloaded data with.
Note: Recently there was a change regarding the “master” local name. Although it still says “master” in the terminal at the end of the directory address, typing “git pull origin master” will fail with the error “fatal: Couldn’t find remote ref master”.
This is fixed by instead using “git pull origin main”. Optional: You can also use the command “git branch -m master main” to rename the remote ref of “master” to “main”, which will now show up at the end of the directory address. “git pull origin main” will work regardless of whether or not you’ve renamed it, but it’s a good reminder.
Now, moving on. I want to pull from our remote “origin” server (which I created in “Setting up Git & GitHub (for Unity Projects)”), getting the files from the “main” branch inside “origin”, and merging with whichever local branch we’re currently on. In this case, “main”.
“FETCH_HEAD” is the most recent push/version from the online repository, so you know it is up-to-date.
You can show a list of all branches using the “git branch” command.
Next is to add the desired local files into the collection of files we want to commit. “git status” shows all files and folders in the directory. Red names are untracked/not added, green names are tracked/added.
Add them individually with “git add <file/folder name>”, e.g. “git add assets”. Or add them all using “git add .”
Now we need to create the actual commit, setting the commit message to be whatever new thing I did in this commit compared to any previous commit. In this case, its the first commit after creating a new Unity project so I’ll say that.
Lastly, we need to push the commit to the online repository.
Now all those files are in the online repository!
That’s the whole process of Pull 🡆 Commit 🡆 Push done! Easy!