Code versioning - csinn/CSharp-From-Zero-To-Hero-v2 GitHub Wiki

What is Code Versioning?

Code versioning is a way to store code in a secure and organized way. It allows having multiple versions of code available to us at any time and to come back to any version or add a new version at ease.

Have you ever lost the code on which you have been working a long time ago? Had you made good progress, but then a mistake and wanted to go back, but couldn't because the changes were made across multiple places after closing the application. Are you in a team and looking for and struggle to work together? If done right- code version solves such problems.

What is Git?

Git is a code versioning system.

Git has its own language- a command line integration which we use to version code. You don't have to learn the commands right away because there are visual tooling alternatives. However, being savvy with git commands will come in handy when encountering difficult situations. As a beginner, it's okay if you just use visual tooling.

Git Logo

While other code versioning systems are slowly declining in demand, Git is rocking it.

Git Popularity

What is GitHub?

GitHub is a Git host. It offers a remote code storage location. The most popular code versioning host and is especially liked and used by the open source communities.

GitHub Logo

Git Bash

Git Bash is a command-line tool for Git.

Git Bash

Here is a beginner's guide to Git with command examples.


Git is just 3 areas

Understanding git comes down to understanding how do files move from one place to another. And it's quite simple, because there are 3 places in total!

3 areas in git

A little prep

In order to make git happen, you will need a remote and local repositories. Repository is a place which holds your code and as the name implies- one is for your local changes and the other is for changes accessible for others.

Usually, it starts from a remote repository. If you haven't already, create a new account in https://github.com/. Then go to repositories tab and create a new one.

After, download and link it with your own computer.

git clone remote/repository.git

Working directory

Working directory is is just another local directory which has nothing to do with git. Git reads the index file which stores all files status updates.

Staging area

Staging area serves as an intermediate area between git and working directory. Files in staging area are the files that will be commited. You can add all the changes to staging area using add command

git add .

Please note- git add is for adding all kinds of changes- this includes removing a file as well.

Git Add

Add moves files from working area to staging area.

Repository

Repository is the last place where files in git end up. It's divided in two areas called local and remote repository.

Local

Local repository is a repository hosted on your own machine.

As mentioned before, files in staging area are ready to be commited. But what is a commit? A commit is a changelist with a message summarising what was changed. Changes that are commited end up in local repository. You can commit the changes with this command:

git commit -m "Meaningful message"

Commit moves files from staging area to local repository

Git Commit

Remote

A remote repository is a repository hosted on hosting provider, for example GitHub.

Push

Moving changes form a local to a remote repository is done using the push command

git push

Git push

Fetch

Remote repository, especially when working in team, might be out of sync with a local repository.

In order to check whether there are any changes in a remote repository you can run this command

git fetch

Git fetch

Fetch takes a peek at file status on a remote repository and reports it back to a local one in attempt to get a list of all the commits which are present in aremote but not in a local repository.

Merge

Joins two development histories together. We will talk more about it in Working Together paragraph

Pull

Pull is a fetch and merge commands executed one after another. In other words, it pulls remote changes into a local repository. In other words, it syncs remote and local changes. You can do it with git pull command

git pull

Git Pull

File statuses

  • Untracked- does not exist outside your local environment.
  • Unmodified- being tracked by git, but has no local changes and so can be ignored for now.
  • Modified- has been modified locally. You need to stage files for them to be moved further down the git pipeline.
  • Staged- ready to be committed

Working Together

When working together, different people will be working on different things. Sometimes, people will work on the same things. Conflicts will happen. This paragraph is dedicated to explain how to manage working together and solving conflicts.

Branch

Branch is a deviation from the main code. You can have as many branches as you want, you can deviate from whatever point of history you want, you can combine different histories into one.

Branches are deviations of commits.

Branching

Usually, they are used for any new work: feature, bug fix, documentation, etc. After all, every change is potentially a breaking one and we want to avoid that by isolating our changes in a safe place- a branch.

Fork

