Part 2: To Git or not to Git — Command Line (Git Bash)

Joe
6 min readApr 27, 2021

Lets go through a simple setup setup using Git Bash, the command line interface for Git.

To start with I created a folder on my D: drive in Windows to contain all of my future Git repositories it is simply called Git.

New folder called Git on my D: drive

Inside this folder I created a new folder called Git-Bash-Example to use for this example.

New folder called Git-Bash-Example

Go to Git — Downloads (git-scm.com) and download the correct version for your Operating System.

Git Download

Install the application downloaded from Git.

Start the application by clicking on the start menu and typing Git

Start Git Bash from the start menu

Git Bash should start and you should see a screen like this:

Git Bash Window

Next to set Git to our repository we will need to navigate to the location of our folder. To do this we will use the cd command in Git Bash to change directories. In this case because I am on a different drive I will precede the folder names with the drive letter like this:

git cd command

Git will tell you where you are after, for confirmation.

Change to correct folder

Next we create our local repository by typing:

git init command
Git Init command

Next create I created a simple text file containing the text “Example text”. I named this file Example-Bash.txt. I now want to let Git know this file exists and that I want to track it. This is done with Git’s add command. So to add my txt file I would type:

git add command

This adds the file to git for tracking but has not yet added it to the local repository. If there is more than one file these commands are helpful:

Add a few specific files
Add all the files and sub-folders in the directory

To commit to the local repository use the commit command with the -m switch, which adds a commit message for tracking any changes.

git commit command with message

Go back to the text file and add some more text to it. Git will be able to detect those changes with the command:

git status command

These changes are staged by using the git add command and then it can be added to the local repository by using the commit command with the -m switch and a new message for tracking the commit. Just like previously.

The next topic is branching.

To create a branch using git bash just use the branch command and give it a name.

git branch command to create a new branch

To switch to this new branch use the checkout or switch command.

git commands to change branches

The branch you are in is identified at the end of the command line. You can have as many branches as you want and you can see all the branches in your repository with the command

git branch command

It will also highlight the branch you are currently in by preceding it with an asterisk and coloring the text for the branch name.

Make a change o the text file then add it to the staging and commit it to the dev branch. You can see the commit logs with the command

git log command

Now to add this change to the main/master branch go back to the main branch using the switch or checkout command. If you have forgotten what you main branch is called use the branch command to show you the branch names.

Once in your main branch you can merge the changes from the dev branch into the main branch with the command

git merge dev into main

If any merge conflicts arise they will need to be resolved to complete the merge. In this case there should be no conflicts. You can verify the merge using the log command.

Now to setup the remote repository. Go to https://github.com/ and create an account, or log in if you already have an account. Once in create a new repository. Give the repository a name and a description. In this case I made the repository public. You should also give it a readme file as well as a .gitignore file created for unity. Github has the .gitignore file already created youo just need to choose it from the drop down. The repository page should look like this after you have filled out the fields.

Github new repository

Copy the url for the new repository and we can now point our local repository to the remote repository on Github using the following command

link local repository to remote repository

To copy the content to the remote repository use the push command to push the changes to the remote server.

push changes to github

If the master branch is called main or something different then ensure it is named correctly in push command.

If working as part of a team and someone else has made changes to the project, those changes can be downloaded into your local repository by using the pull command

git pull command

Alternatively if you are starting on a new computer or need to recreate the full repository on your local system you can use the clone command

git clone command

This should cover all the basics of using git through the command line.

--

--