{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Syncing to a Remote Repository\n",
    "\n",
    "A remote repository is just another copy of the git repository to which yours is linked. It can be on the same comuputer in a different file location, on a different computer, for example a departmental servr, on the laptop of a collaborator who is also working with you or on a git hosting service.\n",
    "\n",
    "## Git Hosting\n",
    "\n",
    "The main sites are\n",
    "\n",
    "- github\n",
    "\n",
    "  - Business model is paid private repo hosting, public for free \n",
    "  - Education allows 5 private repos\n",
    "  \n",
    "- bitbucket\n",
    "- gitlab\n",
    "\n",
    "  - can be on premis\n",
    "  \n",
    "## Create a Repository on GitHub\n",
    "\n",
    "You create a new repository by clicking the green \"New Repository\", give it a name, and optionally a description, licence, gitignore and README.\n",
    "\n",
    "\n",
    "## Authentication\n",
    "\n",
    "When we cloned the repository earlier that was a read only operation at the remote end. Now we are going to write to the remote repository we need to be authorised to do it. To avoid having to repeatedly put in the passowrd we'll ask git to cache the credentials\n",
    "\n",
    "```\n",
    "train31@myVM:~/git-lesson/workshop-test$ git config credential.helper cache\n",
    "```\n",
    "\n",
    "## git remote\n",
    "\n",
    "```\n",
    "train31@myVM:~/git-lesson/workshop-test$ git remote\n",
    "origin\n",
    "train31@myVM:~/git-lesson/workshop-test$ git remote -v\n",
    "origin\thttps://github.com/christopheredsall/workshop-test.git (fetch)\n",
    "origin\thttps://github.com/christopheredsall/workshop-test.git (push)\n",
    "```\n",
    "\n",
    "## git fetch\n",
    "\n",
    "\n",
    "\n",
    "```\n",
    "git fetch\n",
    "remote: Counting objects: 3, done.\n",
    "remote: Compressing objects: 100% (2/2), done.\n",
    "Unpacking objects: 100% (3/3), done.\n",
    "remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0\n",
    "From https://github.com/christopheredsall/workshop-test\n",
    "   582e532..765c3bb  master     -> origin/master\n",
    "```\n",
    "\n",
    "## git pull\n",
    "\n",
    "``git pull`` does a ``git fetch`` followed by a ``git merge``\n",
    "\n",
    "```\n",
    "$ git status\n",
    "On branch master\n",
    "Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.\n",
    "  (use \"git pull\" to update your local branch)\n",
    "nothing to commit, working directory clean\n",
    "```\n",
    "\n",
    "```\n",
    "$ git pull\n",
    "Updating 582e532..765c3bb\n",
    "Fast-forward\n",
    " file2.md | 1 +\n",
    " 1 file changed, 1 insertion(+)\n",
    " create mode 100644 file2.md\n",
    "```\n",
    "\n",
    "## git push\n",
    "\n",
    "If we have made changes to our local repository we want to get them in to the remote. We use the ``git push`` command to do that. The first time trhough it will give you a warning about how the default behavour has changed. You can do what it suggests to get rid of the warning next time\n",
    "\n",
    "```\n",
    "  git config --global push.default simple\n",
    "```\n",
    "\n",
    "```\n",
    "$ git push\n",
    "Username for 'https://github.com': christopheredsall\n",
    "Password for 'https://christopheredsall@github.com': \n",
    "Counting objects: 3, done.\n",
    "Delta compression using up to 16 threads.\n",
    "Compressing objects: 100% (2/2), done.\n",
    "Writing objects: 100% (3/3), 320 bytes | 0 bytes/s, done.\n",
    "Total 3 (delta 0), reused 0 (delta 0)\n",
    "To https://github.com/christopheredsall/workshop-test.git\n",
    "   765c3bb..e031f0f  master -> master\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercises\n",
    "\n",
    "## Exercise 1\n",
    "\n",
    "- create a repository on github\n",
    "- clone the repository\n",
    "\n",
    "```\n",
    "git clone https://github.com/christopheredsall/workshop-test.git\n",
    "```\n",
    "\n",
    "- find the name of the remote repository\n",
    "\n",
    "It is called origin by default\n",
    "\n",
    "```\n",
    "git remote\n",
    "```\n",
    "\n",
    "- find the URL of the remote repository\n",
    "\n",
    "```\n",
    "git remote -v\n",
    "```\n",
    "\n",
    "## Exercise 2\n",
    "\n",
    "- Edit a file in the github interface and commit it\n",
    "- pull the commit to you local repository\n",
    "\n",
    "```\n",
    "git pull\n",
    "```\n",
    "\n",
    "## Exercise\n",
    "\n",
    "- make a local change and commit it\n",
    "\n",
    "```\n",
    "nano new.md\n",
    "git add new.md\n",
    "git commit -m \"some new work from my laptop\"\n",
    "```\n",
    "\n",
    "- push to github\n",
    "\n",
    "```\n",
    "git config --global push.default simple\n",
    "git push \n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
