Git Primer, Resources, and Cheat Sheet

Git is a distributed version control system primarily used to design computer software and develop complex web applications. Unlike other source code management systems, which rely on a client-server approach to version control, Git is based on a peer-to-peer working model. This allows software developers to work on projects independent of a centralized or common network.

Each coder's workstation contains a full copy of the working directory and repository, including all of their individual and ongoing changes and revisions. When one programmer's code is finalized, a copy can then be shared among peers or with a central server. Git has become one of the more popular open source version control systems among software developers, due in no small part to its speed, reliability, and innate data integrity.

History

Git was created by Linus Torvalds, creator and principal developer of the Linux kernel. Work on the project began in 2005, when Torvalds and his development team were forced to find an alternative to BitKeeper, the proprietary source control management system they had been using to develop and refine the Linux kernel. BitKeeper's copyright holder had withdrawn the free use of the product, so Torvalds was forced to find a version control system to fill the void. Unfortunately, none of the open source distributed systems he found were up to the task, falling short on both speed and performance.

Finding nothing that would suit his needs, Torvalds decided to develop a distributed version control system of his own. His goals were simple:

  • Use the current state of concurrent version systems as an example of what not to do;
  • Focus on a distributed work-flow;
  • Ensure that patching takes no more than 3 seconds;
  • Include safeguards against accidental or malicious corruption.

Torvalds began developing Git in April of 2005. The project's initial performance goals were soon met, and the first iteration of the system was launched before the end of the year. In 2006 Torvalds turned over maintenance of Git to Junio Hermano, one of the principal contributors to the project. Hermano has continued to develop Git, overseeing subsequent versions of the system including the latest release of Git 2.9 in 2016.

Features

To better understand Git's unique features, it is important to differentiate between the two most common types of source code management systems. concurrent version systems (CVS) rely on a single centralized server to house the full version history of a given piece of software. With this model, programmers and developers must have access to the communal server in order to review and work on the software's code. This often makes it difficult for programmers to work quickly and efficiently, especially when working as a team. Relying on a centralized server for a database or repository also means that all work on a project is dependent upon the availability of the server, and that can hamper the work of coders when they, or the server itself, are offline.

Git, however, is distributed version control system (DVCS), and as such does not depend upon a centralized server to house the full version history of any given project. Git uses a distributed architecture, meaning that every developer's working copy of an ongoing project is also a repository containing a full version history of the software being developed. This allows programmers to work in safer and more flexible coding environments, free from the need to remain connected to the communal server.

Enhanced performance features are another reason Git has become a popular alternative to other source code management systems. Git simplifies branching and merging functions, making it easier for developers to commit changes and review and compare previous versions of a project's source code. Moreover, because all of these actions are made on the developer's own workstation, the process is faster and more reliable than other systems. Git's high performance features, combined with its distributed architecture, also facilitates collaboration amongst developers, allowing them to work more successfully from remote workstations.

Finally, a key feature of Git is its inherent security. All data and files in the Git repository are protected by the SHA1 hashing algorithm. This protects the code from malicious and accidental changes. The potential for data loss with Git is also greatly reduced as compared with other source management systems, because the complete version history of any given project is stored on each contributor's workstation.

Compatibility

Git is an open source DVCS, and it can be downloaded directly from the Git website. Git is compatible with Windows, Linux, Mac OSX, and BSD operating systems.

Getting Started

Git is a powerful source coding management system, and it is a key component to many ongoing commercial and open source software projects. Those who have never used Git can often find the system confusing at first, however the learning curve is not as steep as it may appear and there are some very good online tutorials (both text and video) that provide ample instruction for beginners.

  • Git Tutorial: A Comprehensive Guide: this Udemy tutorial provides a full overview of Git, including its development history. Readers are introduced to the basic functions of the system, with sections dedicated to setting up a repository, undoing actions, branching and merging, and integration via Github.
  • Git Tutorial for Beginners: Command Line Fundamentals: this video tutorial is targeted to first time Git users, and those with at least a cursory understanding of DVCS systems. The presentation delivers a simple overview of the system, with a focus on command line fundamentals.
  • Learn Git: A Git Tutorial: presented by Tutorials Point, this comprehensive introduction and guide to Git covers all of the fundamentals of the version control system. This tutorial includes a large number of visual aids to help demonstrate Git in action. Specific sections addressed include operation creation, performing and committing changes, updating operations, patching, and managing branches.
  • Git Tutorial for Beginners: A Quick Start Guide: Presented by Timothy Corey, this video tutorial is a highly effective introduction to Git for beginners. The use of real world examples makes this tutorial particularly easy to follow and understand.

Books

