GitHub hosts Git repositories. Great backup for source code and other documents (like this presentation).
Two ways to access repositories on GitHub
HTTPS
git clone https://github.com/jknightlab/git-tutorial.git
SSH
git clone git@github.com:jknightlab/git-tutorial.git
Two ways to access repositories on GitHub
HTTPS
git clone https://github.com/jknightlab/git-tutorial.git
push
, pull
or fetch
Git can do this for you
git config --global credential.helper 'cache --timeout=36000'
SSH
git clone git@github.com:jknightlab/git-tutorial.git
Two ways to access repositories on GitHub
HTTPS
git clone https://github.com/jknightlab/git-tutorial.git
SSH
git clone git@github.com:jknightlab/git-tutorial.git
push
, pull
or fetch
command.ssh-agent
to take care of passwords.Tell Git your name and email address. These will be used to attribute commits.
git config --global user.name <your name>
git config --global user.email <your email>
If you are working on Windows also set this option
git config --global core.autocrlf true
Currently we are using the master
branch.
git status
List all existing branches
git branch -a
Switch to data-collection
branch
git checkout data-collection
Create a new file (using your favourite text editor).
We now have an untracked file in our repository.
git status
Add the file to the staging area
git add <your file>
Time to commit all staged changes (don't forget to add a descriptive commit message)
git commit -m "Added file with very important information"
Now we can push the new files to GitHub
git push
Before doing anything else, make sure your local repository is up to date.
git pull
Create a new branch (off the data-collection
branch).
git checkout -b analysis
Combine all the data (in R) ...
files <- dir(pattern=".txt")
data <- lapply(files, read.table)
data <- do.call(rbind, data)
names(data) <- "value"
names <- lapply(files, strsplit, ".", fixed=TRUE)
data$name <- sapply(names, sapply, "[[", 1)
write.table(data, file="combined.tab", row.names=FALSE)
and add it to the repository.
git add combined.tab
git rm *.txt
git commit -m "Combined data into single file"
When pushing a new branch for the first time we need to tell Git where it should go.
git push --set-upstream origin analysis
Let's plot the data
library(ggplot2)
data <- read.table("combined.tab", header=TRUE)
data$rank <- order(data$value)
ggplot(data, aes(y=value, x=rank)) + geom_point() + theme_bw()
ggsave("figure/combined.png")
and add the plot to the repository.
git add figure/combined.png
git commit -m "added plot of data"
git push
Maybe that plot could be improved?
ggplot(data, aes(y=value, x=rank)) + geom_point() +
geom_text(aes(label=name), hjust=0, vjust=0) +
theme_bw()
git add figure/combined.png
git commit -m "Added labels to data points."
git push
Can merge branches on GitHub (if there are no conflicts).
Branches that have been deleted on GitHub still exist in the local repository. Best to clean them up.
git pull
git branch --merged | grep -v "\*" | grep -v master | xargs -n 1 git branch -d
git pull --prune
Can always merge locally and then push to GitHub.
Here we merge data-collection
into master
git checkout master
git merge data-collection
and then delete the local branch and push everything to GitHub
git branch -d data-collection
git push
Finally, delete the remote branch as well.
git push origin --delete data-collection
GitHub provides many ways to explore the history of a project.
git clone
git add
git commit
git push
git pull
git status
git branch
git checkout
git merge
git rm
git reset
git stash
If working on a Linux machine that isn't automatically starting an ssh-agent instance this can be achieved by adding the following code to .profile
SSH_ENV="$HOME/.ssh/environment"
function start_agent {
echo "Initialising new SSH agent..."
/usr/bin/ssh-agent -s | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add;
}
# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
ps -ef | grep ${SSH_AGENT_PID} | grep "$(whoami).*ssh-agent\s" > /dev/null || {
start_agent;
}
else
start_agent;
fi
The ssh passphrase then only needs to be entered once when the ssh agent is started.
Social network