{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![Callysto.ca Banner](https://github.com/callysto/curriculum-notebooks/blob/master/callysto-notebook-banner-top.jpg?raw=true)\n", "\n", "\"Open" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Using Git and GitHub\n", "\n", "Git is a version control system to collaborate on code and track changes. Git responsitories can be hosted on [GitHub](https://github.com).\n", "\n", "You can learn about git and github here: https://rogerdudler.github.io/git-guide/\n", "\n", "## WARNING:\n", "\n", "The git commands will create and remove files and directories. Normally, this is what you want. However, if you don't know what you are doing, you may accidentally erase some files that you did not want to erase!\n", "\n", "For this reason, you must be very careful before running any of the git commands in this notebook. For this reason, there are no executable cells in this notebook. If you want to run a git command in this notebook, you will need to type it in yourself, or copy and paste into a code cell before running. \n", "\n", "## Definitions\n", "\n", "A **repository** is a place (folder) with all the files and history of a project. It can be called a *repo* for short.\n", "\n", "A **remote** repo is on a server, usually a site such as [GitHub.com](http://github.com), and it can be accessed by multiple users.\n", "\n", "A **local** repo is a copy on your computer or the [Hub](https://hub.callysto.ca) where you are using this notebook.\n", "\n", "A **branch** is a copy or version of a project that is parallel to the main project.\n", "\n", "The **HEAD** is a reference pointer to the branch you're currently working on.\n", "\n", "A **fork** is like a branch, but it's a copy of someone else's repo that you don't have write access to. It can be merged (via [pull request or PR) back to the original repo.\n", "\n", "A **pull request**, or [PR](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests), is a way to notify other users about changes and collaborate before committing.\n", "\n", "To **commit** means to save your changes.\n", "\n", "## Configuration\n", "\n", "The first step is set up your Git environment, if you haven't done so already. Change the values in the following cell and then run it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git config --global user.email \"me@example.com\"\n", "!git config --global user.name \"Eric Idle\"\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Commands\n", "\n", "### Clone\n", "\n", "to clone a repository (download it from a remote repo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git clone https://github.com/callysto/curriculum-notebooks.git\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You may then want to copy this notebook into that new folder. Then you can close this notebook and open that new copy." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!cp Github.ipynb curriculum-notebooks\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Status\n", "\n", "check on files and commits in the current folder." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git status\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Check what has been happening" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git log\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pull changes from remote\n", "\n", "This downloads any changes that are in the remote repo but not yet present in your local repository.\n", "\n", "It will try to **merge** the changes into your local repo, but you may get a notification about [merge conflicts](https://docs.microsoft.com/en-us/azure/devops/repos/git/merging?view=azure-devops&tabs=command-line)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git pull\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git pull origin master\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fetch changes from remote\n", "\n", "if you'd prefer to just download remote changes but not integrate them into your local environment (integrating then requires the `merge` command)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``` \n", "!git fetch --all\n", "!git merge\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stash\n", "\n", "to [temporarily store your local changes](https://www.git-scm.com/docs/git-stash) so you can go back to them later" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git stash\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Stash list\n", "\n", "to check on your stashes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git stash list\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Stash pop\n", "\n", "apply stashed changes (from the top of the stash list) back on to current branch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git stash pop\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Branches\n", "\n", "A branch is a copy or version of a project that is parallel to the main project.\n", "\n", "### List branches" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git branch -a\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Checkout a branch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git checkout remotes/origin/master\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a new branch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git checkout origin/master -b NameOfNewBranch\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Change to a branch\n", "\n", "if you're not already there" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git checkout --track origin/NameOfBranch\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Making Commits\n", "\n", "### stage changes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git add changed_file.ipynb\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git add .\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### commit to local\n", "\n", "Change the commit message in the quotation marks to reflect what this commit does" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git commit -m \"Fix typographical error\"\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### push to remote" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git push\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git push -u origin NameOfBranch\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### permanently discard all local changes to all files\n", "(use with caution)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "!git reset --hard\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a new repository to save your work on GitHub\n", "\n", "Run each of these command in a New Terminal in your Callysto Hub.\n", "\n", "1. \n", "Change directories until you are in the directory (folder) that you want to save on GitHub.\n", "`cd my-interesting-folder`\n", "\n", "2. \n", "From inside the folder you want to save, set it up as a git repo with these commands:\n", "```\n", "git init\n", "git add *\n", "git commit -m \"First commit, or whatever\"\n", "```\n", "\n", "3. \n", "Log in to [GitHub](https://github.com) and create a new repo. As an example we call it Test1. Do not include any initialization files.\n", "\n", "4. \n", "Add your GitHub account repo as the origin for your local repo. (Here, acct_name should be replaced with your github account. Test1.git is the name of the github repo you created.)\n", "```\n", "git remote add origin https://github.com/acct_name/Test1.git\n", "```\n", "\n", "5.\n", "Now push your local files onto the github repo using this:\n", "```\n", "git push -u origin master\n", "```\n", "\n", "6. \n", "Git will ask you for your GitHub account name and password.\n", "\n", "7. \n", "You can now check on GitHub to see that your online repo has been populated with the files you wanted.\n", "\n", "8. \n", "From now on, you can pull and push items between your local repo and the GitHub repo as you desire.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Learning more about Git and GitHub\n", "\n", "For more information about these and other commands, check the [git documentation](https://git-scm.com/docs) or [GitHub help](https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line).\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[![Callysto.ca License](https://github.com/callysto/curriculum-notebooks/blob/master/callysto-notebook-banner-bottom.jpg?raw=true)](https://github.com/callysto/curriculum-notebooks/blob/master/LICENSE.md)" ] } ], "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }