FEEG6002 - Applied Programming 3 - Version Control and Git II Sam Sinayoko 2015-10-16 1 / 26
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Online repositories Conclusions Appendix 2 / 26
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Online repositories Conclusions Appendix 3 / 26
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 private 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 / 26
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Online repositories Conclusions Appendix 5 / 26
Working with a single repository Command Description git init Initialize a version controlled repository git add Add file(s) to repository (or staging area) git commit -a -m "Sth" Commit all changes (creates a new commit) git log List the commit history git checkout 5096a Revert the repository to commit 5096a git checkout -f 5096a Discard all changes and revert to 5096a git checkout master Revert to latest commit (= master) git show 5096a Display some info about commit 5096a git diff 5096a 730a3 Display the diff between commits 5096a and 7 master Latest commit (in the default master branch) HEAD The current commit HEAD^ Head s parent HEAD~i Head s i th parent 6 / 26
Original repository contents Change directory to last week s repository and checkout master 1 # cd /path/to/my/repos/feeg6002-git-basics git checkout master # make sure we ve got the latest commi 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 country is the best country in the world - Your city is the best city in the world - Your food is the best in the world 1 Alternatively see Appendix A for how to download a copy of the repository from Bitbucket. 7 / 26
Original repository log file git log commit 2b483859fceb1c19a9862593b61e1a99fcc0e39d Author: Sam Sinayoko <s.sinayoko@soton.ac.uk> Date: Thu Oct 15 16:29:36 2015 +0100 Nice guy commit bbed77870c10468aaa6a086feb6273628632e123 Author: Sam Sinayoko <s.sinayoko@soton.ac.uk> Date: Thu Oct 15 16:29:00 2015 +0100 France lover commit 7556a41f57860373ad7051eda1b6f0ac284b990c Author: Sam Sinayoko <s.sinayoko@soton.ac.uk> Date: Thu Oct 15 16:23:53 2015 +0100 8 / 26
Make some more changes and commit Let s add some details to wise.txt: - Your country is the best country in the world + welcoming people + great climate - Your city is the best city in the world + great parks + vibrant night life + beautiful architecture - Your food is the best in the world git commit -a -m "Add details on country and city." [master 2cb4f93] Add details on country and city. 1 file changed, 5 insertions(+) 9 / 26
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Online repositories Conclusions Appendix 10 / 26
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 / 26
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 / 26
(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 "Sam Sinayoko" 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@darkplace.com" 13 / 26
Make some (angry) changes to the new repository Edit "wise.txt" as follows: - Your country is NOT the best country in the world + unwelcoming people + terrible climate - Your city is NOT the best city in the world + dirty parks + boring night life + ugly architecture - Your food is NOT the best in the world Don t forget to commit: git commit -a -m "Mr Hyde is ANGRY tonight" 14 / 26
Pulling changes from another repository (git pull) In the oringal 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 orig Updating 2cb4f93..71e46cd Fast-forward wise.txt 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) Similarly, if "Sam Sinayoko" commits any new changes to the original repository, "Mr Hyde" can use: git pull to incorporate the commits to his repository. There is a similar command to push commits to a remote repository called git push. 2 2 However this is only possible when the the remote repository is not checked in the same branch 15 / 26
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Online repositories Conclusions Appendix 16 / 26
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 within the file one after the other, and the conflict must be dealt with manually. See Bonus Video on Blackboard. 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 / 26
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Online repositories Conclusions Appendix 18 / 26
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 authorize. Public repositories can be cloned by anyone. Private repositories use the secure protocols SSH or HTTPS. SSH requires further configuration steps so stick to HTTPS for now. 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. 19 / 26
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 wit \ because it s long git clone \ https://sinayoko@bitbucket.org/sinayoko/feeg6002-labs Cloning into feeg6002-labs... warning: You appear to have cloned an empty repository. Checking connectivity... done. 20 / 26
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 no need to initialize 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 21 / 26
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://sinayoko@bitbucket.org : Counting objects: 21, done.... To https://sinayoko@bitbucket.org/sinayoko/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 22 / 26
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Online repositories Conclusions Appendix 23 / 26
Conclusions Summary of the key commands git clone create local copy of a repository git pull pull changes from the original repository git push push changes to the original repository Use Bitbucket to create a private repository online for the labs Clone it locally with the https protocol, add your new programs for each lab, make commits and push your changes. 24 / 26
Outline Learning outcomes Working with a single repository (review) Working with multiple versions of a repository Advanced features Online repositories Conclusions Appendix 25 / 26
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://sinayoko@bitbucket.org/sinayoko/feeg6002-git-basics Cloning into feeg6002-git-basics... remote: Counting objects: 12, done. remote: Compressing objects: 100% (9/9), done. remote: Total 12 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (12/12), done. Checking connectivity... done. This will create a repository in your current folder called feeg6002-git-basics. 26 / 26