Version Control

Β·

9 min read

What is version control?

Version control, also known as source control, is the practice of tracking and managing changes to software code. Version control systems are software tools that helps software teams manage changes to source code over time.

Version control software keeps track of every modification to the code in a special kind of database. If a mistake is made developers can turn back the clock and compare earlier versions of the code to help fix the mistake while minizing disruption to team members.

Version control helps teams solve these kinds of problems, tracking concurrent work from conflicting. It also helps in tracking every individual change by each contributor.

Ideally, it should work on any platform, rather than dictate what operating system or toolchain developers must use.

Benefits of version control?

Using version control software is a best practice for high-performing software and devOps teams.

  1. A complete long-term change history of every file. This means every change made by many individuals over the years. Changes include the creation and deletion as well as edits of file contents.

  2. Branching and merging - Creating a branch on VCS(Version Control System) tools keep multiple streams of work independent from each other while also providing the facility to merge that work back together, enabling developers to verify changes on each branch do not conflict.

  3. traceability - Being able to trace each change made to the software and connect it to project management and bug tracking change with a message describing the purpose and intent of the change.

Source code management

Source code management (SCM) is used to track modifications to a source code repository. SCM tracks a running history of changes to a code base and helps resolve conflicts.

Source code management best practices

  • Commit often - Commits are cheap and easy to make. They should be made frequently to capture updates to a code base. Each commit is a snapshot that the codebase can be reverted to if needed.

  • Ensure you're working from latest version - SCM enables rapid updates from multiple developers. It's easy to have a local copy of the codebase fall behind the global copy.

  • Make detailed notes - Each commit has a corresponding log entry. At the time of commit creation, this log entry is populated with a message. It is preferred to write descriptive log messages.

  • Review changes before committing - SCM's offer a staging area. The staging are can be used to collect a group of edits before writing them to a commit. Staging area can also be used to manage and review changes before creating the commit snapshot.

  • Use Branches - Branches allow developers to create a separate line of development. Branches enable multiple developers to work in parallel on separate lines of development.

  • Agree on Workflow - Teams must establish shared patterns of collaboration. SCM workflows establish patterns and processes for merging branches.

What is Git?

Git is by far the most widely used version control developed by Linus Torvalds in 2005.

Having a distributed architecture, Git is an example of a DVCS (hence Distributed Version Control System). Every developer's working copy of the code is also a repository that can contain the full history of all changes.

In addition to being distributed, Git has been designed with performance, security and flexibility.

Performance

The raw performance characteristics of Git are very strong when compared to many alternatives. Committing new changes, branching, merging and comparing past versions are all optimized for performance. The algorithms implemented inside Git take advantage of deep knowledge about common attributes of real source code file trees, how they are usually modified over time and what are the access patterns.

Unlike other VCS, Git is not fooled by the name of the files when determining what the storage and version history of the file tree should be, instead, Git focuses on the file content itself. The object format for Git's repository files uses a combination of delta encoding (storing content differences), compression and explicitly stores directory contents and version metadata objects.

Security

Git has been designed with the integrity of managed source code as a top priority. The content of the files as well as the true relationships between files and directories, versions, tags and commits, all of these objects are secured with a cryptographically secure hashing algorithm called SHA1. This protects the code and the change history against both accidental and malicious change and ensures that the history is fully traceable.

Flexibility

Git is flexible in several aspects. It is efficient in both small and large projects and it is compatible with many existing systems and protocols.

It has been designed to support branching and tagging as first-class citizens and operations that affect branches and tags are also stored as part of the change history.

version control with Git

  • Git is good - It has functionality, performance, security and flexibility that most teams and individual developers need.

  • Git is a de facto standard - Git is the most broadly adopted tool of its kind.

  • Git is a quality open-source project - It is very well-supported open-source project with over a decade of solid stewardship. It enjoys great community support and a vast user base.

  • Criticism of Git - One of the common criticism is that it can be difficult to learn. It is very capable and provides a lot of power to its users. Learning to use that power can take some time.

Install Git

Install Git on Mac OS X

If you've installed XCode, git may already be installed. To find out, open a terminal and enter git --version

