Using Git and GitHub¶
Git is a version control system to collaborate on code and track changes. Git responsitories can be hosted on GitHub.
You can learn about git and github here: https://rogerdudler.github.io/git-guide/
WARNING:¶
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!
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.
Definitions¶
A repository is a place (folder) with all the files and history of a project. It can be called a repo for short.
A remote repo is on a server, usually a site such as GitHub.com, and it can be accessed by multiple users.
A local repo is a copy on your computer or the Hub where you are using this notebook.
A branch is a copy or version of a project that is parallel to the main project.
The HEAD is a reference pointer to the branch you’re currently working on.
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.
A pull request, or PR, is a way to notify other users about changes and collaborate before committing.
To commit means to save your changes.
Configuration¶
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.
!git config --global user.email "me@example.com"
!git config --global user.name "Eric Idle"
Commands¶
Clone¶
to clone a repository (download it from a remote repo)
!git clone https://github.com/callysto/curriculum-notebooks.git
You may then want to copy this notebook into that new folder. Then you can close this notebook and open that new copy.
!cp Github.ipynb curriculum-notebooks
Check what has been happening¶
!git log
Pull changes from remote¶
This downloads any changes that are in the remote repo but not yet present in your local repository.
It will try to merge the changes into your local repo, but you may get a notification about merge conflicts.
!git pull
or
!git pull origin master
Fetch changes from remote¶
if you’d prefer to just download remote changes but not integrate them into your local environment (integrating then requires the merge
command)
!git fetch --all
!git merge
Stash¶
to temporarily store your local changes so you can go back to them later
!git stash
Stash pop¶
apply stashed changes (from the top of the stash list) back on to current branch
!git stash pop
Branches¶
A branch is a copy or version of a project that is parallel to the main project.
List branches¶
!git branch -a
Checkout a branch¶
!git checkout remotes/origin/master
Create a new branch¶
!git checkout origin/master -b NameOfNewBranch
Making Commits¶
commit to local¶
Change the commit message in the quotation marks to reflect what this commit does
!git commit -m "Fix typographical error"
Creating a new repository to save your work on GitHub¶
Run each of these command in a New Terminal in your Callysto Hub.
Change directories until you are in the directory (folder) that you want to save on GitHub.
cd my-interesting-folder
From inside the folder you want to save, set it up as a git repo with these commands:
git init
git add *
git commit -m "First commit, or whatever"
Log in to GitHub and create a new repo. As an example we call it Test1. Do not include any initialization files.
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.)
git remote add origin https://github.com/acct_name/Test1.git
Now push your local files onto the github repo using this:
git push -u origin master
Git will ask you for your GitHub account name and password.
You can now check on GitHub to see that your online repo has been populated with the files you wanted.
From now on, you can pull and push items between your local repo and the GitHub repo as you desire.
Learning more about Git and GitHub¶
For more information about these and other commands, check the git documentation or GitHub help.