Programming - From Base to AceProgramming - From Base to Ace
  • Introduction

    • About this Course
  • Developer tools

    • Visual Studio Community
    • Visual Code
    • Git and GitHub
  • Version control fundamentals

    • Basics of Git and GitHub
    • GitHub Teamwork
  • Introduction to Programming

    • 01 - Computer Programming
    • 02 - Basic Building Blocks
    • 03 - Starting in C#
    • 04 - Storing Data
    • 05 - Processing Data
    • 06 - Getting User Data
    • 07 - Methods
    • 08 - Making Decisions
    • 09 - Loops
    • 10 - Strings
    • 11 - Arrays
  • Object Oriented Thinking

    • 21 - Abstraction
    • 22 - All About Objects
  • Object Oriented Programming

    • 24 - Defining Custom Classes
    • 25 - Getters, Setters and Properties
    • 26 - Constructors
    • 27 - Composition
    • 28 - Interfaces
    • 29 - Inheritance
  • Advanced Topics

    • 41 - Handling Exceptions
    • 42 - Creating Libraries
    • 43 - Unit Testing
  • WPF

    • 50 - Introduction to WPF
  • Tutorials

    • Terminal Selection Menu

Basics of Git and GitHub

🌐 Supporting learning material

LinkedIn Learning course: Learning Git and GitHub

Cheatsheet: Cheatsheet GIT

In the software world we will be using Git & GitHub as a version control management system that enables us to have versions of our sofware, keep track of the changes that where made and make branches to safely develop new content without the risk of messing up the live version.

Here is some terminology we will be using:

TermMeaning
RepositoryYou can think of a repository as the main folder of your project. This contains all the files in your project and the history of changes you have made.
BranchSeparate place within your repository, where you can, for example, test new code, without having to do so on the “product version”.
Master/mainThis is the main branch, or the “product version” of your project. New code that has been tested and approved is merged into the Master branch.
ForkA fork is a copy of a repository. This allows you to work on someone else's project without modifying the original.
CommitGit command that adds changes to your local repository.
PushGit command to send changes to your (remote) repository, which is on GitHub.
PullGit command to send changes from your (remote) repository to your local files.
MergeGit command to merge changes from one branch to another branch. For example, merging changes that have been tested and approved in the “Develop” branch with the “Master” branch.
CheckoutThis Git command is often used to switch between branches. You check out of one branch, as it were, and start working in another branch.

Git

Starting a new project

  1. Create a folder for the project (you can name it whatever you want, but it's adviced to choose a meaningfull name).
  2. Now you need a terminal in that folder. You can either open that folder with Visual Code and then open a terminal or you can open a powershell from that folder.
  3. Now enter the command git init to start the version control with Git.

Staging files

To create an entry that we can return to we need to add the files to a temporary space where we can store files that we can commit later. This temporary space is called the staging environment.

This can be done by the commands git add -all, git add -A or git add .. I prefer to use the last one.

Commit files

Create a new commit containing the current contents of the index and the given log message describing the changes. A checkpoint is made that makes it possible to return to this point.

This is done by the command git commit -m "text that explanes the changes"

If you want to have a look at the commit information you can use the command git log.

IMAGE

You can see the unique ID (hash) for the current commit and the branch (master) you are committing to.

Environments & File states

In Git we have the following environments:

  • Working environment: Files here look as they did during the last commit
  • Staging environment: Temporary environment where you can queue changes until you are ready to create a new commit.
  • Commit environment: A new entry is created with a new hash.

Files can have the following states:

  • Tracked

    Files that already existed in the previous commit

    • Unmodified: unchanged
    • Modified: changed
    • Staged: Sitting in the staging environment
  • Untracked

    Everything else (a new file added since the last commit)

Ignoring files

Not all files should be included in the Git management, such as sensitive or personal information:

  • API keys
  • Authentication tokens
  • To Do items
  • System files

You can do this by adding a .gitignore file and adding the folders and files that should be excluded from the git tracking.

Looking for differences

  • git diff: To show the differences between the working copy and the HEAD commit (latest commit in the current branch)
  • git diff --staged: To show the differences between the staging area and the HEAD commit

Branches

Normaly you are working on the Master or Main branch, let's call this the production of live branch. If you want to make changes without risk of breaking the live branch it's a good idea to make a new branch for this. Usually this branch is called dev as in development branch, but other branches like hotfix are also possible. Once the development is done and fully tested you can merge it with the production branch.

The git flow below is therefore often used.

For example, you want to add a new feature or perform a patch or fix:

  • You create a new branch with an appropriate name
  • You make the necessary adjustments and test everything
  • You merge that branch with the master branch
  • You delete the used branch.

In all this you never work directly on the master branch.

IMAGE

  • git branch: Shows all branches
  • git switch -c "new-branch": Changes to a new branch called "new-branch"
  • git switch master: Switch to a existing branch (in this case the master branch)
  • git merge new-branch: Merge the current branch with the "new-branch" branch
  • git branch -d new-branch: Delete the branch called "new-branch"

GitHub

GitHub is an online platform for software development and version control that is built around the Git version control system, providing all the features of Git for managing versions and source code. It is a social coding website with a storage service in the cloud that offers the possiblity to share code and even collaborate on software development.

Link Git to your GitHub repository

When logged in into GitHub you can see all your repositories. With the New button you can make a new repository (public or private). Once that is done you can add this repository as your remote to your git local development folder:

git remote add origin URL

Where the url is url of your GitHub repository (you can find this under the button Code of your repository in the tab HTTPS)

Push changes to the remote

Once changes have been committed they can be pushed to your GitHub repository with git push --u origin master

Cloning a repository

You can make infinite local copies of a repository.

Just open a terminal in the folder where you want to add the local copy and type git clone URL foldername. Where URL is the URL of the GitHub repository and foldername is the name you want to give to the local folder.

❗Warning

When working with different local copy's (maybe these are local copies of different teammembers) always merge the remote with the local version before changing and pushing again.

  • git fetch: only retrieves the latest commit history from the remote repository, but it does not affect the local copy.
  • git pull origin master: merges the master branch on the remote with the local copy.
Last Updated:: 5/6/25, 1:20 PM
Contributors: Ronny Mees
Next
GitHub Teamwork