Fork is a copy of a repository, in which you can do changes independently from the original. This is the way to go when you don't own the repository, yet you want to contribute to it. Open source happens through forks, because random people can make contributions.

Fork to a repository is like a branch to a commit- mirroring everything, but at a different scope.

Each Git host will have their own way of implementing forking, it's more of a GUI interaction thing rather than a Git command.

Fork Doing fork in GitHub.

Request For Changes

Let's say you made a change and would like to integrate it into the original repository? What's next?

Before answering that, it's important to understand two terms:

  • Origin- a repository which you own
  • Upstream- a repository which you don't own, but is either the original or a fork

You can synchronize with either and upstream or an origin, but you cannot push changes directlty to an upstream because you don't have access to it.

Let's say you are happy with your changes- do the usual- stage, commit and push them. Lastly, create a new Pull Request- a request for change.

How to do it will also depend on a Git Host.

New Pull Request New PR in GitHub

PR targets PR targets: 2- Source (which repository provides changes) and 1- Target (to which repository will it be merged)

Git Merge

Merge takes 2 branches (or/and repositories) and combines their histories.

Merging

Easy Merge

A merge is simple, if the changes are separated. For example if locally I changed File1 and remotelky someone changed File2 - both changes are separate and merging someone's changes will just work.

Let's say I changed locally File1 in lines 1 and 2, someone else changed the same file remotely by adding 2 more lines. Such a change is not a breaking one- because the changes do not affect the same lines of file.

Conflicting Changes

However, in some cases the same file will be changed in the same places. Such a change is a breaking.

Breaking Change

Solving Conflicts

We will use VS to solve conflicts.

When you attempt to merge two histories and it fails- you will get this warning

Merge Conflict- VS

If you open the file, it will look horrible! But don't worry, it's not that complex. <<< HEAD block refers to your changes and >>> branch/name block refers to outside changes. You need to pick one of the conflicting changes and solve conflicts like that.

Merge Conflict- File

Click on a file in the conflicts windows and select merge. Select the changes you want (1,2,3) and hit "Accept Merge" (4).

Merge Conflict- Resolution

A successful merge will require a commit (becuase it is yet another change) and will include histories from both branches.

Successful Merge

Mistakes Were Made?

Mistakes will happen. And that is fine, as long as you learn from them. What matters, is to be able to amend.

No Ragrets Fixing mistakes with git is easier than fixing a mispelled word on a tattoo. It's effective, in fact maybe even too effective.

Reset is the command which allows us to undo stuff.

Remember when earlier we established that Git is just moving between 3 areas? Reset is a great example of that.

Soft Reset

Undoes commits. So files move from repository to a staging area.

Mixed Reset

Undoes staging. So files move from repository to a working directory

Hard Reset

Undoes local changes. So files are deleted locally as well as from a repository.

In case such files are already pushed to a remote repository, you will need to run a forced git push command

git push -f

Which specifices that you are fine with losing changes. This is a command which cannot be undone, be extremely careful!

Danger

Git Tools

This section has a detailed guide on how to work with all the above discussed concepts.

Visual Studio

Visual Studio integrates with many code versioning systems, including Git. In fact, it has extra support even for GitHub.

Team Menu in Visual Studio

Here is a a guide to work with Git in Visual Studio

Tortoise Git

Tortoise Git is a tool to integrate with Git natively, through windows explorer extensions. If you right-click on an empty space while inside a directory, you will get a menu like this

Tortoise Git Menu

Here is a guide to Tortoise Git

Git Kraken

Git Kraken is a third party tool which many people recommend for its nice looks and simplicity.

Git Kraken

Here is a guide of how to use Git Kraken

Learn Git Branching

There is a cool website to teach you all about branching

Git Flow Guide

Atlassian has made a guide on how to properly work using Git- GitFlow. It teaches a workflow that you should follow on a daily basis.

Guide To Code Review

It is important to be able to both prepare code in an easy-to-review state and be able to review it in a polite way.

There is a nice guide by a Google engineer with their practices

Homework

Create a PR for previous lesson homework.