Skip to content

4 Version Control

Dimitris Tsapetis edited this page Dec 11, 2022 · 2 revisions

For a video presentation of the following content, please follow this link

What is Version Control?

What is version control | Atlassian Git Tutorial

Git - About Version Control

Version control is the system that records changes to a set of files and allow the developer to manage them and have access to older versions later down the softawre development path. Special software dedicated for managing versions of files are called version control software and they can track the modifications of each file and save it to a special database. A variety of such version control systems exist, such as Mercurial, Subversion and Git. The last is maybe the most widely adopted one throughout the development community.

Despite their differences in their details, the version control software aimto support the development workflow chosen by the development team, while allowing a smooth incorporation of changes, even when a plethora of developers work at the same time on a single project. The benefits such of such version control systems are the following:

File History

For each file, the entire history of changes is saved. This means that every small change is tracked by version control and the developer can at any point refer back to older version of the code to understand the changes and the reason why they were made, including addition and deletion of files. Most version control systems provide a way of comparing the differences between different version of the software, usually called diff. Additional info at each point in history include the author, date and notes on the particular changes.

Branching & Merging

As teams grow in size, the need for working at the same time becomes imperative. This functionality is enabled by branches. Each branch separates the development workflow from the main one and as a results allows for multiple streams of changes. Merging these changes acommpanies this capability, as it ensures that branches do not have contradicting changes. Depending on the workflow selected by the development team, branches can be created for either the implementation of new features or each independent release.

Traceability

Version control is also a powerful tool for the purposes of tracking the development of features/bugfixes and their link to a project management tool. The notes that accompany the changes can be directly linked to issues and provide not only the context of the changes, but also additional info in the case of code failures. Proving detailed notes is especially critical when dealing with legacy code, as future users can refer back and keep track of the changes and the reasons why they were made.

Git & Github

Chapter 8. Secrets Revealed

What is a Git Repo?

What is a Git Repository? | Beginner Git Tutorial

After installing Git on your operating system, you can use it to initialize a project with it. This generates a hidden .git folder in your project folder which is called Git Repo. The purpose of this folder is to track all changes that are happen in your project files over time. In case the developer chooses to delete the .git folder then the projects history is erased as well.

Git Basics

Reference

Creating Projects

There are two basic ways to initilize a repository with Git.

Git init

The first options is to ceate a local git repo using the init command:

git init

This command will create an empty Git repository, i.e. .git folder with subdirectories for all required git data. Automatically a default initial branch will be created, without any commits associated with it. In case git init command is run in an existing git repository, nothing will be ovewritten, so it is safe to perform. The only reason to re-run though is to add new templates to the existing git structure.

Git clone

This second command for creating a git repository is used to create a local copy of a remote repository as follows:

git clone <repository_url>

After running the last command, a new directory will be created and all the content of the remote repository will be copied. An initial branch will be checkout, which usually is the repository’s default branch. At the same time all remote-branches will be linked with each branch of the cloned repository.

Basic Snapshotting

Git Status & Add

There are multiple states a file in git can be such as untracked, modified etc. To check the state each file is on, the developer can use the git status command:

git status

The output of this command is the status of the files as well as info about the branches and commits we will see later on. Since the history of our newly create file is not tracked we want to add to the git version control system, using the git add command. Slightly different alernatives of this command are:

git add .

The later command will stage all files, while to track only a specific file the command slighlty chnages to:

git add myfile.txt

Finally to stage a whole folder, the filename can instead be replaced by the folder name, e.g.:

git add myfolder
‼️ If name of the file or folder has space, make sure to wrap it in quotes `" "`

If you want to return a file to its untracked state you can use the following command:

git rm --cached filename

or alternatively

git reset filename

How to undo "git add"

Git Commit

Commits are the basic components of Git and their purpose is to save a snapshot of the current state of a git project at a particular point of time. In contrast to other VCS, Git tracks the whole file not only its differences comapred to a previous version. As a result, at each point in time, the file does not need to be assebled by its differences over time but is readily available. There are different ways to use the git commit command, with themost basic option being:

git commit

This will create a snapshot of the staged project files. After ruuning the command the developer will be prompted to enter a meaningful commit message.

In order to create a snapshot of all changes in the current working directory we can alternatively use:

git commit -a 

To avoid being prompted to enter a commit message as previously mentioned, the git commit allows to provide the message as an argument as follows:

git commit -m "Adds myfile.txt"
‼️ **Meaningfull Commit Messages**

In case, while creating a commit we forgot to add all necessary changes we can append them to last commit using the amend option:

git commit --amend

