Version control
Preparations
If you haven’t done so already, make sure that you do all preparatory steps described in Preparations
Working with git in SourceTree
Add your project directory to SourceTree
Click to see hints
-
Clone / New -> Create New Repository tab
- Repository Type: Git
- Destination path: path to your directory (can be selected by using the … button)
- Check the Bookmark this repository option
- Name: directory name (or what you want)
- At the next dialog, add your name and email address (preferable the same that you used when you registered your GitHub account)
- Check the Use these details for all repositories option@
Do an initial commit of your files
Click to see hints
- Open your project repository in SourceTree
- On the lefthand panel of your project, select FILE STATUS - Working Copy
- Select the files listed under ‘Unstaged files’ and click Stage selected
- Type an short and informative message in the commit message text field at the bottom, e.g.
Initial commit - Hit commit button
- (Expand the BRANCHES tab on the left, and select the master branch to see what was committed)
Edit one of the files, and commit the change
Click to see hints
- Select the changed file listed under ‘Unstaged files’, and view the changes that has been made
- Type an short and informative message in the commit message text field at the bottom, e.g.
Added more information about this or that - Hit commit button
- (Expand the Branches tab on the left, and select the master branch to see what was committed)
Add a file to your project that you don’t want to commit to git.
For example, this could be a dataset file that you don’t want to make public yet, e.g. my_project/data/raw/2016-11-16/bryceveg.R. Make git ignore this file.
Click to see hints
- Add a file somewhere in your project directory
- Right-click the recently added file (listed under ‘Unstaged files’), and select the Ignore… choice.
- Select an appropriate available option, and click OK
- Notice that a new file .gitignore has appeared. Add and commit that file to your repository.
Working with a GitHub remote repository
Create a new repository on GitHub
Click to see hints
- Go to GitHub, and log in to your account
- Click the Start a project or the New repository button
- Give the the repository the same name as the folder on your computer
- You can name it anything you want, but it’s usually easier to understand what’s going on if they are called the same thing
- Add a description if you want to
- Select Public or Private. (If you want to make it Private, what’s your reason for this?)
- Ignore the rest of the choices
- Click the Create repository button
- Give the the repository the same name as the folder on your computer
- Copy the link shown under Quick setup on the next page
- In SourceTree, click Settings (on the top right).
- then Remotes tab, Add button
- Check the Default remote choice
- In URL / Path, paste the link that you copied from GitHub
- Add your GitHub username
- To push your local repository state to the remote repository on GitHub, click the Push button in the toolbar at the top
- Check the master branch choice, and click the Push button
- Submit username and password in the Authenticate dialog that pops up
- You should now see two branch names in the commit history: master and origin/master
- Go back to GitHub and check that you can see your repository. (Click the name of the repository at the top, or the Code tab)
- Also notice that the file you told git to ignore in the previous task is not present in the remote repository
Make a change to your repository locally, e.g. by modifying or adding a file. Commit your changes to both the local and remote repositories
Click to see hints
- After changing some file, stage the file as usual.
- Check the Push changes immediately to origin/master choice below the commit message text field, Click the Commit button
Make a release of your repository on GitHub
This makes it easy for others to download the repository. You can use it yourself if you arrive at a directory structure that you want to reuse.
Click to see hints
- Go to your repository on GitHub
- Click where it says 0 releases
- Click Create a new release
- Tag the release, e.g.
v0.1, give it a title, and maybe a description - Click Publish release
Recover an earlier state.
- Delete one of the files in your working directory
- Commit the change
- Now try to recover the state before the file was deleted.
This is a bit tricky…
Click to see hints
- This can be done in many different ways…
- If you want to go back to a previous commit one step back, the easiest way is to
- Right-click the last commit and select Reverse commit
- Otherwise you can use branches:
- Check out a new branch (Branch button at the top)
- Base it on the commit that had the deleted file
- Name the branch to something useful, e.g. recover
- Check that you get the file back
- Change something in your working directory (e.g. add a space to a file)
- Note that the two branches have separated.
- Merge the recover branch with the master branch:
- Switch back to the master branch by double-clicking master under BRANCHES
- Click Merge at the top
- Select the latest commit on the recover branch, and click OK. Pray…
- Check out a new branch (Branch button at the top)