class: center, middle # Version Control with Git # Part 2 ### Tutorial for the SIAM CSE Conference, 2017 http://meetings.siam.org/sess/dsp_programsess.cfm?SESSIONCODE=61498 ### On Github: https://github.com/uwescience/git-tutorials-siamcse2017 ??? ### Tutorial for the SIAM CSE Conference, 2017 http://meetings.siam.org/sess/dsp_programsess.cfm?SESSIONCODE=61498 --- # .blue[Review] We assume you: - have Git installed - understand basics of working on a single branch - are comfortable with basic git commands, e.g. - `init, add, commit, status, log, checkout` ### .blue[For some tips, see:] - [part1.html](part1.html) - [Git Cheat Sheet](https://education.github.com/git-cheat-sheet-education.pdf) from https://education.github.com ### .blue[Other useful tools] - [gitk](https://git-scm.com/docs/gitk) The git repository browser - [atom](https://atom.io/) Atom editor developed by Github --- # .blue[Outline:] - branching - merging - merge conflicts - remotes - git push - git fetch, git pull - Setting up a new repo on Github - Forking a repository - Pushing to your fork and doing a pull request - Continuous integration --- # .blue[Create a repository for experimenting] ```bash mkdir git-tests cd git-tests git init ``` - Add a `README.md` file - Commit changes - Modify `README.md` - Commit changes Now `git log` should show two commits. --- # .blue[Branching] Branches allow you to work on multiple indpendent modifications simultaneously. A branch could be used for: - Adding a new feature - Fixing a bug - Trying out changes that you may not want to make permanent You can easily and instantly switch between branches to work with different sets of changes or choose which changes to keep permanently. [Figures from https://git-scm.com/book/en/v1/Git-Branching-What-a-Branch-Is] --- ```bash git branch testing ```
--- ```bash git checkout testing ```
--- ```bash vim test.py git add test.py git commit -m 'add a test' ```
--- ```bash git checkout master ```
--- ```bash vim README.md git add README.md git commit -m 'update README' ```
--- # .blue[Branching experiments] ### .red[Try this:] ```bash git checkout -b new-feature ``` - Modify `README.md` - Add a new file `file1.txt` - Commit changes - `git log`
- `git checkout master` - `git log` - Add a new file `file2.txt` - Commit changes
- `git checkout new-feature` - `git ls-files` If you have `gitk` installed, you can also use this to view tree. --- # .blue[Merging] See: - https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging - https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging --- # .blue[Merge experiment -- with no conflict] ### .red[Try this:] ```bash git checkout master git diff --stat new-feature git diff new-feature git merge new-feature ``` If you made distinct changes on the two branches (even to different parts of the same file) , this should merge fine, without any conflicts. Now ```bash git ls-files ``` should show both `file1.txt` and `file2.txt` and ```bash git log ``` shows the merged history. --- # .blue[Merge experiment with conflicts] ```bash git checkout -b another-feature master # branch from master ``` - modify README.md - git add and commit this file ```bash git checkout master ``` - modify README.md -- same lines as before, different change - git add and commit this file .red[Now try:] ```bash git merge another-feature ``` You should have a conflict reported. --- # .blue[Resolving merge conflicts] Edit the file with a conflict, you should see lines like this: ```bash some text that is fine <<<<<<< HEAD change made on master branch ======= change made on another-feature branch >>>>>>> another-feature more text that is fine ``` ### .red[Fix this file by hand and then save it] Then you can do: ```bash git add README.txt # indicate it has been fixed git commit -m "merging another-feature into master" ``` --- # .blue[Setting up a remote repository] .red[To list remotes:] ```bash git remote -v ``` - If you cloned the repository, you will see a remote named "origin". - If you started it with `git init`, you will have not remotes initially. .red[To add a new remote:] ```bash git remote add
``` The `
` could be: - another clone on the same machine - a clone on a server or your desktop - a repository on [Github](https://github.com), [Bitbucket](https://bitbucket.org/product), [Gitlab](https://gitlab.com), etc. https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes --- # .blue[Cloud hosting sites:] - Some popular options: [Github](https://github.com), [Bitbucket](https://bitbucket.org/product), [Gitlab](https://gitlab.com), etc. - Public vs. private - Cloud services provide issue trackers, wiki pages ### .blue[Workflow models] - Open source projects often requiring forking, pull requests - Managing branches for releases, development, bug fixes, - See e.g. https://git-scm.com/docs/gitworkflows ### Open source projects on GitHub: - Some sample repositories: [linux](https://github.com/torvalds/linux), [shablona](https://github.com/uwescience/shablona), [knitr](https://github.com/yihui/knitr) - "Organizations" with several repositories: [IPython](https://github.com/ipython), [Jupyter](https://github.com/jupyter), [Clawpack](https://github.com/clawpack) Explore the repositories, pull requests, issues, wiki, etc. --- # .blue[Contributing to a project on Github] We will use a temporary repository to play around with: ### .blue[View online:] https://github.com/rjleveque/git-sandbox ## .red[Fork this project:] Assuming you have a Github account, log in, navigate to the site above, and then click the "Fork" button at the top right. This takes you to a clone of the repository on your own Github account. Then click the "Clone or download" button and copy the text to clone it, e.g. ```bash git clone https://github.com/YourUserName/git-sandbox.git ``` --- # .blue[Things to try:] ```bash git clone https://github.com/YourUserName/git-sandbox.git cd git-sandbox ``` - Modify `README.txt` - Add and commit this change ```bash git push origin master ``` --- # Other things to try, time permitting: - Make an edit to `README.md` on Github, fetch and merge locally - Do a pull request - Merge a pull request - Disuss Travis and continuous integration, see https://github.com/rjleveque/git-sandbox/pull/1