{"id":1623,"date":"2017-04-21T18:38:31","date_gmt":"2017-04-22T01:38:31","guid":{"rendered":"https:\/\/blogs.ubc.ca\/coetoolbox\/?p=1623"},"modified":"2017-04-24T16:19:53","modified_gmt":"2017-04-24T23:19:53","slug":"getting-started-with-git-and-github","status":"publish","type":"post","link":"https:\/\/blogs.ubc.ca\/coetoolbox\/2017\/04\/21\/getting-started-with-git-and-github\/","title":{"rendered":"Getting Started with Git and GitHub"},"content":{"rendered":"<p class=\"md-end-block md-heading\"><span class=\"md-line md-end-block\">If you write code, you should be using a version control system. At it&#8217;s most basic, version control tracks changes in files over time and allows you to easily view differences and restore the last working version if you break something (which you will). Most version control systems work at the file level so they don&#8217;t\u00a0care if you&#8217;re writing R, Python, GAMS, C++ or CSS or just text like this blog post. <\/span><\/p>\n<p><span class=\"md-line md-end-block\">Using version control starts with tracking changes on your computer, but using a server allows you to collaborate with others on the same code. Changes are captured at the line level so they can be merged automatically with changes from other people in your team. There are also tools to resolve occasional conflicts that arise when multiple people modify the same line.<\/span><\/p>\n<p><span class=\"md-line md-end-block\"><span class=\"\">Using version control also helps emphasize repeatability in analysis projects. Your work should start with source data, follow a repeatable (possibly automated) set of analysis steps and produce figures, reports, trained models or other outputs. For the COE, your source data is kept in the shared drive and on your local machine and the code is kept on your machine and in version control. The output of the code is treated as temporary. Anyone who has a copy of the data should be able to run your code on their computer and reproduce all the figures, models and reports as needed.<\/span><\/span><\/p>\n<h2 class=\"md-end-block md-heading\">Git and GitHub<\/h2>\n<p><span class=\"md-line md-end-block\">There are many version control systems including <span class=\"\"><a spellcheck=\"false\" href=\"https:\/\/subversion.apache.org\/\">Subversion<\/a><\/span>, <span class=\"\"><a spellcheck=\"false\" href=\"https:\/\/www.mercurial-scm.org\/\">Mercurial<\/a><\/span> and <span class=\"\"><a spellcheck=\"false\" href=\"https:\/\/git-scm.com\/\">Git<\/a><\/span>. Each has many users and benefits but the most common in our industry is Git, largely because of the excellent web service <span class=\"\"><a spellcheck=\"false\" href=\"https:\/\/github.com\/\">GitHub<\/a><\/span>. While Git is the software that runs on your computer, GitHub is a site which will host your code and allow you to share it with your team or outside collaborators. They also provide a desktop application which is much friendlier than the Git command line interface.<\/span><\/p>\n<h2 class=\"md-end-block md-heading\">Setting Up Your Account<\/h2>\n<ol class=\"ol-list\" start=\"\">\n<li class=\"md-focus-container\"><span class=\"md-line md-end-block md-focus\"><span class=\"md-expand\">When Stuart and the client have approved using GitHub for your project, you will receive an email inviting you to the COE&#8217;s GitHub account. Follow the instructions to accept the invitation and join the organization. From the organization&#8217;s home page, you should be able to see several repositories that you can browse but you won&#8217;t be able to modify. Access to repositories is governed by client teams so if you&#8217;re working with WorkSafeBC you&#8217;ll have access to your repository and past projects. <\/span><\/span><\/li>\n<li class=\"\"><span class=\"md-line md-end-block\">Sign up for a GitHub account with your COE email address and a <span class=\"\"><strong>strong<\/strong><\/span> password. It is required that\u00a0\u00a0your account to use two-factor authentication so make sure you set that up at this point.\u00a0<span class=\"\"><em>Editorial Note<\/em><\/span>: I highly encourage you to use 2FA everywhere it&#8217;s supported. If you aren&#8217;t aren&#8217;t familiar, check out <span class=\"\"><a spellcheck=\"false\" href=\"http:\/\/www.slate.com\/articles\/technology\/future_tense\/2017\/02\/how_to_set_up_two_factor_authentication.html\">this article<\/a><\/span>. While we&#8217;re talking about passwords, consider a <span class=\"\"><a spellcheck=\"false\" href=\"https:\/\/www.wired.com\/2016\/01\/you-need-a-password-manager\/\">password manager<\/a><\/span> and please, please, <span class=\"\"><em>please<\/em><\/span> for your sake don&#8217;t use the same password in multiple places. <\/span><\/li>\n<li class=\"\"><span class=\"md-line md-end-block\">Next, look for the application called GitHub on your computer. If its not already installed, download and run the <span class=\"\"><a spellcheck=\"false\" href=\"https:\/\/desktop.github.com\/\">GitHub Desktop Installer<\/a><\/span><span class=\"\">. It installs in your user directory so you shouldn&#8217;t need admin privileges. <\/span><\/span><\/li>\n<\/ol>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/github_desktop.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1611\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/github_desktop.png\" alt=\"\" width=\"524\" height=\"410\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/github_desktop.png 949w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/github_desktop-300x235.png 300w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/github_desktop-768x601.png 768w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/github_desktop-624x489.png 624w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\" \/><\/a><\/p>\n<ol class=\"ol-list\" start=\"4\">\n<li class=\"\"><span class=\"md-line md-end-block\">Open the GitHub app and sign in with your credentials and two-factor code. Once you&#8217;re signed in you&#8217;ll see a blue plus icon in the top left corner that you can use to add a repository. You can either <span class=\"\"><strong>add<\/strong><\/span><span class=\"\"> an existing repository on your computer, <\/span><span class=\"\"><strong>create<\/strong><\/span> a new repository on your computer, or <span class=\"\"><strong>clone<\/strong><\/span> an existing repository from GitHub. An empty repository for your project should already exist on GitHub so click &#8220;Clone&#8221;, select COE-UBC from the left panel and your project from the right. <\/span><span class=\"md-line md-end-block\">Once you click the check to clone your repository, you&#8217;ll be prompted to choose a location to store the repository. Any location on your local machine is fine. It&#8217;s not backed up, but that&#8217;s okay becuase the canonical version of your code will live on GitHub and everything in your local repository can be recreated.<\/span><\/li>\n<\/ol>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/clone_repo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1602\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/clone_repo.png\" alt=\"\" width=\"474\" height=\"298\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/clone_repo.png 569w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/clone_repo-300x189.png 300w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<ol class=\"ol-list\" start=\"5\">\n<li><span class=\"md-line md-end-block\">Back at the main GitHub Desktop screen you should now see your repo highlighted in the panel on the left. As you add more repos, this list will grow so you can keep track of several projects at the same time. In the main panel you will see a timeline, a list of the commit history for the project and details for the selected commit. <\/span><\/li>\n<\/ol>\n<h2 class=\"md-end-block md-heading\">Git Basics<\/h2>\n<p><span class=\"md-line md-end-block\">In this section we&#8217;ll cover the basic commands in Git that you&#8217;ll use to keep track of changes to your files. You&#8217;re going to create a practice repository and fill it with files that we&#8217;ll sync with GitHub. <\/span><\/p>\n<h3 class=\"md-end-block md-heading\">Creating a new repository<\/h3>\n<p><span class=\"md-line md-end-block\">To get started let&#8217;s create a new repository called &#8220;CouncilBudget&#8221; in the default location. Click on the plus in the top left, then create and type in the name. The location should be the default <span spellcheck=\"false\"><code>Documents\\GitHub<\/code><\/span>. GitHub desktop also gives you the opportunity to create a <span spellcheck=\"false\"><code>gitignore<\/code><\/span> file that is pre-populated to exclude helper\u00a0files for specific languages and operating systems. We&#8217;re going to use the default <span class=\"\"><em>Windows<\/em><\/span> option for now. <\/span><\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/create_councilbudget.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1607\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/create_councilbudget.png\" alt=\"\" width=\"482\" height=\"260\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/create_councilbudget.png 562w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/create_councilbudget-300x162.png 300w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><\/a><\/p>\n<p>Click create repository to intialize the repository on your system. Notice that all we did was create a folder in called <span spellcheck=\"false\"><code>CouncilBudget<\/code><\/span> with a couple files in it. GitHub automatically creates the first commit for us which contains two text files with instructions for how Git should behave. The <span spellcheck=\"false\"><code>.gitignore<\/code><\/span> file is a list of rules for files that Git should ignore (open it up and have a look) and <span spellcheck=\"false\"><code>.gitattributes<\/code><\/span> is a configuration file that we don&#8217;t need to pay attention to at this point. If hidden folders are visible on your system, you&#8217;ll notice that a new folder called <span spellcheck=\"false\"><code>.git<\/code><\/span> was also created. This is where Git keeps track of everything for you and you don&#8217;t need to bother with it at all.<\/p>\n<p><span class=\"md-line md-end-block\">Congratulations! You&#8217;ve made your first repository. Now let&#8217;s make something. Open up R\u00a0and set the working directory to the folder we just created. You can do this with the GUI or by running <span spellcheck=\"false\"><code>setwd(\"~\/GitHub\/CouncilBudget\")<\/code><\/span> in the console. Check you&#8217;re in the right place with the command <span spellcheck=\"false\"><code>getwd()<\/code><\/span> which prints out your path.<\/span><\/p>\n<h3 class=\"md-end-block md-heading\">Adding files to your repository<\/h3>\n<p><span class=\"md-line md-end-block\">Now create a new file with the following content and save it as <span spellcheck=\"false\"><code>analysis.R<\/code><\/span> in your repository.<\/span><\/p>\n<pre class=\"md-fences md-end-block\" lang=\"r\" contenteditable=\"false\"><span class=\"cm-comment\"># Analysis for 2016 City Councel Budget Data<\/span>\r\n<span class=\"cm-variable\">library<\/span>(<span class=\"cm-variable\">dplyr<\/span>)\r\n<span class=\"cm-variable\">library<\/span>(<span class=\"cm-variable\">ggplot2<\/span>)<\/pre>\n<p><span class=\"md-line md-end-block\">Go back to GitHub desktop and notice that the selector at the top of screen that says <span class=\"\"><em>Changes<\/em><\/span> has an indicator next to it. When you switch to that view you&#8217;ll see the file we just created is listed. When the file is selected, it&#8217;s contents are shown on the right. Additions (lines that don&#8217;t exist in the repository) are highlighted in green and deletions\u00a0(lines to be removed form the repository) are listed in red. <\/span><\/p>\n<p><span class=\"md-line md-end-block\">Select the file and write a commit message that&#8217;s descriptive of the changes or additions you&#8217;re making. You can write a detailed description in the box below but only the commit message is required. Click <span class=\"\"><em>commit to master<\/em><\/span> to add the file and its contents to version control. Git will now know to keep an eye out for any changes you make later. Go back to the <span class=\"\"><em>History<\/em><\/span> panel and you&#8217;ll see that there is another commit message with the details about the file you just added. <\/span><\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/first_commit.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1608\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/first_commit.png\" alt=\"\" width=\"1021\" height=\"598\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/first_commit.png 1021w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/first_commit-300x176.png 300w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/first_commit-768x450.png 768w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/first_commit-624x365.png 624w\" sizes=\"auto, (max-width: 1021px) 100vw, 1021px\" \/><\/a><\/p>\n<h3 class=\"md-end-block md-heading\">Using gitignore to exclude files and directories<\/h3>\n<p><span class=\"md-line md-end-block\">Now let&#8217;s do something more interesting. Using the City of Vancouver&#8217;s <span class=\"\"><a spellcheck=\"false\" href=\"http:\/\/data.vancouver.ca\/datacatalogue\/index.htm\">Open Data Calogue<\/a><\/span> download the <span class=\"\"><a spellcheck=\"false\" href=\"http:\/\/data.vancouver.ca\/datacatalogue\/mayorAndCouncil.htm\">city council annual budget and expenses<\/a><\/span> data for 2016 as a CSV (or use <span class=\"\"><a spellcheck=\"false\" href=\"ftp:\/\/webftp.vancouver.ca\/opendata\/csv\/2016CityCouncilBudgetandExpensesDetailedandSummary_csv.zip\">this link<\/a><\/span> to get the zip file directly). Create a new folder in your repository called <span spellcheck=\"false\"><code>data<\/code><\/span> and move the <span spellcheck=\"false\"><code>2016CityCouncilBudgetandExpensesDetailedandSummary_Councillors.csv<\/code><\/span> from the zip archive into it. This file contains our source data but we don&#8217;t want to put source data on GitHub. It&#8217;s COE policy and it can be difficult to manage large files through GitHub s we&#8217;re going to use <span spellcheck=\"false\"><code>.gitignore<\/code><\/span> to help us. <\/span><\/p>\n<p><span class=\"md-line md-end-block\">Open <span spellcheck=\"false\"><code>.gitignore<\/code><\/span> in a text editor such as Notepad++ or Visual Studio Code. Add the following comment and rule near the top of the file. <\/span><\/p>\n<pre class=\"md-fences md-end-block\" lang=\"\" contenteditable=\"false\"># Excluded Directories\r\ndata\/<\/pre>\n<p><span class=\"md-line md-end-block\">Now save the file and go back to GitHub Desktop. You&#8217;ll notice that the data file is no longer staged and now there is a change to <span spellcheck=\"false\"><code>.gitignore<\/code><\/span>. Notice that only some of the lines in the file have been changed and these have been highlighted in green. Commit this change with an appropriate commit message. From now on, everything added to the <span spellcheck=\"false\"><code>data<\/code><\/span><span class=\"\"> directory will be excluded from Git by default.<\/span><\/span><\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/gitignore_changes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1613\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/gitignore_changes.png\" alt=\"\" width=\"488\" height=\"132\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/gitignore_changes.png 815w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/gitignore_changes-300x81.png 300w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/gitignore_changes-768x207.png 768w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/gitignore_changes-624x168.png 624w\" sizes=\"auto, (max-width: 488px) 100vw, 488px\" \/><\/a><\/p>\n<h3 class=\"md-end-block md-heading\">Commiting changes to a file<\/h3>\n<p class=\"md-end-block md-heading\"><span class=\"md-line md-end-block\">Let&#8217;s make a simple plot of the councillor data using a little dplyr and ggplot wizardry. Add the following code to <span spellcheck=\"false\"><code>analysis.R<\/code><\/span> to create a simple bar plot of the total expenses for each councillor. <\/span><\/p>\n<pre class=\"md-fences md-end-block\" lang=\"r\" contenteditable=\"false\"><span class=\"cm-variable\">raw.df<\/span> <span class=\"cm-arrow\">&lt;-<\/span> <span class=\"cm-variable\">read.csv<\/span>(<span class=\"cm-string\">'data\/2016CityCouncilBudgetandExpensesDetailedandSummary_Councillors.csv'<\/span>, <span class=\"cm-variable\">skip<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-number\">3<\/span>, <span class=\"cm-variable\">nrows<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-number\">10<\/span>)\r\n<span class=\"cm-variable\">str<\/span>(<span class=\"cm-variable\">df<\/span>)\r\n\u200b\r\n<span class=\"cm-variable\">df<\/span> <span class=\"cm-arrow\">&lt;-<\/span> <span class=\"cm-variable\">raw.df<\/span> <span class=\"cm-variable-2\">%&gt;%<\/span>\r\n  <span class=\"cm-variable\">transmute<\/span>(<span class=\"cm-variable\">Councillor<\/span> <span class=\"cm-arg-is\">=<\/span> <span class=\"cm-variable\">Councillor<\/span>, <span class=\"cm-variable\">Expenses<\/span> <span class=\"cm-arg-is\">=<\/span> <span class=\"cm-variable\">Total.Councillors.Expenses<\/span>) <span class=\"cm-variable-2\">%&gt;%<\/span> \r\n  <span class=\"cm-variable\">mutate<\/span>(<span class=\"cm-variable\">Expenses<\/span> <span class=\"cm-arg-is\">=<\/span> <span class=\"cm-variable\">as.numeric<\/span>(<span class=\"cm-variable\">gsub<\/span>(<span class=\"cm-string\">\"[$|,]\"<\/span>, <span class=\"cm-string\">\"\"<\/span>, <span class=\"cm-variable\">Expenses<\/span>))) <span class=\"cm-variable-2\">%&gt;%<\/span> \r\n  <span class=\"cm-variable\">transform<\/span>(<span class=\"cm-variable\">Councillor<\/span> <span class=\"cm-arg-is\">=<\/span> <span class=\"cm-variable\">reorder<\/span>(<span class=\"cm-variable\">Councillor<\/span>, <span class=\"cm-variable\">Expenses<\/span>))\r\n\u200b\r\n<span class=\"cm-variable\">plot<\/span> <span class=\"cm-arrow\">&lt;-<\/span> <span class=\"cm-variable\">ggplot<\/span>(<span class=\"cm-variable\">df<\/span>) <span class=\"cm-operator\">+<\/span> \r\n  <span class=\"cm-variable\">geom_bar<\/span>(<span class=\"cm-variable\">aes<\/span>(<span class=\"cm-variable\">x<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-variable\">Councillor<\/span>, <span class=\"cm-variable\">y<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-variable\">Expenses<\/span>), <span class=\"cm-variable\">stat<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-string\">\"identity\"<\/span>) <span class=\"cm-operator\">+<\/span>\r\n  <span class=\"cm-variable\">xlab<\/span>(<span class=\"cm-string\">\"\"<\/span>) <span class=\"cm-operator\">+<\/span> <span class=\"cm-variable\">ylab<\/span>(<span class=\"cm-string\">\"Total Expenses (Dollars)\"<\/span>) <span class=\"cm-operator\">+<\/span> <span class=\"cm-variable\">coord_flip<\/span>()\r\n<span class=\"cm-variable\">print<\/span>(<span class=\"cm-variable\">plot<\/span>)<\/pre>\n<p><span class=\"md-line md-end-block\">I noticed I made a typo in the comment in the first line so I can also correct <span class=\"\"><em>councel<\/em><\/span> to <span class=\"\"><em>council<\/em><\/span>. We can use the same procedure as before to add this code to our repository. Return to the <span class=\"\"><em>Changes<\/em><\/span><span class=\"\"> pane in GitHub Desktop and click on <\/span><span spellcheck=\"false\"><code>analysis.R<\/code><\/span> (if you don&#8217;t see the file then you may not have saved your changes). Notice how the additions are highlighted in green and the line we corrected is highlighted in red. Git works line by line so changes to existing lines of code are stored one deleted line and one added line. Commit the changes with an appropriate commit message.<\/span><\/p>\n<p><span class=\"md-line md-end-block\"><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/commit_add_delete.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1605\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/commit_add_delete.png\" alt=\"\" width=\"405\" height=\"285\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/commit_add_delete.png 512w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/commit_add_delete-300x211.png 300w\" sizes=\"auto, (max-width: 405px) 100vw, 405px\" \/><\/a><\/span><\/p>\n<h3>Reverting a change<\/h3>\n<p><span class=\"md-line md-end-block\">One reason you might want to use version control is so you can revert mistakes you&#8217;ve introduced in your code. There are ways to force Git to abandon commits entirely but in most cases the preferred method is to revert a commit by making the opposite changes to your code. Let&#8217;s see this in action by changing line 15 of <span spellcheck=\"false\"><code>analysis.R<\/code><\/span> so that the bars are filled with a color. This can be done by adding <span spellcheck=\"false\"><code>fill=\"pink\"<\/code><\/span> to the <span spellcheck=\"false\"><code>aes<\/code><\/span> in <span spellcheck=\"false\"><code>geom_bar<\/code><\/span>. Save the file and commit the changes. Your history should now look something like this:<\/span><\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/revert-pre.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1618\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/revert-pre.png\" alt=\"\" width=\"550\" height=\"279\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/revert-pre.png 809w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/revert-pre-300x152.png 300w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/revert-pre-768x389.png 768w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/revert-pre-624x316.png 624w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p>If your project advisor comes by and gives you a hard time for making the plot less &#8220;professional&#8221; then you have a couple options. You can edit the file, remove the part that sets the color, save and commit the file, or you can simply revert the commit by clicking\u2026 <span class=\"\"><em>Revert<\/em><\/span>. A new commit is made with the opposite changes as your previous commit. This keeps your history intact and makes it easy for Git and the GitHub server to track the changes. Here&#8217;s what your commit history should look like now.<\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/revert-post.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1617\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/revert-post.png\" alt=\"\" width=\"550\" height=\"299\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/revert-post.png 808w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/revert-post-300x163.png 300w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/revert-post-768x417.png 768w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/revert-post-624x339.png 624w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<h3 class=\"md-end-block md-heading\">Pushing Changes to GitHub<\/h3>\n<p><span class=\"md-line md-end-block\">Version control can be useful for one-person projects on your own local machine but it really shines when you start backing up the project on a <span class=\"\"><em>remote repository<\/em><\/span><span class=\"\"> and sharing it with others. GitHub offers this service and is widely used by individuals, companies and the open source community.<\/span><\/span><\/p>\n<p><span class=\"md-line md-end-block\"><span class=\"\">Publishing to a new repository is easy; just click <\/span><span class=\"\"><em>Publish<\/em><\/span> in the top right hand corner. The name of the remote repository is populated with the name of your local repository and you can add an optional description. By default, everything you share to GitHub is public but you can purchase private repositories (or <span class=\"\"><a spellcheck=\"false\" href=\"https:\/\/education.github.com\/pack\">get them for free<\/a><\/span> if you&#8217;re a student). Give your project a description and create the repository.<\/span><\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/publish.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1615\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/publish.png\" alt=\"\" width=\"391\" height=\"265\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/publish.png 515w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/publish-300x203.png 300w\" sizes=\"auto, (max-width: 391px) 100vw, 391px\" \/><\/a><\/p>\n<p><span class=\"md-line md-end-block\">Once you&#8217;ve published your repository the <span class=\"\"><em>Publish<\/em><\/span> icon should change to <span class=\"\"><em>Sync<\/em><\/span>. Syncing changes from your machine to the GitHub server is referred to as a <span class=\"\"><strong>push<\/strong><\/span> and retrieving changes that exist on the server already is called a <span class=\"\"><strong>pull<\/strong><\/span>. Once you introduce a remote repository on GitHub, the canonical version of your project lives on the GitHub server. Other people can become contributors to your project or they can <span class=\"\"><em>fork<\/em><\/span><span class=\"\"> your repository and start working on their own copy. If you view\u00a0the repository on GitHub you will be able to see what files are tracked by version control and were pushed to GitHub. Note that the <\/span><span spellcheck=\"false\"><code>data<\/code><\/span> folder was not synced because it is listed in <span spellcheck=\"false\"><code>.gitignore<\/code><\/span>.<\/span><\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/github-screenshot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1612\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/github-screenshot.png\" alt=\"\" width=\"1003\" height=\"507\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/github-screenshot.png 1003w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/github-screenshot-300x152.png 300w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/github-screenshot-768x388.png 768w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/github-screenshot-624x315.png 624w\" sizes=\"auto, (max-width: 1003px) 100vw, 1003px\" \/><\/a><\/p>\n<h3 class=\"md-end-block md-heading\">Cloning an existing repository<\/h3>\n<p><span class=\"md-line md-end-block\"><span class=\"\">You can clone an existing repository that you own by clicking the plus at the top left, selecting clone and then selecting the repository. Follow the prompt to select an appropriate place on your computer and all the files will be pulled from GitHub to your computer. This could be useful if you want to get a copy on another computer or server or restore a copy because you lost or corrupted your local copy. <\/span><\/span><\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/clone_existing.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1601\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/clone_existing.png\" alt=\"\" width=\"448\" height=\"231\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/clone_existing.png 562w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/clone_existing-300x155.png 300w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><\/a><\/p>\n<p><span class=\"md-line md-end-block\">Sharing code with others is one of the main reasons to use GitHub and in order to share you need to clone someone else&#8217;s repository. This is possible even if the repository is private as long as you&#8217;ve been added as a collaborator. In this case, the repository will be named something like <span spellcheck=\"false\"><code>owner\/repository-name<\/code><\/span> but the process is the same. The image above is what it would look like from\u00a0<span class=\"\"><em>your-partner<\/em><\/span><span class=\"\">&#8216;s point of view. When you clone the repository, everything on GitHub will be synced to your local machine.<\/span><\/span><\/p>\n<h3 class=\"md-end-block md-heading\"><span class=\"\">Pulling changes from GitHub<\/span><\/h3>\n<p><span class=\"md-line md-end-block\">When multiple people contribute to the same project there will be changes in the GitHub repository which you do not have locally. In order to retrieve those changes you will need to perform a <span class=\"\"><strong>pull<\/strong><\/span>. In GitHub desktop this is done by clicking <span class=\"\"><em>Sync<\/em><\/span>. This will first perform a <span class=\"\"><em>pull<\/em><\/span><span class=\"\"> and then perform a <\/span><span class=\"\"><em>push.<\/em><\/span>\u00a0One of the reasons that remote repositories work is that you must pull what is on the repository and resolve any conflicts locally before you can push your changes to the server. This makes sure that Git won&#8217;t break anything underneath your feet: as long as you always push working code, the code on the remote repository will always work. <\/span><\/p>\n<p><span class=\"md-line md-end-block\" contenteditable=\"\">To demonstrate, I&#8217;ve made two additions from the copy on <span class=\"\"><em>your-partner<\/em><\/span>&#8216;s working copy, commited and pushed the changes to the remote repository on GitHub. The commit history for <span class=\"\"><em>your-partner<\/em><\/span> will have the new commits and the server will have the new commits but your repository will remain the same until you <span class=\"\"><em>choose<\/em><\/span><span class=\"\"> to pull the commits from ther server. <\/span><\/span><\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/pull-changes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1616\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/pull-changes.png\" alt=\"\" width=\"539\" height=\"220\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/pull-changes.png 894w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/pull-changes-300x122.png 300w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/pull-changes-768x314.png 768w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/pull-changes-624x255.png 624w\" sizes=\"auto, (max-width: 539px) 100vw, 539px\" \/><\/a><\/p>\n<p>When you pull, the changes from the server will be merged with your code. If you&#8217;ve both made changes to the same file the changes will usually merge gracefully but if you&#8217;ve both made changes to the same line, then you&#8217;ll have to handle a merge conflict.<\/p>\n<p>A\u00a0word of caution: Git won&#8217;t make changes until you tell it to. This means that it won&#8217;t incorporate changes from a collaborator while you&#8217;re in the middle of working but it also means that if you neglect to keep your code up-to-date with the server, you could have to manage a lot of merge conflicts when you sync next.<\/p>\n<h3 class=\"md-end-block md-heading\"><span class=\"\">Resolving merge conflicts<\/span><\/h3>\n<p><span class=\"md-line md-end-block\">Regardless of how dilligent you are about syncing regularly, merge conflicts will occur and, while they can be intimidating, they aren&#8217;t not as bad as they seem. Let&#8217;s look at a simple case where two people make changes to the same line of the same file. <\/span><\/p>\n<p><span class=\"md-line md-end-block\">On their machine, <span class=\"\"><em>your-partner<\/em><\/span><span class=\"\"> sets the dpi of the saved image and, on your machine, you set the dimensions of the saved image. Both of you commit the changes but have not yet pushed them to GitHub. You would each see commits in GitHub desktop that look like this:<\/span><\/span><\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/conflicting_commits.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1606\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/conflicting_commits.png\" alt=\"\" width=\"607\" height=\"164\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/conflicting_commits.png 1022w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/conflicting_commits-300x81.png 300w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/conflicting_commits-768x207.png 768w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/conflicting_commits-624x169.png 624w\" sizes=\"auto, (max-width: 607px) 100vw, 607px\" \/><\/a><\/p>\n<p><span class=\"\">These changes affect the same line of code so there will be a conflict but only one of you will need to resolve the issue. The first person to push their changes doesn&#8217;t see anything unusual but the second person will get a sync conflict error that requires them to pull the change from the server, resolve the conflict and push the resolved code to the server. For example, if <\/span><span class=\"\"><em>your-partner<\/em><\/span><span class=\"\"> pushes their commit to the server first, then when you click <\/span><span class=\"\"><em>Sync<\/em><\/span><span class=\"\"> in GitHub desktop, you&#8217;ll get\u00a0a message about the conflicts.<\/span><\/p>\n<p><span class=\"md-line md-end-block\"><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/sync_conflict.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1620\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/sync_conflict.png\" alt=\"\" width=\"322\" height=\"115\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/sync_conflict.png 454w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/sync_conflict-300x107.png 300w\" sizes=\"auto, (max-width: 322px) 100vw, 322px\" \/><\/a><\/span><\/p>\n<p><span class=\"md-line md-end-block\">Git will add the changes from your local machine (referred to as <em>HEAD<\/em>) and the remote repository (referred to as <em>origin\/master<\/em>) to the file with the conflict which in this case is\u00a0<span spellcheck=\"false\"><code>analysis.R<\/code><\/span><span class=\"\">. You&#8217;ll need to resolve the conflict manually and then commit the changes before pushing the results back to GitHub.<\/span><\/span><\/p>\n<pre class=\"md-fences md-end-block\" lang=\"r\" contenteditable=\"false\"><span class=\"cm-variable\">p<\/span> <span class=\"cm-arrow\">&lt;-<\/span> <span class=\"cm-variable\">ggplot<\/span>(<span class=\"cm-variable\">df<\/span>) <span class=\"cm-operator\">+<\/span>\r\n  <span class=\"cm-variable\">geom_bar<\/span>(<span class=\"cm-variable\">aes<\/span>(<span class=\"cm-variable\">x<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-variable\">Councillor<\/span>, <span class=\"cm-variable\">y<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-variable\">Expenses<\/span>), <span class=\"cm-variable\">stat<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-string\">\"identity\"<\/span>) <span class=\"cm-operator\">+<\/span>\r\n  <span class=\"cm-variable\">xlab<\/span>(<span class=\"cm-string\">\"\"<\/span>) <span class=\"cm-operator\">+<\/span> <span class=\"cm-variable\">ylab<\/span>(<span class=\"cm-string\">\"Total Expenses (Dollars)\"<\/span>) <span class=\"cm-operator\">+<\/span> <span class=\"cm-variable\">coord_flip<\/span>() <span class=\"cm-operator\">+<\/span>\r\n  <span class=\"cm-variable\">ggtitle<\/span>(<span class=\"cm-string\">\"2016 Councillor Expenses\"<\/span>)\r\n<span class=\"cm-operator\">&lt;&lt;&lt;&lt;&lt;&lt;&lt;<\/span> <span class=\"cm-variable\">HEAD<\/span>\r\n<span class=\"cm-variable\">ggsave<\/span>(<span class=\"cm-string\">'figures\/councillor_expenses.pdf'<\/span>, <span class=\"cm-variable\">width<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-number\">8<\/span>, <span class=\"cm-variable\">height<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-number\">6<\/span>)\r\n<span class=\"cm-operator\">=======<\/span>\r\n<span class=\"cm-variable\">ggsave<\/span>(<span class=\"cm-string\">'figures\/councillor_expenses.pdf'<\/span>, <span class=\"cm-variable\">dpi<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-number\">300<\/span>)\r\n<span class=\"cm-operator\">&gt;&gt;&gt;&gt;&gt;&gt;&gt;<\/span> <span class=\"cm-variable\">origin<\/span><span class=\"cm-operator\">\/<\/span><span class=\"cm-variable\">master<\/span>\r\n\u200b<\/pre>\n<p><span class=\"md-line md-end-block\"><span class=\"\">Open the file in a text editor, choose what code to keep and remove the markup left by Git to identify the changes. You can combine the changes, keep your local changes only, keep the changes from the server only or write some new code entirely. In this case, it makes sense to keep both changes so we can edit the file and save the results.<\/span><\/span><\/p>\n<pre class=\"md-fences md-end-block\" lang=\"r\" contenteditable=\"false\"><span class=\"cm-variable\">p<\/span> <span class=\"cm-arrow\">&lt;-<\/span> <span class=\"cm-variable\">ggplot<\/span>(<span class=\"cm-variable\">df<\/span>) <span class=\"cm-operator\">+<\/span>\r\n  <span class=\"cm-variable\">geom_bar<\/span>(<span class=\"cm-variable\">aes<\/span>(<span class=\"cm-variable\">x<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-variable\">Councillor<\/span>, <span class=\"cm-variable\">y<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-variable\">Expenses<\/span>), <span class=\"cm-variable\">stat<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-string\">\"identity\"<\/span>) <span class=\"cm-operator\">+<\/span>\r\n  <span class=\"cm-variable\">xlab<\/span>(<span class=\"cm-string\">\"\"<\/span>) <span class=\"cm-operator\">+<\/span> <span class=\"cm-variable\">ylab<\/span>(<span class=\"cm-string\">\"Total Expenses (Dollars)\"<\/span>) <span class=\"cm-operator\">+<\/span> <span class=\"cm-variable\">coord_flip<\/span>() <span class=\"cm-operator\">+<\/span>\r\n  <span class=\"cm-variable\">ggtitle<\/span>(<span class=\"cm-string\">\"2016 Councillor Expenses\"<\/span>)\r\n<span class=\"cm-variable\">ggsave<\/span>(<span class=\"cm-string\">'figures\/councillor_expenses.pdf'<\/span>, <span class=\"cm-variable\">width<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-number\">8<\/span>, <span class=\"cm-variable\">height<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-number\">6<\/span>, <span class=\"cm-variable\">dpi<\/span><span class=\"cm-arg-is\">=<\/span><span class=\"cm-number\">300<\/span>)<\/pre>\n<p><span class=\"md-line md-end-block\"><span class=\"\">Return to GitHub desktop, commit the changes with the pre-populated commit message and sync the changes to the server as usual. Your collaborators will receive the merged changes next time they pull and you can get back to work.<\/span><\/span><\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/merge-commit.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1614\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/merge-commit.png\" alt=\"\" width=\"546\" height=\"292\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/merge-commit.png 813w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/merge-commit-300x161.png 300w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/merge-commit-768x411.png 768w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2017\/04\/merge-commit-624x334.png 624w\" sizes=\"auto, (max-width: 546px) 100vw, 546px\" \/><\/a><\/p>\n<p><span class=\"md-line md-end-block\"><span class=\"\">The best way to avoid merge conflicts is to sync changes with GitHub regularly. The more often you sync, the less likely you and your collaborators will change the same line at the same time. If you run into a conflict, stay calm, resolve the issue by editing the code in a text editor and push the changes back to the server. <\/span><\/span><\/p>\n<h2 class=\"md-end-block md-heading\">Other Resources<\/h2>\n<p><span class=\"md-line md-end-block\"><span class=\"\">Git and GitHub are powerful and complex tools and this is only one blog post. So far we&#8217;ve only talked about using about a simple use case but you can explore branches, tags, forking, pull-requests and other concepts. There are the guides from <\/span><span class=\"\"><a spellcheck=\"false\" href=\"https:\/\/help.github.com\/articles\/git-and-github-learning-resources\/\">GitHub<\/a><\/span> and <span class=\"\"><a spellcheck=\"false\" href=\"https:\/\/git-scm.com\/book\/en\/v2\/Getting-Started-Git-Basics\">Git<\/a>\u00a0and many others,<\/span>\u00a0interactive courses from <span class=\"\"><a spellcheck=\"false\" href=\"https:\/\/www.codecademy.com\/learn\/learn-git\">Codecademy<\/a><\/span> and <span class=\"\"><a spellcheck=\"false\" href=\"https:\/\/try.github.io\/levels\/1\/challenges\/1\">GitHub<\/a><\/span>, and plenty of blog posts and video series from <span class=\"\"><a spellcheck=\"false\" href=\"http:\/\/bfy.tw\/1e6w\">other sources<\/a><\/span>. One of my favorites is a <span class=\"\"><a spellcheck=\"false\" href=\"https:\/\/www.git-tower.com\/learn\/\">video series<\/a><\/span><span class=\"\"> from the makers of Git Tower which is polished GUI that I like to use. If you want to understand git commands and the graph structure better, check out <\/span><span class=\"\"><a spellcheck=\"false\" href=\"https:\/\/www.youtube.com\/watch?v=cd-g06nA3ns\">this talk<\/a><\/span> and this <span class=\"\"><a spellcheck=\"false\" href=\"http:\/\/git-school.github.io\/visualizing-git\/\">visualization tool<\/a><\/span>. And, if you&#8217;re stuck, <span class=\"\"><a spellcheck=\"false\" href=\"http:\/\/bfy.tw\/BMbh\">Google<\/a><\/span> and <span class=\"\"><a spellcheck=\"false\" href=\"http:\/\/stackoverflow.com\/documentation\/git\/topics\">StackExchange<\/a><\/span><span class=\"\"> are your best friends. <\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you write code, you should be using a version control system. At it&#8217;s most basic, version control tracks changes in files over time and allows you to easily view differences and restore the last working version if you break something (which you will). Most version control systems work at the file level so they [&hellip;]<\/p>\n","protected":false},"author":41749,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[72479,888780,1054146],"class_list":["post-1623","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-git","tag-github","tag-industry-projects"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/posts\/1623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/users\/41749"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/comments?post=1623"}],"version-history":[{"count":5,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/posts\/1623\/revisions"}],"predecessor-version":[{"id":1628,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/posts\/1623\/revisions\/1628"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/media?parent=1623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/categories?post=1623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/tags?post=1623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}