Version Control with Git
Automated Version Control
- Version control is like an unlimited ‘undo’.
- Version control also allows many people to work in parallel.
Setting Up Git
- Use
git config
with the--global
option to configure a user name, email address, editor, and other preferences once per machine.
Creating a Repository
git init
initializes a repository.- Git stores all of its repository data in the
.git
directory.
Branches
git status
shows you the branch you’re currently on.git switch -c <branch-name>
creates a new branch and switches you to it. Make sure you know what branch you are branching from before usinggit switch
without a start-point!git switch -c <branch-name> <start-point>
lets you define the start-point to branch off, via another branch name, a commit ID, or a tag.git switch <branch-name>
switches you to another branch that already exists.git branch -vv
shows you all the branches in the repository.git branch -m <old-branch-name> <new-branch-name>
renames branches.git branch -d <branch-name>
deletes a branch. Use the-D
flag instead of-d
to force delete the branch.
Tracking Changes
git status
shows the status of a repository.- Files can be stored in a project’s working directory (which users see), the staging area (where the next commit is being built up) and the local repository (where commits are permanently recorded).
git add
puts files in the staging area.git commit
saves the staged content as a new commit in the local repository.- Write a commit message that accurately describes your changes.
Exploring History
git log
displays the repositories history.git diff
displays differences between commits.HEAD
references the last commit.HEAD~1
references the commit before last.
Reverting Changes
git restore
recovers old versions of files.git reset
undoes staged changes.git revert
reverses a commit.
Ignoring Things
- The
.gitignore
file tells Git what files to ignore.
Break
Remotes in GitHub
- A local Git repository can be connected to one or more remote repositories.
- Use the SSH protocol to connect to remote repositories.
git push
copies changes from a local repository to a remote repository.git pull
copies changes from a remote repository to a local repository.
Exploring GitHub
- Quickly navigate to a repository in your browser using the url pattern:
https://github.com/<username or organisation>/<repo name>
Exploring History on GitHub
- The
Commits
page displays the history for the specified branch. - Adding
compare
to the end of the repository URL displays differences between commits.
Pull Requests
- A pull request (PR) is where your changes go through code and science review.
- PRs can contain automated checks to help speed up the review process and avoid human error.
- The PR will automatically create an easy to read diff (difference) of the changes for the review (in the Files changed tab).
- Squashing and merging takes all the commits in your PR and ‘squashes’ them into a single new commit on the target branch.
git fetch
fetches changes to the GitHub remote.git pull
pulls and merges changes to the GitHub remote into your local copy.git branch -avv
displays all your local branches including references to any remote branches.git remote prune origin
removes references to remote branches that have been deleted on GitHub.
Configuring GitHub
- You access your GitHub profile settings by navigating to: https://github.com/settings/profile.
- To configure general GitHub notification settings navigate to: https://github.com/settings/notifications.
- Click on the Watch or Unwatch repository dropdown button to change notifications for specific repositories.
End
Open Science
- Open scientific work is more useful and more highly cited than closed.
Licensing
- The
LICENSE
,LICENSE.md
, orLICENSE.txt
file is often used in a repository to indicate how the contents of the repo may be used by others. - People who incorporate General Public License (GPL’d) software into their own software must make the derived software also open under the GPL license if they decide to share it; most other open licenses do not require this.
- The Creative Commons family of licenses allow people to mix and match requirements and restrictions on attribution, creation of derivative works, further sharing, and commercialization.
- People who are not lawyers should not try to write licenses from scratch.
Citation
- Add a CITATION file to a repository to explain how you want your work cited.
Hosting
- Projects can be hosted on university servers, on personal domains, or on a public hosting service.
- Rules regarding intellectual property and storage of sensitive information apply no matter where code and data are hosted.
‘Using Git from RStudio’
- Using RStudio’s Git integration allows you to version control a project over time.