06 December, 2022
Git - is a distributed version control system
Git - stores snapshots of the filesystem
Files can be in one of four states:
Files can be in one of four states:
Files can be in one of four states:
Files can be in one of four states:
This is a once off action (per machine)
define who you are
git config --global user.name "Your name" git config --global user.email "your_email@whatever.com"
Specify that the initial branch should be called main rather than master
git config --global init.defaultBranch main
Check these settings
git config --global --list
## user.name=Murray Logan ## user.email=i.obesulus@gmail.com ## credential.helper=!pass Web/Git/github/tokens/general/pcinereus ## push.default=simple ## pull.rebase=false ## init.defaultbranch=main
Or within R (via the usethis package)
usethis::use_git_config(user.name='Your name', user.email='your_email@whatever.com', scope='user') usethis::git_sitrep()
OR via the gert package
gert::git_config_global()
mkdir ~/tmp/Test_repo cd ~/tmp/Test_repo
git init
library(usethis) create_project(path='~/path/project_name', rstudio=TRUE) use_git()
Create a file (text, code etc)
x=seq(1, 10, len=1) y=40*2 + rnorm(10,0,5) plot(x,y)
Otherwise, create any kind of file (in the folder we just created)
Stage the changes (add)
git add <file(s)>
For example:
git add analysis.R
gert::git_add('analysis.R')
Examples
.RData
all files ending in .RData.pdf
all files ending in .pdfdata/
the entire folder called datagit commit -m 'Initial commit'
## [main (root-commit) 48f0b69] Initial commit ## 1 file changed, 3 insertions(+) ## create mode 100644 analysis.R
gert::git_commit('Initial commit')
x=seq(1, 10, len=1) y=40*2 + rnorm(10,0,5) plot(x,y) summary(x)
## [main 6937474] Added summary for x ## 1 file changed, 1 insertion(+)
analysis.R
x=seq(1, 10, len=1) y=40*2 + rnorm(10,0,5) plot(x,y)
summary.R
summary(x) summary(y)
## [main a2c1d74] Added summaries for x and y ## 2 files changed, 2 insertions(+), 1 deletion(-) ## create mode 100644 summary.R
git log --oneline --graph --decorate
## * a2c1d74 (HEAD -> main) Added summaries for x and y ## * 6937474 Added summary for x ## * 48f0b69 Initial commit
gert::git_log(max=10)
## commit author ## 1 a2c1d74517edac69e236d9fbb4e92f8da9ef4abc Murray Logan <i.obesulus@gmail.com> ## 2 69374744bf5c47820a30f7e39e6da820783c2800 Murray Logan <i.obesulus@gmail.com> ## 3 48f0b692636256597a31e0f1d550e7ffb3bdeb10 Murray Logan <i.obesulus@gmail.com> ## time files merge message ## 1 2022-12-06 09:25:35 2 FALSE Added summaries for x and y\n ## 2 2022-12-06 09:25:35 1 FALSE Added summary for x\n ## 3 2022-12-06 09:25:34 1 FALSE Initial commit\n
A listing (data.frame) of tracked files
gert::git_ls()
## path filesize modified created ## 1 analysis.R 53 2022-12-06 09:25:35 2022-12-06 09:25:35 ## 2 summary.R 22 2022-12-06 09:25:35 2022-12-06 09:25:35
git tag -a <tag> -m <message>
For example:
git tag -a 'V.1' -m 'Version 1'
gert::git_tag_create(name='V1', message='Version 1')
checkout
reset
revert
git checkout #
# is a commit or tag name
git checkout 6937
gert::git_branch_create(branch = 'temp', ref = '6937', checkout = TRUE)
Restore the HEAD to the tip of main
git checkout main
## Previous HEAD position was 6937474 Added summary for x ## Switched to branch 'main'
R/Rstudio
gert::git_branch_checkout(branch = 'main')
git reset --hard #
# is a commit or tag name
cd ~/tmp/Test_repo git reset --hard 6937
gert::git_reset_hard(ref='6937')
Restore HEAD to the tag V.1
git reset --hard V.1
## HEAD is now at a2c1d74 Added summaries for x and y
gert::git_reset_hard(ref='V.1')
git revert HEAD --no-edit
## [main 614ba62] Revert "Added summaries for x and y" ## Date: Tue Dec 6 09:25:43 2022 +1000 ## 2 files changed, 1 insertion(+), 2 deletions(-) ## delete mode 100644 summary.R
git revert --no-commit HEAD git revert --no-commit HEAD~1 git commit -m 'Rolled back'
git checkout -b <Name>
For example
git checkout -b Experimental
We are on the new branch
x=seq(1, 10, len=1) y=40*2 + rnorm(10,0,5) plot(x,y) summary(x) mean(x)
Otherwise, create any kind of file (in the folder we just created)
git checkout <Name>
For example:
git checkout main
We are on the main branch
mean(c(1,2,3))
Otherwise, create any kind of file
git log --online --graph --decorate --all
git diff main <branch>
For example:
git diff main Experimental
## diff --git a/analysis.R b/analysis.R ## index 9b5dda9..660b1bc 100644 ## --- a/analysis.R ## +++ b/analysis.R ## @@ -2,3 +2,4 @@ x=seq(1, 10, len=1) ## y=40*2 + rnorm(10,0,5) ## plot(x,y) ## summary(x) ## +mean(x) ## diff --git a/test.R b/test.R ## deleted file mode 100644 ## index 0242716..0000000 ## --- a/test.R ## +++ /dev/null ## @@ -1 +0,0 @@ ## -mean(c(1,2,3))
git merge <Name>
For example:
cd ~/tmp/Test_repo git merge Experimental -m 'Merge main and Experimental'
## Merge made by the 'ort' strategy. ## analysis.R | 1 + ## 1 file changed, 1 insertion(+)
gert::git_branch_checkout('main') # ensure on main branch gert::git_merge(ref='Experimental')
Step 1. get a github account and verify email address
Step 2. create one (or more) Personal Access Tokens (PAT)
Step 3. create a remote repository on github
Step 4. push/pull between remote and local
usethis::create_github_token()
gitcreds::gitcreds_set()
gitcreds::gitcreds_get()
## <gitcreds> ## protocol: https ## host : github.com ## username: pcinereus ## password: <-- hidden -->
#gh::gh_whoami()
From exising git and Rstudio project
usethis::use_github()
If not using use_github()
…
If not using use_github()
…
cd ~/tmp/Test_repo git remote add origin https://github.com/pcinereus/Test.git git push -u origin main
gert::git_remote_add(url='https://github.com/pcinereus/Test.git') gert::git_remote_list()
Lets make a small change to one of the files..
mean(c(1,2,3)) sd(c(1,2,3))
Otherwise, create any kind of file
git push -u origin main
git clone <git name> <local name>
Before making any changes that you intend to push, it is advisable that you pull to get the latest from the remote
git pull -v origin main
Clone the Test repo of the person next to you
Make a change, commit, push
Pull the changes of your neighbour
Try making a branch