Repository navigation
Git
Migration in progress - do not use
[TOC]
Git is software which helps you to manage changes to your files. It works especially well with plain text files, like code. For a quick Git lookup, this guide is excellent. I strongly recommend working through the Software Carpentry Git course. Jenny Bryan's class on working with RStudio and Git is very good.
This wiki will take you through some usual tasks, like starting a new project, your day-to-day workflow and working with others.
Note: you can use tab to auto-complete directory and file names in the command line.
Starting a new project could mean two things:
- You have a totally blank canvas
- You have an existing project you want to use Git with.
This section will cover each of these.
You've created a new folder and are about to begin a project, what's your next step? First, have a think about folder structure; I like to have folders for data, code (repo) and docs, as a minimum. This blog post has some great ideas on structuring your work.
Depending on how you work and how you structure your project, you will have different options for what you're managing with Git.
If you are version controlling your whole project (not ideal with data, spreadsheets or Word-type files), you want your git directory in the main project folder.
If you are only version controlling code you can put the git directory in your code/repo folder.
The only difference is where you run the git init command.
First we create a new repository on Github using the "New" icon. You may want to assign an organisation as owner of the repo, rather than yourself. You're then presented with an overview pane, with two options at the bottom: "I have an existing project" and "I'm starting from scratch". The existing project option describes the approach if you have an existing Git project on your local machine and want to connect it to Github. The from scratch option helps you if you do not have a Git project, but have an empty folder. Follow the appropriate choice and you'll be set. If you have an existing project which doesn't use Git, then the next section is for you.
Do not worry about there being a right time to start using Git/Github, it's designed to help you develop!
This section will help if you have an existing project, which isn't using Git.
First, you create a remote mirror of your project in Github using the "New" icon. You may want to assign an organisation as owner of the repo, rather than yourself. Add a licence and README, then create the repository. Use the green code button and copy the https/ssh link provided.
# change directory (navigate) to the one you want your version controlled directory in.
cd Documents/project
# Clone (copy) the repository from Github
git clone [link you copied from the code button]
# Copy your existing work to the new directory
git add *
git commit -m "initial commit"
git push -u origin masterYou now have a version controlled folder on your computer and a mirror of it at Bitbucket. Have a read of the subsequent sections for how to work with these.
If you are working alone on a project, from a single computer, Git is really straightforward.
Your Git project exists on your local machine (using git init) and can also be mirrored on Bitbucket (or Github, Gitlab, etc.). See the previous section for more details.
There are three commands you need, and the following describes what they do:
-
git addtakes a snapshot of your file(s) -
git commitcopies that snapshot into your local file history -
git pushcopies that local file history to your remote (Bitbucket).
So to use these, you must first navigate to your repository folder with cd, and can then use them at will.
For example, you're adding a feature/function to a script and finish it, at this point you run git add, use git commit to record it with a message and finally git push to sync it with your remote repository. These are shown below:
git add file1.txt file2.R
git commit -m "your succinct description of change"
git push origin masterAs you're working in bash (the terminal language), you can use wildcards.
So git add * adds all files in that directory, git add *.R adds all files with an R extension.
If you want Git to ignore certain files (like login files for a database), you can do this with a .gitignore file.
Please read this lesson from Software Carpentry for detail.
Working across multiple computers presents an extra step.
When you complete work on one machine make sure you have pushed any changes to Bitbucket.
You can then use git pull origin master to bring those changes to your other computer.
These are the basic commands, you will likely need others and some of these are covered in the following sections.
This section describes how to work with others. It is split into two parts, with the first covering general interaction and the second explaining how to solve conflicts.
When you want to collaborate in Bitbucket, your team need access to the repository. By default, any new repository you create in the SRUC-LEES team will be visible to all within the LEES team (you can change this in settings), but they will not have write permission. To grant them write permission to your repository, open your repository overview page (for example), click the 'Send invitation' button, type their name and change their access permission.
They can now clone the repository to their local machine with the https URL at the top of the page. Their command will look something like this:
git clone https://mikerspencer@bitbucket.org/sruclees/help.gitThe above creates a directory called help and stores the files in there. If you keep your code in a directory called "repo", or want to clone into a specific folder, you can do this by name or using a "." (. means current directory), e.g.
git clone https://mikerspencer@bitbucket.org/sruclees/help.git .Your collaborator now has a complete copy of your project history on their machine. You both now need to push work from your local machines to the remote repository (Bitbucket), and periodically (e.g. each morning) pull changes that any collaborators may have made. This push/pull workflow is the same one you use if you work across multiple computers. At some point, you or your collaborator will push a change on the same line. In Git this is called a conflict, and the next section describes how you solve it. Otherwise, Git will merge together different versions for you.
If you commit files that shouldn't be there, you'll need to remove them from your history. Do this with BFG repo-cleaner.
