Here are some basics. I’m going to push the code from my last post to GitHub. In this example I am using PowerShell with Git and GitHub Desktop already installed. You will have to ensure you sign into GitHub Desktop with your GitHub account.

Tip: You can install and import the PowerShell module posh-git for the Git summary status info to display in your terminal.

Directory Structure

In the following example, I created my new repository I want to push to GutHub in C:\git\repos\ and created a new folder called basic-go-container.

cd C:\git\repos\
mkdir basic-go-container

The contents of the folder is as follows

dockerfile
└─go
  └─src
     └─app
       main.go

Change directory into the newly created directory

cd C:\git\repos\basic-go-container

Initialise the Branch

You can initialise a new Git repository and call the initial branch main as follows:

git init -b main

Notice the Git summary status in the terminal changes as the locally installed Git application and posh-git module recognises the current directory is a Git repository.

gitInit

The +2 shows that there are two new added files (dockerfile and main.go), but the “!” means they are un-tracked. Now we can change that.

First Commit

Track all the files within the repo and make the initial commit.

git add *
git commit -m "initial commit"

After git add * the “!” in the prompt changes to “~”, which means there are uncommitted changes. After git commit the status turns green as there are currently no un-tracked or uncommitted changes pending.

gitInit

Create the GitHub Repository

Here, I logged into my GitHub account and created a new, public repository called basic-go-container. I can add a README file here but will do that later.

gitRepo

Define the Remote Repository

Next, I have to define the remote repository where I want to push the local repo to. I’m using my markkerry GitHub account and the new repo I just created.

git remote add origin https://github.com/markkerry/basic-go-container.git

# Now let's verify it
git remote -v

gitInit

Push to GitHub

It’s time to push the locally committed changes to the remote GitHub repo. The -u option automatically sets that upstream for you, linking your repo to a central one

git push -u origin main

gitPush

You can see the newly create repo in GitHub.

github

Additional Commits

Now let’s create a README.md file with a bold H1 header of basic-go-container. Then we’ll add the file, commit the change, and push to GitHub.

echo "# basic-go-container" >> README.md
git add README.md
git commit -m "added README.md"
git push -u origin main

gitCommit2

If you refresh GitHub you can see the README.md file in the repo.

github2

Finally, I’ll add simple instructions within README.md of what to do with the dockerfile and main.go files.

Create a Branch

Git branches allows you to seperate and continue developing code without affecting the main branch. Once complete you can then merge the code back into the origin branch (main). See the below to create a new “test” branch

git checkout -b test main

Add and commit a file

git add <filename>
git commit -m "Commit filename"

Switch back to the main branch

git checkout main

Merge the test branch into main

git merge test

Now delete the test branch

git branch -d test