Ondřej Mottl
Setkání komunity Data Stewardů
10.09.2024
It is all about keeping track of changes 📓✍️
03:00
Follow instructions in Version Control - git hell (a separate presentation).
Getting all the necessary software installed, configured, and playing nicely together is honestly half the battle … Brace yourself for some pain
Activate git for a repo
Create new project with git tracking
For existing project
usethis::use_git()
Create new project with git tracking
usethis::create_project("<DIRECTORY>")
# switch to the new project
usethis::use_git()
Git integration is automatic in Source control panel
Make a new project with Git tracking
03:00
Copy (download) for remote repo to local machine
Example of online repo: OndrejMottl/VersionControl_DataStewards_Sep2024
usethis::create_from_github(
repo_spec = "https://github.com/<OWNER>/<REPO>.git",
destdir = "<DIRECTORY>",
fork = FALSE
)
Open Command Palette (Ctrl+Shift+p
)
Paste in URL: "https://github.com/<OWNER>/<REPO>.git"
clone a repo (e.g. this one)
05:00
A commit is a record of a change
If you create or edit a file in your repository and save the changes, you need to record your change via a commit
Chess move diary:
Pawn to d4
Edit line 32 of file A
Make a change to a file and save it. Now stage the change:
05:00
Commit (record) staged changes:
SHA - unique identifier
Author - who has done this?
Date - when was this done?
Message - description of what has been done
Stats - what has changed?
05:00
Commits are quick and cheap. Therefore:
Now we need to sync chnages with the remote using PUSH
Add a remote to existing local repo (only once):
Push local to remote (GitHub):
Add a remote to existing local repo (only once):
Push local to remote (GitHub):
Add a remote to existing local repo (only once):
usethis::use_github(protocol = "https")
Push local to remote (GitHub):
Add a remote to existing local repo (only once):
Push local to remote (GitHub):
05:00
README - description of the project
.gitignore - list of files ignored by GitHub (more about it later)
license - tell other what they can do wit your code
{usethis} package provide a lot of usefull helpers
usethis::use_readme_md()
usethis::use_mit_license(name = "Ondřej Mottl")
usethis::use_tidy_contributing()
usethis::use_code_of_conduct()
Create a new repo on GitHub
05:00
.gitignore
fileA guide to the git which files should be ignored for detecting changes
Here is an example of a .gitignore file:
# History files
.Rhistory
.Rapp.history
# Session Data files
.RData
# RStudio files
.Rproj.user/
# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3
.httr-oauth
.Rproj.user
#data (excludes everything in the folder data)
data/*
# you can make exceptions for specific files
!data/dragon_taxonomy.csv
#figures & output (excludes all figure files)
*.png
*.pdf
*.jpeg
Now we need to sync chnages from the remote to local the using PULL
A merge conflict can occur when you are changing the same line in one file differently.
To https://github.com/picardis/myrepo.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/picardis/myrepo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
a good strategy to avoid such conflicts:
Merge conflicts cannot always be avoided (but can be mitigated by branches; later).
In pairs:
03:00
How to undo last commit?
Variant A: I commited but NOT pushed yet.
RStudio has a range of possibilities to work with Git and GitHub as shown in this tutorial. The Terminal (NOT console) has more commands and options and will be handy for trouble shooting.
Open Command Palette (Ctrl+Shift+p
)
Write Git: Undo Last Commit
How to undo last commit?
Variant B: I commited but AND pushed already.
Right-click on the commit you would like to undo to and select Revert a commit.
In the Source control panel -> COMMITS section -> Right-click on the commit you want to revert to -> Select the Reset Current Branch to Previous Commit
Undo/Revert commit
03:00
checkout
)The default branch is called main or master
‼️ Make sure that you have all changes commited before switching ‼️
05:00
Request to merge a branch
After you push new branch, you should have a green button Compare & pull request
Now you can more commits, (add Comment to start discussion), or merge
05:00
You can use Markdown in the description and comments
More details on Github Docs
A tool to review suggested changes
On someone else’s PR, you can comment on individual lines or whole files
05:00
Merge conflict with branches is much more pleasant😎
Edit the file as needed
Commit the changes
Merge commit
Squash & Merge
Rebase & Merge
Merge a branch
03:00
We can delete branch directly on GitHub after merging
We can also delete branch before merging
To delete a local branch
To delete a remote branch
We need the Terminal (NOT console) again.
To delete a local branch
To delete a remote branch
Open Command Palette (Ctrl+Shift+p
)
Select Git: Delete branch …
Delete a branch
03:00
Ondřej Mottl Assistant Professor at Charles University