{"id":156983,"date":"2016-07-01T14:00:41","date_gmt":"2016-07-01T14:00:41","guid":{"rendered":"https:\/\/premium.wpmudev.org\/blog\/?p=156983"},"modified":"2022-03-11T00:02:02","modified_gmt":"2022-03-11T00:02:02","slug":"git-for-wordpress-development","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/git-for-wordpress-development\/","title":{"rendered":"How to Use Git for WordPress Development"},"content":{"rendered":"<p>You know <a href=\"https:\/\/git-scm.com\/\" target=\"_blank\">Git<\/a>, right? It&#8217;s one of the most <a href=\"http:\/\/programmers.stackexchange.com\/questions\/136079\/are-there-any-statistics-that-show-the-popularity-of-git-versus-svn\" rel=\"noopener\" target=\"_blank\">popular version control systems<\/a> in use today. Plus, it&#8217;s everywhere: Pre-installed by many hosting providers, used as the system that operates behind GitHub and Bitbucket, and now even built into easy-to-use WordPress plugins.<\/p>\n<p>There&#8217;s been a lot of noise about Git in the WordPress community lately thanks to the development of Git-powered tools like VersionPress, WP Pusher, Revisr, and Gitium. Like many other WordPress professionals, I&#8217;m a freelancer working primarily on an independent basis and I&#8217;ve never had to embrace version control. However, all of the Git noise as-of-late has led me to a realization: My Git skills need some attention.<\/p>\n<p>Maybe, like me, you&#8217;re one of the many WordPress freelancers and developers who haven&#8217;t yet fully embraced Git or another version control system. Maybe you&#8217;re part of a small development team that has realized you need a better way to keep track of your collaborative projects. Or maybe you&#8217;re studying WordPress development and want to learn Git, because at this point version control expertise is a skill expected out of every professional developer.<\/p>\n<p>I&#8217;ve researched how Git is being used by experienced WordPress developers and the tools available to make it easier to pair Git and WordPress. In this article, I&#8217;ll share what I&#8217;ve learned, and point out resources you can use to get started with Git for WordPress.<\/p>\n<div  class=\"wpdui-pic-large   \" ><img loading=\"lazy\" decoding=\"async\" class=\"attachment-1364x1364 size-1364x1364\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/06\/git.png\" alt=\"screenshot of git-scm.com website\" width=\"1364\" height=\"357\" \/><\/div>\n<p>Continue reading, or jump ahead using these links:<\/p>\n<ul>\n<li><a href=\"#what-is-git\">What is Git?<\/a><\/li>\n<li><a href=\"#where-does-git-live\">Where Does Git Live?<\/a><\/li>\n<li><a href=\"#how-do-word-press-developers-use-git\">How Do WordPress Developers Use Git?<\/a><\/li>\n<li><a href=\"#get-started-learning-git\">Get Started Learning Git<\/a><\/li>\n<\/ul>\n<h2 id=\"what-is-git\">What is Git?<\/h2>\n<p>Git is a version control system. I&#8217;m sure you already knew that, but what does Git actually do?<\/p>\n<p>Let&#8217;s look at a simple example of what Git can do to track the creation and editing of a document:<\/p>\n<ol>\n<li>The document is created.<\/li>\n<li>The document is saved.<\/li>\n<li>The document is edited.<\/li>\n<li>The changes are saved.<\/li>\n<\/ol>\n<p>The first two steps only happen once. However, the last two steps typically happen many, many, many times.<\/p>\n<p>What Git does is:<\/p>\n<ul>\n<li>Keep track of every time modifications are saved.<\/li>\n<li>Include a history of the state of the document before and after the modification.<\/li>\n<li>Provide you, the author or editor, with the opportunity to add a note explaining the nature of each change.<\/li>\n<\/ul>\n<p>The result is that Git creates a complete history of every change made to the document from its inception, with the ability to roll the document back to any prior state.<\/p>\n<h3>Don&#8217;t Just Track a Document, Track a Project<\/h3>\n<p>Git isn&#8217;t limited to tracking a single document. As a matter of fact, Git tracks all of the activity in a specified folder including any subdirectories of that folder.<\/p>\n<p>Most web applications, such as WordPress, are composed of multiple files that reside inside of a directory and multiple subdirectories. Git can be used to keep track over every change in one or all of these directories and subdirectories, and you can even tell Git to ignore certain files and directories as well.<\/p>\n<p>Just like the single document history we mentioned, Git can be used to keep track of the history of an entire project that consists of a top-level directory, multiple subdirectories, and dozens of files.<\/p>\n<div  class=\"wpdui-pic-regular  \">\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735 size-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/06\/git-collaboration.png\" alt=\"screenshot of collaboration from git website\" width=\"670\" height=\"223\" \/><figcaption class=\"wp-caption-text\">A visual explanation of how Git enables collaboration. Source: git-scm.com.<\/figcaption><\/figure>\n<\/div>\n<h3>And Don&#8217;t Just Track a Project, Collaborate<\/h3>\n<p>Git really begins to shine when it is used in a collaborative environment. Git is designed to maintain a central version of a project so that multiple contributors can work on project files <em>simultaneously<\/em> and merge all of their changes with the central version.<\/p>\n<p>Git even notices conflicts between edits made by different team members and provides the opportunity to resolve those conflicts.<\/p>\n<h3>And Don&#8217;t Just Collaborate on a Project, Iterate<\/h3>\n<p>Let&#8217;s say you&#8217;re working on a project with a group of collaborators and you have an idea you want to try out without committing the entire project to go in that direction. Git makes this easy.<\/p>\n<p>With Git you just create a new branch of the project, work out your idea, and then discard your branch if it doesn&#8217;t work out, merge it with the original project if it does, or save it as an entirely new project (referred to as forking \u2013\u00a0<a href=\"https:\/\/codex.wordpress.org\/History\" rel=\"noopener\" target=\"_blank\">WordPress was born as a fork of b2\/cafelog<\/a>, an earlier blogging platform).<\/p>\n<p>Change your mind about all those changes after a good night&#8217;s sleep? No problem. With Git&#8217;s version history, you can rollback the changes and undo the modifications.<\/p>\n<h2 id=\"where-does-git-live\">Where Does Git Live?<\/h2>\n<p>Most Git workflows involve managing the flow of a project between three different locations:<\/p>\n<ul>\n<li><strong>A local development environment:<\/strong> Git is used locally to track changes as you work on a local copy of a development project. You can optionally push a project from the local development environment to a Git host, or just keep it local if you&#8217;re the only one working on it.<\/li>\n<li><strong>A Git host:<\/strong> GitHub and Bitbucket are two of the most popular Git hosts, but there are others. A Git host is a place where the master version of a project resides so that more than one developer can work on it at a time.<\/li>\n<li><strong>A web server:<\/strong> With Git installed on the server, use SSH to clone the repository from the Git host you selected to the directory where the website will reside on the web server. More and more hosts offer Git installed even on low-cost shared servers.<\/li>\n<\/ul>\n<h2 id=\"how-do-word-press-developers-use-git\">How Do WordPress Developers Use Git?<\/h2>\n<p>What does a WordPress development workflow look like with Git thrown in the mix?<\/p>\n<p>WordPress developer <a href=\"http:\/\/joshpress.net\/\" rel=\"noopener\" target=\"_blank\">Josh Pollock<\/a> detailed his workflow in our article <a href=\"https:\/\/wpmudev.com\/blog\/improve-wordpress-development-workflow-local-server\/\" target=\"_blank\">No More Cowboy Coding: Improving Your WordPress Workflow<\/a>.<\/p>\n<p>Josh uses Git in three different places: A local development environment, a private Bitbucket Git repository, and a web server hosting the production version of the WordPress site.<\/p>\n<p>Here&#8217;s how Josh&#8217;s system works:<\/p>\n<ul>\n<li>The official version of a site lives in the Bitbucket Git repository.<\/li>\n<li>SSH is used to initiate command-line Git requests on the webserver to pull or push project files between Bitbucket and the server.<\/li>\n<li>SourceTree, a Git GUI, is used to initiate Git requests locally to pull or push project files between Bitbucket and the local development environment.<\/li>\n<li>Git, initiated over SSH on the webserver and manipulated with SourceTree locally, is used to track changes and push\/pull files, but an additional plugin, <a href=\"https:\/\/deliciousbrains.com\/wp-migrate-db-pro\/\" rel=\"noopener\" target=\"_blank\">WP Migrate DB Pro<\/a>, is used to make sure the database migrates back and forth.<\/li>\n<\/ul>\n<p>This isn&#8217;t the only model for using Git and WordPress, but the three-part model \u2013 local, Git host, and web server \u2013 is pretty universal. The differences center on the software tools used to interact with Git and the choice of Git host.<\/p>\n<h3>WordPress-Git Red Flags<\/h3>\n<p>There are a few problems endemic to using Git and WordPress together:<\/p>\n<ul>\n<li>How do you handle <em>wp-config.php<\/em> since it will be different on the local and production servers and you don&#8217;t want it exposed in a public repository?<\/li>\n<li>How do you deal with media files? Does it make sense to copy them all from the production server to your local development environment on a periodic basis?<\/li>\n<li>Does it make sense to include the entire WordPress core and third-party themes and plugins in your Git repository, or should you limit Git&#8217;s tracking to custom plugins and custom theme modifications?<\/li>\n<li>How do you keep the database updated between the local and production servers?<\/li>\n<\/ul>\n<p>You can find out how Josh recommends dealing with those issues by reading the full post to get all of the details.<\/p>\n<p>Meanwhile, full-stack developer <a href=\"https:\/\/stevegrunwell.com\/\" rel=\"noopener\" target=\"_blank\">Steve Grunwell<\/a> explains his workflow at the website <a href=\"http:\/\/stevegrunwell.github.io\/wordpress-git\/\" rel=\"noopener\" target=\"_blank\">Keeping WordPress Under [Version] Control with Git<\/a>.<\/p>\n<p>Steve&#8217;s workflow looks an awful lot like Josh&#8217;s. However, it&#8217;s worth taking a look at because Steve&#8217;s explanation includes two valuable, time-saving bits of information.<\/p>\n<ul>\n<li>He provides text for a recommended <a href=\"https:\/\/help.github.com\/articles\/ignoring-files\/\" rel=\"noopener\" target=\"_blank\">.gitignore<\/a> file.<\/li>\n<li>He provides an <code>IfModule<\/code> you can drop into your local site&#8217;s <em>.htaccess<\/em> file.<\/li>\n<\/ul>\n<p>These files work together to do two things: limit the directories and files included in the Git repository and tell the local version of the site to load media files from the webserver. This eliminates the question of whether or not you should copy media to your local environment and excludes certain directories and files (such as <em>wp-config.php<\/em>) from your Git repository.<\/p>\n<p>Steve&#8217;s workflow description is not entirely beginner-friendly. It leaves several assumed steps out&#8211;such as the fact that the <code>IfModule<\/code> should be dropped into the local <em>.htaccess<\/em> file. However, once you have some Git experience, it will all make a lot more sense.<\/p>\n\n<h3>Git Tools<\/h3>\n<p>Now that we have a pretty idea for how WordPress developers use Git to manage WordPress projects, let&#8217;s take a look at some of the Git tools used by WordPress developers.<\/p>\n<h3>Git GUIs<\/h3>\n<p>Git was initially designed to run from the command line, and many developers still use the command line to work with Git. However, you can get away from using the command line to work with Git locally if you want to. If both Git and the command line are foreign to you, using a Git GUI can make the process of learning Git easier by allowing you to skip the command line for now.<\/p>\n<p><strong><a href=\"https:\/\/www.sourcetreeapp.com\/\" rel=\"noopener\" target=\"_blank\">SourceTree<\/a><\/strong> is a very popular and free Git client that integrates with both Bitbucket and GitHub. <strong><a href=\"https:\/\/desktop.github.com\/\" rel=\"noopener\" target=\"_blank\">GitHub Desktop<\/a><\/strong> is another popular option, but one that only works with GitHub. There are several other <a href=\"https:\/\/git-scm.com\/download\/gui\/linux\" rel=\"noopener\" target=\"_blank\">Git GUIs<\/a> you can consider. Just make sure that the client you settle on works with the Git host you plan on using.<\/p>\n<h3>Git Hosts<\/h3>\n<p>A Git host is a home for your Git repositories on the web. You don&#8217;t <em>have<\/em> to use a Git host. It&#8217;s entirely possible to push changes from your local environment straight to your web server with SSH or just to use Git for local version control.<\/p>\n<p>Using a Git host means you have a complete backup of your website files in a place where it&#8217;s easy to push them to a web server over SSH at a moment&#8217;s notice. In addition, if you want to collaborate with another developer, using a Git host to manage and merge changes to the project will make the process a lot easier.<\/p>\n<p>Obviously, <strong><a href=\"https:\/\/github.com\/\" rel=\"noopener\" target=\"_blank\">GitHub<\/a><\/strong> is the most well-known Git host. However, they are far from the only option and really only a great option if you want your repositories to be public or don&#8217;t mind paying a few dollars every month for private repositories.<\/p>\n<p><strong><a rel=\"noopener\" target=\"_blank\">Bitbucket<\/a><\/strong> is another very popular option, especially for developers who prefer to use private repositories. Another well-known option is <strong><a href=\"https:\/\/about.gitlab.com\/\" rel=\"noopener\" target=\"_blank\">GitLab<\/a><\/strong> and there are several <a href=\"https:\/\/www.git-tower.com\/blog\/git-hosting-services-compared\/\" rel=\"noopener\" target=\"_blank\">additional Git hosts<\/a> if you aren&#8217;t sold on GitHub, Bitbucket, or GitLab.<\/p>\n<h3>Git Tools for WordPress<\/h3>\n<p>You don&#8217;t have to use any WordPress-specific tools to incorporate Git into your workflow. However, since you&#8217;re getting started with Git, there are several WordPress tools you will definitely want to know about.<\/p>\n<h3 class=\"wpdui-tutorial-list__item__title\">VersionPress<\/h3>\n<figure id=\"attachment_206626\" class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-206626\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/07\/VersionPress.png\" alt=\"VersionPress logo.\" width=\"600\" height=\"117\" \/><figcaption class=\"wp-caption-text\">VersionPress is a free plugin that&#8217;ll bring git to your WordPress.<\/figcaption><\/figure>\n<p><a href=\"https:\/\/versionpress.com\/\" rel=\"noopener\" target=\"_blank\">VersionPress<\/a> creates a Git repository on your site\u2019s web server and tracks every single change you make. It also adds a dashboard to the WordPress admin where there is a literal\u00a0<em>undo<\/em> button available for every change. Plus, a great aspect of this plugin is &#8212; it&#8217;s free!<\/p>\n<p>You can use VersionPress to undo any change you make, including rolling back the WordPress core after an upgrade, undoing a plugin update, or uninstalling a theme. Also, if you\u2019re comfortable with WP-CLI, you can use VersionPress to create staging sites which you can then merge with the production site once your staging site is ready for public consumption. Our previous post about VersionPress will help you with\u00a0<a href=\"https:\/\/wpmudev.com\/blog\/versionpress\/\" target=\"_blank\">Setting Up VersionPress for Git-Powered WordPress Version Control<\/a>.<\/p>\n<h3 class=\"wpdui-tutorial-list__item__title\">WP Pusher<\/h3>\n<figure id=\"attachment_206627\" class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-206627\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/07\/wp-pusher.png\" alt=\"wp pusher header.\" width=\"600\" height=\"117\" \/><figcaption class=\"wp-caption-text\">With WP Pusher, you can deploy your plugins and themes directly from GitHub, Bitbucket or GitLab.<\/figcaption><\/figure>\n<p><a href=\"https:\/\/wppusher.com\/\" rel=\"noopener\" target=\"_blank\">WP Pusher<\/a> connects your WordPress website to a WordPress plugin or theme hosted in a GitHub repository. This makes it much easier to install and update WordPress plugins and themes that are hosted at GitHub.<\/p>\n<p>Think of WP Pusher as a bridge for plugins and themes that connects your WordPress site to GitHub. Without WP Pusher, you have to use SSH or FTP to use plugins or themes hosted at GitHub. With WP Pusher you can easily install or update GitHub-hosted plugins and themes right from the WordPress dashboard. This means WP Pusher is also a useful development tool when developing your own custom plugins and themes and using GitHub for versioning.<\/p>\n<p>WP Pusher is free if the plugins and themes you use are hosted in public GitHub repositories. However, working with private repositories will trigger the requirement to purchase a license.<\/p>\n<h2 id=\"get-started-learning-git\">Get Started Learning Git<\/h2>\n<p>If you&#8217;ve made it this far, you must be seriously committed to learning Git and using it to improve your WordPress development workflow. I&#8217;ve tracked down some high-quality Git education that will take you from Git beginner to competent user in less than eight hours.<\/p>\n<ul>\n<li><a href=\"https:\/\/try.github.io\" rel=\"noopener\" target=\"_blank\">Try Git<\/a> is a quick interactive introduction to Git from Code School and GitHub. Burn through it in 15 minutes to get a feel for how Git works and to learn some of the basic commands. <em>Time to complete: 15 minutes.<\/em><\/li>\n<li>The <a href=\"https:\/\/git-scm.com\/videos\" rel=\"noopener\" target=\"_blank\">Git Basics<\/a> video series provides a high-level overview of version control and Git. <em>Time to complete: 30 minutes or less.<\/em><\/li>\n<li><a href=\"https:\/\/www.codecademy.com\/learn\/learn-git\" rel=\"noopener\" target=\"_blank\">Learn Git<\/a> is a free course from Codecademy that will get you comfortable working with basic Git commands such as <code>init<\/code>, <code>status<\/code>, <code>add<\/code>, <code>diff<\/code>, <code>commit<\/code>, and <code>log<\/code>. <em>Time to complete: 2 hours.<\/em><\/li>\n<li>You don&#8217;t have to use command line to work with Git locally, but most developers do. Also, if you&#8217;re going to use Git on a server over SSH, you will have to use the command line. For this reason, I&#8217;ve included <a href=\"https:\/\/www.codecademy.com\/learn\/learn-the-command-line\" rel=\"noopener\" target=\"_blank\">Learn Command Line<\/a> on this list. It&#8217;s another free course from Codecademy that will teach you how to use the command line to navigate between diretories and files. <em>Time to complete: 3 hours.<\/em><\/li>\n<li>Git for WordPress Developers includes 35 screencasts put together by the developer behind WP Pusher. Screencasts cover topics like ignoring files, advanced Git commands, and deployment strategies. <em>Time to complete: 2 hours or less.<\/em><\/li>\n<\/ul>\n<p>Make it through those free resources and you&#8217;ll be equipped to use Git competently while you continue to learn as you incorporate Git into your workflow. However, if you really want to develop professional-grade version control chops, then take a look at <a href=\"https:\/\/git-scm.com\/book\/en\/v2\" rel=\"noopener\" target=\"_blank\">Pro Git<\/a> by Scott Chacon and Ben Straub. This 500+ page book is available for free in a varity of ebook formats, or you can get a dead tree version from Amazon.<\/p>\n<p>The Git website also provides a list of many additional <a href=\"https:\/\/git-scm.com\/documentation\/external-links\" rel=\"noopener\" target=\"_blank\">useful resources for learning Git<\/a> in case you don&#8217;t see exactly what you&#8217;re looking for in the list above.<\/p>\n<h2>Wrapping Up<\/h2>\n<p>WordPress is growing up. It&#8217;s transitioning from being a blogging platform to being the beating heart of a powerful CMS and web application platform. Integrating version control into mainstream WordPress development workflows is natural step in the maturation of the platform.<\/p>\n<p>If you&#8217;re serious about staying on the cutting edge of WordPress development \u2013 and I know you are, because you&#8217;re reading this blog \u2013 you have to add Git competency to your WordPress developer toolbox. Making it through this guide to Git for WordPress is a great first step.<\/p>\n<p>Next, schedule the time to work through one or more of the recommended free resources and start integrating Git into your WordPress development workflow.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You know Git, right? It&#8217;s one of the most popular version control systems in use today. Plus, it&#8217;s everywhere: Pre-installed by many hosting providers, used as the system that operates behind GitHub and Bitbucket, and now even built into easy-to-use WordPress plugins. There&#8217;s been a lot of noise about Git in the WordPress community lately [&hellip;]<\/p>\n","protected":false},"author":388460,"featured_media":157014,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[557],"tags":[10074,390,10409,10356],"tutorials_categories":[],"class_list":["post-156983","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-git","tag-code","tag-github","tag-version-control"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/156983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/388460"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=156983"}],"version-history":[{"count":18,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/156983\/revisions"}],"predecessor-version":[{"id":206898,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/156983\/revisions\/206898"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/157014"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=156983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=156983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=156983"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=156983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}