|
| 1 | +--- |
| 2 | +title: Git and GitHub |
| 3 | +date: 2025-06-14-14:16:03Z |
| 4 | +type: docs |
| 5 | +weight: 500 |
| 6 | +toc: true |
| 7 | +menu: |
| 8 | + hpc-best-practices: |
| 9 | +--- |
| 10 | + |
| 11 | +__What is version control?__ |
| 12 | + |
| 13 | +Version control is the process of recording and managing changes to a file or set of files over time so that you can recall specific versions later, if needed. |
| 14 | + |
| 15 | +__What do we version control?__ |
| 16 | + |
| 17 | +The files we're typically interested in being version controlled are *software source code* files. However, you can version control almost any type of file. |
| 18 | + |
| 19 | +>"In practice, everything that has been created manually should be put in version control, including programs, original field observations, and the source files for papers." |
| 20 | +> |
| 21 | +> Best Practices for Scientific Computing, Wilson et al. 2012 (arXiv:1210.0530) |
| 22 | +
|
| 23 | +## Git and GitHub |
| 24 | + |
| 25 | +__What is Git?__ |
| 26 | + |
| 27 | +Git is a distributed version control system. It was designed to be *simple*, *fast*, and *fully-distributed*, with support for large projects and nonlinear development workflows. Git was originally created in 2005 by Linus Torvalds and other Linux kernel developers when the free use of the proprietary version control system they had been using for kernel development was revoked by its copyright holder. |
| 28 | + |
| 29 | +### Private vs Public Repositories |
| 30 | + |
| 31 | +{{< figure src="/notes/hpc-best-practices/img/privatevspublic.png" width=70% height=70% >}} |
| 32 | + |
| 33 | +When using Git, each contributor who is sharing changes with others on a project has at least two repositories: |
| 34 | +* A __private repository__ is the one that exists on your local computer and is the one you make commits to |
| 35 | +* A __public repository__ is the one that you use to share your changes with collaborators |
| 36 | + |
| 37 | +## The Git Workflow |
| 38 | + |
| 39 | +1. Update your private repository by `fetch`ing all recent changes committed by your team of collaborators from the shared public repository (or their individual public repositories). |
| 40 | + |
| 41 | +2. Make changes and `commit` them to your private repository. |
| 42 | + |
| 43 | +3. Review your committed changes. |
| 44 | + |
| 45 | +4. Share your new changes with the team by pushing your committed changes back to the team's shared public repository (or your individual public repository; then submit a pull request from your public repository to the team's shared public repository). |
| 46 | + |
| 47 | +5. Rinse and repeat. |
| 48 | + |
| 49 | +## Getting Started with GitHub |
| 50 | + |
| 51 | +{{< figure src="/notes/hpc-best-practices/img/git1.png" width=30% height=30% >}} |
| 52 | + |
| 53 | +GitHub website: https://github.com |
| 54 | + |
| 55 | +### Your GitHub Profile |
| 56 | + |
| 57 | +{{< figure src="/notes/hpc-best-practices/img/git2.png" width=70% height=70% >}} |
| 58 | + |
| 59 | +### Creating a New GitHub Repository |
| 60 | + |
| 61 | +{{< figure src="/notes/hpc-best-practices/img/git3.png" width=70% height=70% >}} |
| 62 | + |
| 63 | +### Your GitHub Repository After the First `git push` |
| 64 | + |
| 65 | +{{< figure src="/notes/hpc-best-practices/img/git4.png" width=70% height=70% >}} |
| 66 | + |
| 67 | +## Using GitHub with Rivanna |
| 68 | + |
| 69 | +__I need to push and commit code changes from Rivanna/Afton to my GitHub account. How do I set that up?__ |
| 70 | + |
| 71 | +You must first generate an ssh key and then copy it to your Git repository. Here are the instructions for generating the ssh key and what to do on your Git page: |
| 72 | + |
| 73 | +1. To generate an ssh key, see the following link: [ssh key generation](https://www.rc.virginia.edu/userinfo/howtos/general/sshkeys/) |
| 74 | + |
| 75 | +2. Click on the drop-down menu next to your Git profile picture in the upper right corner; Select Settings; Click on SSH and GPG keys in the left column; Click on the New SSH Key button and follow the directions to upload your ssh key. Make sure that the ssh key is in your authorized_keys file in your .ssh directory on Rivanna/Afton. |
| 76 | + |
| 77 | +3. The next step is to clone the repository using the ssh link. If you have already cloned the repository using the http link and made a number of changes to your files, you won’t want to redo them. Rename the directory that was created when you first cloned the repository. Then, re-clone the repository using the ssh link and copy all of the files you had changed to the new directory. Finally, push those changes back to the repository. |
| 78 | + |
| 79 | +## Benefits of Version Control |
| 80 | + |
| 81 | +* __Archiving:__ You *must* regularly save the changes you make |
| 82 | + |
| 83 | +* __Reproducibility:__ Creating a history of saved changes allows you to revert selected files or your entire project back to any previous state in its recorded history. |
| 84 | + |
| 85 | +* __Collaboration:__ You and a team of contributors can work on a project independently, but share your changes amongst one another as the project takes shape. |
| 86 | + |
| 87 | +* __Accountability:__ Compare changes, see who last modified something, and who introduced a problem and when |
| 88 | + |
| 89 | +* __Recoverability:__ Each contributor has their own local, recent copy of the project and its complete history, making it highly unlikely you'll ever lose a significant portion of the project |
| 90 | + |
| 91 | +## References |
| 92 | + |
| 93 | +* __Version Control with Git__ by D. Huang and I. Gonzalez: https://swcarpentry.github.io/git-novice/ |
| 94 | +* __Pragmatic Version Control Using Git__ by T. Swicegood |
| 95 | +* __Pro Git__ by S. Chacon and B. Straub: https://git-scm.com/book/en/v2 |
| 96 | +* Research Computing Documentation: https://learning.rc.virginia.edu/notes/git-intro/ |
0 commit comments