{"id":64,"date":"2020-07-07T19:45:57","date_gmt":"2020-07-07T18:45:57","guid":{"rendered":"https:\/\/www.slowergram.com\/?p=64"},"modified":"2020-08-19T19:47:39","modified_gmt":"2020-08-19T18:47:39","slug":"beginners-guide-to-git","status":"publish","type":"post","link":"https:\/\/www.slowergram.com\/index.php\/2020\/07\/07\/beginners-guide-to-git\/","title":{"rendered":"Beginners Guide to git"},"content":{"rendered":"\n<p>From the&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Git\" target=\"_blank\" rel=\"noreferrer noopener\">wikipedia page<\/a>\u2026&nbsp;<strong>Git<\/strong>&nbsp;(<a href=\"https:\/\/en.wikipedia.org\/wiki\/Help:IPA\/English\" target=\"_blank\" rel=\"noreferrer noopener\">\/\u0261\u026at\/<\/a>)<sup><a href=\"https:\/\/en.wikipedia.org\/wiki\/Git#cite_note-:0-7\" target=\"_blank\" rel=\"noreferrer noopener\">[7]<\/a><\/sup>&nbsp;is a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Distributed_version_control\" target=\"_blank\" rel=\"noreferrer noopener\">distributed version-control<\/a>&nbsp;system for tracking changes in&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Source_code\" target=\"_blank\" rel=\"noreferrer noopener\">source code<\/a>&nbsp;during&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Software_development\" target=\"_blank\" rel=\"noreferrer noopener\">software development<\/a>.<sup><a href=\"https:\/\/en.wikipedia.org\/wiki\/Git#cite_note-effcomp-8\" target=\"_blank\" rel=\"noreferrer noopener\">[8]<\/a><\/sup>&nbsp;It is designed for coordinating work among&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Programmer\" target=\"_blank\" rel=\"noreferrer noopener\">programmers<\/a>, but it can be used to track changes in any set of&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Computer_file\" target=\"_blank\" rel=\"noreferrer noopener\">files<\/a>.<\/p>\n\n\n\n<p>I\u2019m not a software developer by trade but I do dabble with website development, automation scripts and some useful little tools which all require some degree of coding\/scripting. A lot of people write the code locally, then \u201cpush\u201d that code to a code repository, this is achieved using the git tool. When in a code repo, the code can be shared with others, and some repos also allow for issue logs to allow anyone to comment on any bugs they find.<\/p>\n\n\n\n<p>Other people who perhaps want to use your code on their own machines, can easily get a full upto date version, by \u201ccloning\u201d the code, this is also achieved using the same git tool.<\/p>\n\n\n\n<p>For collaboration work, programmers working on the same code can create \u201cpull\u201d requests allowing updates to various aspects of code to be done by many people.<\/p>\n\n\n\n<p>For its basic usage I will set up a new code repository and push my code to it.<\/p>\n\n\n\n<p>After installing git through your usual package manager, change to the working directory of your project and issue the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git init\nInitialized empty Git repository in \/home\/pi\/python\/tagdb\/.git\/<\/code><\/pre>\n\n\n\n<p>This creates a hidden directory with all the configuration and tracker information needed.<br>If you have already written some code in this directory, you can use the following command to see what git knows about the project:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git status\nOn branch master\n\nNo commits yet\n\nUntracked files:\n  (use \"git add &lt;file>...\" to include in what will be committed)\n\n        tag.py\n\nnothing added to commit but untracked files present (use \"git add\" to track)\n<\/code><\/pre>\n\n\n\n<p>Here git is telling us that it knows about the file tag.py, but it is not yet included or tracked for changes, to rectify this we can add the file into git\u2019s tracking system:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git add tag.py\n$ git status\nOn branch master\n\nNo commits yet\n\nChanges to be committed:\n  (use \"git rm --cached &lt;file>...\" to unstage)\n\n        new file:   tag.py\n\n<\/code><\/pre>\n\n\n\n<p>Now you can see that the file tag.py is being tracked for changes. You can now continue coding until you are happy, then it is time to commit those changes, this is done with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git commit -m \"Initial Commit\"<\/code><\/pre>\n\n\n\n<p>The comment can be anything you like, although its probably more beneficial if it reflects the changes actually made. When issuing this for the first time it may error saying it doesn\u2019t know who you are. This can be resolved by issuing some git config \u2013global commands to tell git your name and email address, but the onscreen instructions will tell you what it needs.<\/p>\n\n\n\n<p>You now have your project being tracked for changes, however that project is still local to your machine. If you wanted to share this code with others, or have an offsite copy, just in case the worst happened to your machine this can be achieved by pushing your code to a remote code repository, there are many to choose from, each offering different features. For this guide we will use github.<\/p>\n\n\n\n<p>Go to&nbsp;<a href=\"https:\/\/github.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com<\/a>&nbsp;and if not done so already create yourself an account. From here create a new repository, it will ask some questions about having a licence and a README file, if you want these go ahead and choose the options for them<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.slowergram.com\/wp-content\/uploads\/2020\/07\/image.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Once the repository is created, make a note of the full URL, as we will need to tell our local git master about the remote repository and push our files to it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git remote add origin https:\/\/github.com\/pdunn467\/tag.py.git\n$ git push -u origin master\nUsername for 'https:\/\/github.com': \nPassword for 'https:\/\/pdunn467@github.com':\nTo https:\/\/github.com\/pdunn467\/tag.py.git\n ! &#91;rejected]        master -> master (fetch first)\nerror: failed to push some refs to 'https:\/\/github.com\/pdunn467\/tag.py.git'\nhint: Updates were rejected because the remote contains work that you do\nhint: not have locally. This is usually caused by another repository pushing\nhint: to the same ref. You may want to first integrate the remote changes\nhint: (e.g., 'git pull ...') before pushing again.\nhint: See the 'Note about fast-forwards' in 'git push --help' for details.\n<\/code><\/pre>\n\n\n\n<p>Ooops, that didn\u2019t work, what the error is actually saying is that the remote repo already has some files in it which are different from the local files. Those files are the licence file and README that were automatically generated when we created the remote repo on github. To rectify this we need to pull those files down into our local repo, and then push again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git pull origin master --allow-unrelated-histories\nFrom https:\/\/github.com\/pdunn467\/tag.py\n * branch            master     -> FETCH_HEAD\nMerge made by the 'recursive' strategy.\n LICENSE   | 674 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n README.md |   2 +\n 2 files changed, 676 insertions(+)\n create mode 100644 LICENSE\n create mode 100644 README.md\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git push -u origin master\nUsername for 'https:\/\/github.com':\nPassword for 'https:\/\/pdunn467@github.com':\nEnumerating objects: 6, done.\nCounting objects: 100% (6\/6), done.\nDelta compression using up to 4 threads\nCompressing objects: 100% (4\/4), done.\nWriting objects: 100% (5\/5), 1.39 KiB | 284.00 KiB\/s, done.\nTotal 5 (delta 0), reused 0 (delta 0)\nTo https:\/\/github.com\/pdunn467\/tag.py.git\n   d83d6c9..62813d5  master -> master\nBranch 'master' set up to track remote branch 'master' from 'origin'.\n<\/code><\/pre>\n\n\n\n<p>If we then browse the project repo in Github, we can see the files have been pushed, and changes tracked.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.slowergram.com\/wp-content\/uploads\/2020\/07\/image-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Everything is now in sync, and you can continue to code locally, not forgetting to add any new files to git, commiting changes and pushing them to the remote master when finished.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>From the&nbsp;wikipedia page\u2026&nbsp;Git&nbsp;(\/\u0261\u026at\/)[7]&nbsp;is a&nbsp;distributed version-control&nbsp;system for tracking changes in&nbsp;source code&nbsp;during&nbsp;software development.[8]&nbsp;It is designed for coordinating work among&nbsp;programmers, but it can be used to track changes in any set of&nbsp;files. I\u2019m not a software developer by trade but I do dabble with website development, automation scripts and some useful little tools <a href=\"https:\/\/www.slowergram.com\/index.php\/2020\/07\/07\/beginners-guide-to-git\/\" class=\"btn btn-link continue-link\">Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[12,10],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-uncategorised","tag-git","tag-homelab"],"_links":{"self":[{"href":"https:\/\/www.slowergram.com\/index.php\/wp-json\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.slowergram.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.slowergram.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.slowergram.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.slowergram.com\/index.php\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":0,"href":"https:\/\/www.slowergram.com\/index.php\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.slowergram.com\/index.php\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.slowergram.com\/index.php\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.slowergram.com\/index.php\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}