|
| 1 | +--- |
| 2 | +title: "Collaborating with GitHub" |
| 3 | +teaching: 25 |
| 4 | +exercises: 0 |
| 5 | +questions: |
| 6 | +- "What is a remote repository?" |
| 7 | +- "How can I use GitHub to work from multiple locations?" |
| 8 | +objectives: |
| 9 | +- "Understand how to set up remote repository" |
| 10 | +- "Understand how to push local changes to a remote repository" |
| 11 | +- "Understand how to clone a remote repository" |
| 12 | +keypoints: |
| 13 | +- "Git is the version control system: GitHub is a remote repositories provider." |
| 14 | +- "`git clone` to make a local copy of a remote repository" |
| 15 | +- "`git push` to send local changes to remote repository" |
| 16 | +--- |
| 17 | +To collaborate with your colleagues as well as _your self_, we will need to set up a remote destination to save our work *AND* our history. |
| 18 | + |
| 19 | +### GitHub |
| 20 | + |
| 21 | +[GitHub](http://GitHub.com) is a company which provides remote repositories for |
| 22 | +Git and a range of functionalities supporting their use. GitHub allows users to |
| 23 | +set up their private and public source code Git repositories. It provides |
| 24 | +tools for browsing, collaborating on and documenting code. GitHub, like other |
| 25 | +services such as [GitLab](https://about.gitlab.com/) and |
| 26 | +[Bitbucket](https://bitbucket.org), supports a wealth of resources to support |
| 27 | +projects including: |
| 28 | + |
| 29 | +* Browsing code from within a web browser, with syntax highlighting |
| 30 | +* Software release management |
| 31 | +* Issue and bug tracking |
| 32 | +* Project management tools |
| 33 | + |
| 34 | +### GitHub for research |
| 35 | + |
| 36 | +GitHub **isn't** the only remote repositories provider. |
| 37 | +It is very popular, however, particularly within the open source communities. |
| 38 | + |
| 39 | +Also, GitHub has [functionality which is particularly useful |
| 40 | +for researchers](https://github.com/blog/1840-improving-github-for-sciences) |
| 41 | +such as making code citable! |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +### Get a GitHub account |
| 46 | + |
| 47 | +[Sign up](https://GitHub.com) or [sign in](https://GitHub.com) if you already have an account. |
| 48 | + |
| 49 | +### Create a new repository |
| 50 | + |
| 51 | +Now, we can create a repository on GitHub, |
| 52 | + |
| 53 | +* Log in to [GitHub](https://GitHub.com/) |
| 54 | +* Click on the **Create** icon on the top right |
| 55 | +* Enter Repository name: "git-papers" |
| 56 | +* For the purpose of this exercise we'll create a public repository |
| 57 | +* Since we'll be importing a local repository, make sure that **Initialize this repository with a README** is **unselected** |
| 58 | +* Click **Create Repository** |
| 59 | + |
| 60 | +You'll get a page with new information about your repository. We already have our local repository and we will be *pushing* it to GitHub, so we can do the following: |
| 61 | + |
| 62 | +``` |
| 63 | +$ git remote add origin https://github.com/<USERNAME>/git-papers.git |
| 64 | +``` |
| 65 | +{: .language-bash} |
| 66 | + |
| 67 | +This line sets up an alias `origin`, to correspond to the URL of our new repository on GitHub. |
| 68 | + |
| 69 | +### Push locally tracked files to a remote repository |
| 70 | + |
| 71 | +Now we can execute the following: |
| 72 | + |
| 73 | +``` |
| 74 | +$ git push -u origin master |
| 75 | +``` |
| 76 | +{: .language-bash} |
| 77 | +``` |
| 78 | +Counting objects: 32, done. |
| 79 | +Delta compression using up to 8 threads. |
| 80 | +Compressing objects: 100% (28/28), done. |
| 81 | +Writing objects: 100% (32/32), 3.29 KiB | 0 bytes/s, done. |
| 82 | +Total 32 (delta 7), reused 0 (delta 0) |
| 83 | +To https://github.com/emdupre/git-papers.git |
| 84 | + * [new branch] master -> master |
| 85 | +Branch master set up to track remote branch master from origin. |
| 86 | +``` |
| 87 | +{: .output} |
| 88 | + |
| 89 | +This **pushes** our `master` branch to the remote repository, (named via the alias `origin`) and creates a new `master` branch in the remote repository. |
| 90 | + |
| 91 | +Now, on GitHub, we should see our code, |
| 92 | +and if we click the `Commits` tab we should see our complete history of commits. |
| 93 | + |
| 94 | +Our local repository is now available on GitHub. This means that anywhere we can access GitHub, we can access our repository! |
| 95 | + |
| 96 | +### Push other local branches to a remote repository |
| 97 | + |
| 98 | +Let's push each of our local branches into our remote repository: |
| 99 | + |
| 100 | +``` |
| 101 | +$ git push origin branch_name |
| 102 | +``` |
| 103 | +{: .language-bash} |
| 104 | + |
| 105 | +The branch should now be created in our GitHub repository. |
| 106 | + |
| 107 | +To list all branches (local and remote): |
| 108 | + |
| 109 | +``` |
| 110 | +$ git branch -a |
| 111 | +``` |
| 112 | +{: .language-bash} |
| 113 | + |
| 114 | +> ## Deleting branches (for information only) |
| 115 | +> **Don't do this now.** This is just for information. |
| 116 | +> To delete branches, use the following syntax: |
| 117 | +> |
| 118 | +> ``` |
| 119 | +> $ git branch -d <branch_name> # For local branches |
| 120 | +> $ git push origin --delete <branch_name> # For remote branches |
| 121 | +> ``` |
| 122 | +> {: .language-bash} |
| 123 | +{: .callout} |
| 124 | +
|
| 125 | +### Cloning a remote repository |
| 126 | +
|
| 127 | +Now, let's do something drastic! |
| 128 | +But before that step, |
| 129 | +**make sure that you pushed all your local branches into the remote repository!** |
| 130 | +
|
| 131 | +``` |
| 132 | +$ cd .. |
| 133 | +$ rm -rf git-papers |
| 134 | +``` |
| 135 | +{: .language-bash} |
| 136 | +
|
| 137 | +Gulp! We've just wiped our local repository! |
| 138 | +But, because we've pushed to GitHub, we have still have a copy! |
| 139 | +We can just copy the repository down using `git clone`: |
| 140 | +
|
| 141 | +``` |
| 142 | +$ git clone https://github.com/<USERNAME>/git-papers.git |
| 143 | +``` |
| 144 | +{: .language-bash} |
| 145 | +``` |
| 146 | +Cloning into 'git-papers'... |
| 147 | +remote: Counting objects: 32, done. |
| 148 | +remote: Compressing objects: 100% (21/21), done. |
| 149 | +remote: Total 32 (delta 7), reused 32 (delta 7), pack-reused 0 |
| 150 | +Unpacking objects: 100% (32/32), done. |
| 151 | +Checking connectivity... done. |
| 152 | +``` |
| 153 | +{: .output} |
| 154 | +
|
| 155 | +Cloning creates an exact copy of the repository. By default it creates |
| 156 | +a directory with the same name as the name of the repository. |
| 157 | +
|
| 158 | +Now, if we change into *git-papers* we can see that we have our repository, |
| 159 | +
|
| 160 | +``` |
| 161 | +$ cd git-papers |
| 162 | +$ git log |
| 163 | +``` |
| 164 | +{: .language-bash} |
| 165 | +and we can see our Git configuration files too: |
| 166 | +
|
| 167 | +``` |
| 168 | +$ ls -A |
| 169 | +``` |
| 170 | +{: .language-bash} |
| 171 | +
|
| 172 | +In order to see the other branches locally, we can check them out as before: |
| 173 | +
|
| 174 | +``` |
| 175 | +$ git branch -r # Show remote branches |
| 176 | +$ git checkout paperWJohn # Check out the paperWJohn branch |
| 177 | +``` |
| 178 | +{: .language-bash} |
| 179 | +
|
| 180 | +### Push changes to a remote repository |
| 181 | +
|
| 182 | +We can use our cloned repository just as if it were the original, local repository ! |
| 183 | +So, let's make some changes to our files and commit these. |
| 184 | +
|
| 185 | +``` |
| 186 | +$ git checkout master # We'll continue working on the master branch |
| 187 | +$ vim journal.md # Add results section |
| 188 | +$ git add journal.md # Stage changes |
| 189 | +$ git commit |
| 190 | +``` |
| 191 | +{: .language-bash} |
| 192 | +
|
| 193 | +Having done that, how do we send our changes back to the remote repository? |
| 194 | +We can do this by *pushing* our changes: |
| 195 | +
|
| 196 | +``` |
| 197 | +$ git push origin master |
| 198 | +``` |
| 199 | +{: .language-bash} |
| 200 | +
|
| 201 | +If we now check our GitHub page we should be able to see our new changes under |
| 202 | +the *Commit* tab. |
| 203 | +
|
| 204 | +To see all configured remotes for this repository (we can have multiple!), |
| 205 | +type: |
| 206 | +
|
| 207 | +``` |
| 208 | +$ git remote -v |
| 209 | +``` |
| 210 | +{: .language-bash} |
0 commit comments