FEEG6002 - Applied Programming 3 - Version Control and Git II Richard Boardman, Sam Sinayoko 2016-10-19
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Conclusions Appendix 2/25
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Conclusions Appendix 3/25
Learning outcomes By the end of this lecture you should be able to: work with multiple copies of a Git repository git clone git pull git push create an account and a public repository for the C labs on Bitbucket to easily work on multiple computers keep your C programs for the labs / coursework under version control 4/25
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Conclusions Appendix 5/25
Working with a single repository Command git init git add git commit -a -m <msg> git log git checkout 5096a git checkout -f 5096a git checkout master git show 5096a git diff 5096a 730a3 master HEAD HEAD^ HEAD~i Description Initialise a version controlled repository Add file(s) to repository (or staging area) Commit all changes (creates new commit) List the commit history Revert the repository to commit 5096a Discard all changes and revert to 5096a Revert to latest commit (= master) Display some info about commit 5096a Display the diff between 5096a and 730a3 Latest commit (default master branch) The current commit Head s parent Head s i th parent 6/25
Original repository contents Change directory to last week s repo and checkout master # cd /path/to/my/repos/feeg6002-git-basics git checkout master # make sure we ve got the latest commit ls # list directory s contents Your branch is up-to-date with 'origin/master'. wise.txt There is only one file, wise.txt, whose content is cat wise.txt - Your programming language is the best one in the world - Your version control system is the best one in the world - Your favourite musician is the best one in the world Alternatively see Appendix A for how to download a copy of the repository from Bitbucket. 7/25
Original repository log file git log commit a4d01345c173363423ad094f276ee2d81dde0adb Author: Rich Boardman <rpb@soton.ac.uk> Date: Tue Aug 23 14:47:54 2016 +0100 Nice guy commit 624dc5830603ed0ffaecc60ce3a3a4c6f79ef35a Author: Rich Boardman <rpb@soton.ac.uk> Date: Tue Aug 23 14:47:19 2016 +0100 Python lover commit 10ed1319928197956330f039082d37134bf9d1ae Author: Rich Boardman <rpb@soton.ac.uk> Date: Tue Aug 23 14:42:52 2016 +0100 Initial commit: MATLAB lover 8/25
Make some more changes and commit Let s add some details to wise.txt: - Your programming language is the best one in the world + helpful community + great syntax Your version control system is the best one in the world + great support + bug-free + efficient transfers - Your favourite musician is the best one in the world git commit -a -m "Add details on language and VCS." [master 689d875] Add details on language and VCS. 1 file changed, 5 insertions(+) 9/25
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Conclusions Appendix 10/25
Cloning a repository (git clone) Motivation Working on the same program on multiple computers: each computer must have a copy, or clone, of the repository. Working on the same program with several people: each person must have a clone of the repository. Making extensive changes that are kept independent of your original repository. Syntax Make a copy of the repository in the current folder "git clone /path/to/original-repo" creates a copy of the entire repository in the current folder, with the same name. "git clone /path/to/original-repo new-repo" renames the repository to "new-repo" 11/25
Cloning a repository (git clone) Go to parent directory of original repository cd.. ls # (go back to parent directory) # (directory contents before cloning) feeg6002-git-basics Clone the repository git clone feeg6002-git-basics feeg6002-angry # (creates new folder feeg6002-angry ) Directory after cloning ls # (feeg6002-angry has appeared) feeg6002-angry feeg6002-git-basics 12/25
(Optional) Change the username in the new repository Let s change the user name to "Mr Hyde". This is just to distinguish him later from the original user "Rich Boardman" in the revision history. You wouldn t normally have to do this, since another user would already have a different name. cd feeg6002-angry git config user.name "Mr Hyde" git config user.email "mrhyde@example.org" 13/25
Make some (angry) changes to the new repository Edit "wise.txt" as follows: - Your programming language is NOT the best one in the world + unhelpful community + terrible syntax - Your version control system is NOT the best one in the world + dreadful support + buggy + hopelessly inefficient transfers - Your favourite musician is awful as well Don t forget to commit: git commit -a -m "Mr Hyde is ANGRY tonight" 14/25
Pulling changes from another repository (git pull) In the original repository we can pull the changes from Mr Hyde: cd../feeg6002-git-basics # go back to original repo git pull../feeg6002-angry # pull the changes into the original repo Updating 689d875..686d75f Fast-forward wise.txt 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) Similarly, if "Rich Boardman" commits any new changes to the original repository, "Mr Hyde" can use: git pull to incorporate the commits to this repository. There is a similar command to push commits to a remote repository called git push. 15/25
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Conclusions Appendix 16/25
Dealing with "simultaneous" revisions: merging If there are new commits in both the local repository and the remote one, git pull attempts to merge the two commits. This is only possible if there are not conflicts. If there is a conflict in a file, git puts the two possible versions one after the other and the conflict must be dealt with manually. You should use the command git status to find out whether you have dealt with all the changes. After dealing with all conflicts we can commit again. 17/25
Online repositories (Bitbucket & Github) Git repositories can be hosted for free on Bitbucket http://www.bitbucket.org Repositories can be public or private Private repositories are visible only by people you authorise. Public repositories can be cloned by anyone. Private repositories use the secure protocol SSH which we will cover in a later lecture. For now, keep your repository public. A popular alternative site is GitHub https://github.com, but it does not offer private repositories for free accounts. Just create a new repository on the website and follow the instructions to clone data to it. 18/25
TODO: Create a repository on Bitbucket for the labs Create an account on Bitbucket http://www.bitbucket.org Create a new repository for the labs called feeg6002-labs Use git clone to get a copy of your Bitbucket repository on your local computer. For example create a folder repos to store all your future repositories, change to that directory in the terminal and run # cd /path/to/repos # splitting line over two lines with \ because it s long git clone \ https://richboardman@bitbucket.org/richboardman/feeg6002-labs Cloning into 'feeg6002-labs'... warning: You appear to have cloned an empty repository. Checking connectivity... done. 19/25
TODO: Use your Bitbucket repository in the labs In the labs you can put the repos folder in your Documents folder Put your C programs in that folder and use Git to keep them under version control with: git add git commit -a -m etc. Note: there is need to initialise the repository because Bitbucket does that automatically. Create a folder for each lab in your folder repos/feeg6002-labs and add your programs to it 20/25
TODO: Push your commits to your Bitbucket repository At the end of each session (at home or in the lab), use git push to upload all your commits to Bitbucket git push Password for 'https://richboardman@bitbucket.org': Counting objects: 21, done.... To https://richboardman@bitbucket.org/richboardman/feeg6002-labs * [new branch] master -> master If you use a version of Git older than 2.0, the firs time you do this you ll get an error. Run this command to fix it before pushing again. git config --global push.default simple At the start of each lab using git pull to get an up to date copy of your repository 21/25
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Conclusions Appendix 22/25
Conclusions Summary of the key commands git clone git pull git push create local copy of a repository pull changes from the original repository push changes to the original repository Use Bitbucket to create a public repository online for the labs Clone it locally, add your new programs for each lab, make commits and push your changes. 23/25
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Conclusions Appendix 24/25
Appendix A: cloning the repository from lecture 2 I have created a public repository containing the repository from lecture 2. You can clone on your local machine by running this command # splitting command over two lines with \ git clone \ https://richboardman@bitbucket.org/richboardman/feeg6002-git-basics This will create a repository in your current folder called feeg6002-git-basics. 25/25