Apple maintains and ships [their own fork of Git](http://opensource.apple.com/source/Git/), but it tends to lag behind mainstream GIt by several major versions.

Git for Mac Installer

The easiest way to install Git on a Mac is via stand-alone installer

  1. Download the latest [Git for Mac installer](https://sourceforge.net/projects/git-osx-installer/files/).

  2. Follow the prompts to install Git.

  3. Open a terminal and verify the installation was successful by typing git --version

  4. Configure you git username and email using the following commands, replacing Emma's name with your own. These details will be associated with any commits that you create.

      git config --global user.name "Your Name"
    
     git config --global user.email "your_email@example.com"
    

Install Git with Homebrew

If you have [installed Homebrew](http://brew.sh/) to manage packages on OS X, you can follow these instructions to install Git:

  1. Open your terminal and install Git using homebrew: brew install git

  2. Verify the installation was successful by typing which git --version

  3. Configure git username and email

      git config --global user.name "Your Name"
    
     git config --global user.email "your_email@example.com"
    

Install Git on Windows

Git for Windows stand-alone installer

  1. Download the latest [Git for Windows installer](https://git-for-windows.github.io/)

  2. When you've success fully started the installer, you should see Git Setup wizard screen. Follow the Next and Finish prompts to complete the installation.

  3. Configure Git username and email

      git config --global user.name "Your Name"
    
     git config --global user.email "your_email@example.com"
    

Install Git on Linux

Debian/Ubuntu (apt-get)

  1. From you shell, install Git using apt-get

     sudo apt-get update;sudo apt-get install git
    
  2. Verify the installation was successful by typing git --version

  3. Configure your Git username and email

     git config --global user.name "Your Name"
    
     git config --global user.email "your_email@example.com"
    

Git SSH

What is a Git SSH Key?

An SSH key is an access credential for the SSH (secure shell) network protocol. This authenticated and encrypted secure network protocol is used for remote communication between machines on an [unsecured open network](https://whatismyipaddress.com/unsecured-network).

SSH uses a pair of keys to initiate a secure handshake between remote parties. It is used for remote file transfer, network management and remote OS access. The key pair contains a public and private key.

How to Create an SSH Key?

SSH keys are generated through a public key cryptographic algorithm, the most common being RSA or DSA. At a very high-level SSH keys are generated through a mathematical formula.

Generate an SSH Key on Mac and Linux

Both OS X and Linux operating systems have comprehensive modern terminal applications that ship with SSH suite installed.

  1. execute the following to begin the key creation

     ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
  2. You will then be prompted to "Enter a file in which to save the key." You can specify a file location or press Enter to accept the default file location

  3. The next prompt will ask for a secure passphrase. A passphrase will add a layer of security to the SSH and will be required every time the SSH key is used

  4. Add the new SSH key to the ssh-agent

    The ssh-agent is another program that is part of the SSH toolsuite. The ssh-agent is responsible for holding private keys it also brokers requests to sign SSH requests with the private keys so that private keys are never passed around unsecurely.

    Before adding the new SSH key to ssh-agent is running by executing `eval "$(ssh-agent -s)"

    Once the ssh-agent is running the following command will add the new SSH key to the local SSH agent.

     ssh-add -K /Users/you/.ssh/id_rsa
    

    The new SSH key is now registered and ready to use!

Generate an SSH Key on Windows

Windows environments do not have a standard default unix shell. External shell programs will need to be installed to have a complete keygen experience. The most straightforward is to utilize Git Bash.

Git Archive

How to export a git project

It can be useful to create an archive file of a Git repository. An archive file combines multiple files into a single file. An archive file can then be extracted to reproduce the individual files. Archive files remove the overhead of Git's metadata and can be simpler to distribute to other users or preserve in long term.

What does git archive do?

The git archive is a git command line utility that will create an archive file from specified Git Refs like commits, branches or trees.

Git export examples

The most basic git archive example

git archive --format=tar HEAD

This command when executed will create an archive from the current HEAD ref of the repository. By default, git archive will stream the archive output to the ephemeral stdout stream. You will need to capture this output stream to a permanent file.

git archive --output=./example_repo_archive.tar --format=tar HEAD

The above command will create a new archive and store it in the example_repo_archive.tar file. The format option also accepts popular compressed fiel formats zip and tar.gz. Passing one of these format options will produce a compressed archive.

git archive --output=./example_repo_archive.tar.gz --format=tar HEAD ./build

This command will output an archive containing only files stored under the ./build directory.

Options

--prefix=<prefix>/

The prefix options prepend a path to each file in an archive. This can be helpful to ensure the archive contents get extracted in a unique namespace.

--remote=<repo>

The remote option expects a remote repository URL. When invoked with the remote option, git archive will fetch the remote repository and create an archive from the specified ref if it's available on the remote.

Configuration

There are few global Git configuration values that git archive will respect.

tar.unmask

The unmask configuration option is used to specify unix level permission bit restriction on the output archive file.

tar.<foramt>.command

This configuration option allows the specification of a custom shell command that the `git archive output will run through. This similar to omitting the --output option and piping the stdout stream.

tar.<format>.remote

If enabled this allows remote clients to fetch archives of type format.

GIT CHEATSHEET

Did you find this article valuable?

Support Atir Nayab by becoming a sponsor. Any amount is appreciated!

Β