Git is a highly versatile version control system, and to realize its full potential users need a comprehensive understanding of the system and its capabilities. Online tutorials can offer a basic introduction to Git, but most are limited in their scope. For a more in depth look at Git and its uses, developers and programmers will want to explore some of the following books.

  • Pro Git (2009) by Chacon and Straub: recommended by the Git development team, Chacon and Straub's Pro Git provides an overview of distributed version control and the development of Git and GitHub. The authors explain the basics of Git and its many features, from the standpoint of both programmers and project leaders. Available in both print and digital editions.
  • Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development (2012) by Loeliger and McCullough: this practical guide to Git takes readers from initial concepts to advanced techniques using step-by-step examples. Topics include tracking, branching, merging, and managing code revisions with an emphasis on collaborative software development.
  • Git in Practice (2014) by Mark McQuaid: best suited for those already acquainted with Git's core principles, McQuaid's book takes a detailed look at advanced techniques to optimize and enhance team development projects. Git in Practice covers more than 60 advanced uses for Git, including history visualization, advanced branching, rewriting history and disaster recovery.

Conclusion

Git has become one of the most widely used version control systems in the industry, due in no small part to its speed, enhanced performance capabilities, and overall versatility. As an open source system Git continues to undergo further development and revisions, evolving to meet the ever changing needs of professional and amateur software developers. The resources discussed here should provide a helpful introduction to Git, its application to collaborative development projects, and its status in the hierarchy of version control systems.


Git Cheatsheet

Git is an open source distributed version control system for use in software design and the development of web applications. The following cheat sheet contains a sampling of the most commonly used Git command line instructions.

Configure Tooling

$ git config --global user.name "[name]" — assigns a name to your commit transactions
$ git config --global user.email "[email address]" — assigns an email to your commit transactions
$ git config --global color.ui auto — enables colorization of command line output

Creating Repositories

$ git init [project-name] — creates a new local repository with a specified name
$ git clone [url] — clones a current repository with its complete version history

Local Changes

$ git status — lists all new or modified files to committed
$ git diff — shows file references not staged
$ git add[file] — snapshot of the file prior to versioning
$ git diff --staged — shows file differences between staging and last file version
$ git reset [file] — unstages the file while preserving its content
$ git commit — commits previously staged changes
$ git commit -a — commits all local changes in tacked files
$ git commit -m "[descriptive message]" — permanently records file snapshots in version history
$ git log — shows history of commits
$ git log -p [filename] — shows history of changes to a specific file
$ git blame [file] — shows which user changed file content and when

Branches and Tags

$ git branch -av — lists all existing branches
$ git checkout [branch] — switch HEAD branch
$ git checkout [new-branch] — creates a new branch based on the current HEAD
$ git checkout -- track [remote/branch] — creates a new tracking branch based on a remote branch
$ git branch -d [branch] — deletes a local branch
$ git tag [tag-name] — marks the current commit with a tag
$ cat .git/HEAD — shows what the HEAD, or current branch, is pointing to

Updates and Publishing

$ git remote -v — list all currently configured remote repositories
$ git show remote [remote] — shows detail information from a specific remote repository
$ git remote add [shortname] [url] — add a new remote repository
$ git fetch [remote] — download all changes from a specified remote without integrating into HEAD
$ git pull [remote] [branch] — download changes from a specific remote repository and integrate/merge into HEAD
$ git push [remote] [branch] — publish local changes to a remote repository
$ git branch -dr [remote/branch] — deletes a branch on the local remote repository
$ git push --tags — publish tags to repository

Merge and Rebase

$ git merge [branch] — merges specified branch into current HEAD
$ git rebase [branch] — rebase current HEAD onto branch
$ git rebase --abort — aborts previous rebase
$ git rebase --continue — continue rebase after resolving conflicts
$ git add [resolved-file] — manually edit and resolve conflicts and mark file as 'resolved'
$ git mergetool — use merge tool to automatically resolve conflicts

Undo

$ git reset --hard HEAD — discards all local changes in the working directory
$ git checkout HEAD [filename] — discards all local changes to a specified file
$ git revert [commit] — revert a commit by producing a revised or updated commit
$ git reset --hard [commit] — reset the HEAD pointer to a previous commit and discard all subsequent changes
$ git reset [commit] — reset the HEAD pointer to a previous commit and preserve all subsequent changes as unstaged events
$ git reset --keep [commit] — reset the HEAD pointer to a previous commit and preserve uncommitted changes

This short reference guide represents an overview of the most common command line instructions used with the Git version control system. It should not be seen as exhaustive, but should prove useful for basic programming and coding operations.


Further Reading and Resources

We have more guides, tutorials, and infographics related to coding and development:

  • Version Control and Hosting: not just about hosting, but also a comparison of version control systems.
  • Ubuntu Primer: learn all about one of the most popular Linux distributions — an excellent base for MantisBT hosting.
  • Object-Oriented Programming: learn about the wide range of object-oriented programming languages — some may surprise you.

Would the Internet Survive the End of the World?

Have you ever wondered what a major catastrophe would do to the internet? Check out our infographic, Would the Internet Survive the End of the World? It's possible we could all be destroyed but the internet would live on.