To display a detailed list of all commits we can use the git log command:

git log

Branching and Merging

As mentioned previously, at the initiation of a git repository at least a single default branch is generated. One of the most significant VCS capabilities is to allow the creation of branches. Branches allow the developer to diverge from the main development pipeline and continue making progress on his tasks without interfering with the existing stable code base. Here, it worth mentioning that commits essentially are a pointer the latest “saved” snapshot of the code. As new commits are created, the pointer is moved to the latest commit in the git history. Creating a new branch, means that we cerate a new pointer that can move around idependent of the previous. To create a new branch from the previous one, the git branch command is used:

git branch <name_of_new_branch>

The result of this operation is a new pointer, pointing to the exact same commit as the previous branch. These identical pointers might be confusing as to how Git tracks the current branch, the developer is working on. As a result to track the current branch, Git uses a special pointer called HEAD that points to the current branch. Since the git branch command only creates a new branch and does not switch, the current HEAD should point to the branch we were previously working on.

To list all available branches, one can use the following command:

git branch -a

Git checkout

Switching between branches can be performed using the git checkout command:

git checkout <name_of_branch>

In case the developer wants to perform both the branch creation and checkout operation at the same time, the checkout command offers this capability using the -b options as follows:

git checkout -b <name_of_new_branch>

Git merge

To merge the changes between two different branches

Sharing & Updating

In most cases developers are working collaboratively with each other in a shared repository. This creates the need for VCS capabilities that allow sharing one’s contributions, as well as your current code base to accept others code.

Git fetch

The first of these set of commands is git fetch. The purpose of this command is to download recent commit, files and references from a remote repository locally. Note here that this command does not update the local source code, but only makes git aware of any remote changes. This allows for a safe way of reviewing code changes before merging. The easiest way to fetch all changes is by running the fetch command as follows:

git fetch

Git pull

Compared to git fetch, git pull is a more invasive command. It downloads the latest remote changes for the current branch using the git fetch command and depending on the whether there are changes locally, either appends the remote commits to the local git history or merges them with the local ones. The easiest way to use git pull command is the following:

git pull

Git push

Finally, in the case, changes and commits have been added to the local git history and the developer needs to share them with the rest of the team, git push command is used

git push

This command uploads the contents of the local repossitory to a remote one. Pushing is the inverse procedure of git fetch. If there are changes in the remote repository that haven’t been downloaded yet, then a error will occur. But there are also force push options that will overwrite the upstream history.

.gitignore

The .gitignore file, is a Git configuration file that specifies patterns of files or folders that will be intentially ignored by the VCS software in use. In most cases, .gitignore files are not written by hand, instead are created using autamted tools.

https://git-scm.com/docs/gitignore#:~:text=A gitignore file specifies intentionally,gitignore file specifies a pattern.

gitignore.io

Source code management best practices

Source Code Management | Atlassian Git Tutorial

Commit Often

Commits are a way of savings snapshot of the source code. Making frequent commits is highly encouraged as, these small inceremental changes are easier to manage, debug and revert if needed. Each step of implementing a new feature or resolvinga bug should have its own commit so that development steps are also more clear to the future developers that will revisit the code. The commit messages also allows for explanatory messages and details. These messages should explain both “what” and the “why” somethings has changed in the latest code snapshot.

Make sure you work from the latest version

The capabilities of modern VCS systems such as Git, allows for a rapid devleopment workflow, where changes are incorporated in an expedited pace. It that fast-paced environment it easy really easy to fall behind on the latest changes made from your collaborators. Make sure you that you pull the laetst changes every time you start working on a new branch or issue, or any time your software development day begins. Also before pushing your code, make sure that it is up to date as any conflicts or updates may result into test failure or bugs created. The same is true before opening pull requests, as your local branch version may be out of date compared to the branch it will be merged to.

Review changes before commiting

Most version control software provides the capability for a staging area. Even in a case of multiple file changes, the feature allows for collecting a group of changes that can then be bundled together in a commit. As a result, the contents and changes in each commit become for crystal clear.

Use branches

Branching is one of the most powerful tools provided by VCS. This allows multiple developers to work on different features at the same time, without creating bottlenecks. When they are done with their work, merge allows to safely incorporate their changes while keeping all new added functionality and bug fixes intact.

Git Cheatsheet

Git cheat sheet | Atlassian Git Tutorial

What is Github?

Difference Between Git and GitHub - GeeksforGeeks

In contrast to Git, which is a distributed version control system, Github is a web-based hosting service for Git repositories which offers all VCS services of Git as well as its own additional capabilities.

Untitled