XClose

COMP0233: Research Software Engineering With Python

Home
Menu

Collaboration

Form a team

Now we're going to get to the most important question of all with Git and GitHub: working with others.

Organise into pairs. You're going to be working on the website of one of the two of you, together, so decide who is going to be the leader, and who the collaborator.

Giving permission

The leader needs to let the collaborator have the right to make changes to his code.

In GitHub, go to Settings on the right, then Collaborators & teams on the left.

Add the user name of your collaborator to the box. They now have the right to push to your repository.

Obtaining a colleague's code

Next, the collaborator needs to get a copy of the leader's code. For this example notebook, I'm going to be collaborating with myself, swapping between my two repositories. Make yourself a space to put it your work. (I will have two)

In [1]:
import os
top_dir = os.getcwd()
git_dir = os.path.join(top_dir, 'learning_git')
working_dir = os.path.join(git_dir, 'git_example')
os.chdir(git_dir)
In [2]:
%%bash
pwd
rm -rf github-example # cleanup after previous example
rm -rf partner_repo # cleanup after previous example
/home/runner/work/rsd-engineeringcourse/rsd-engineeringcourse/ch00git/learning_git

Next, the collaborator needs to find out the URL of the repository: they should go to the leader's repository's GitHub page, and note the URL on the top of the screen. Make sure the "ssh" button is pushed, the URL should begin with git@github.com.

Copy the URL into your clipboard by clicking on the icon to the right of the URL, and then:

In [3]:
%%bash
pwd
git clone git@github.com:UCL/github-example.git partner_repo
/home/runner/work/rsd-engineeringcourse/rsd-engineeringcourse/ch00git/learning_git
Cloning into 'partner_repo'...
In [4]:
partner_dir = os.path.join(git_dir, 'partner_repo')
os.chdir(partner_dir)
In [5]:
%%bash
pwd
ls
/home/runner/work/rsd-engineeringcourse/rsd-engineeringcourse/ch00git/learning_git/partner_repo
Scotland.md
index.md
lakeland.md

Note that your partner's files are now present on your disk:

In [6]:
%%bash
cat lakeland.md
Lakeland  
========   
  
Cumbria has some pretty hills, and lakes too

Mountains:
* Helvellyn

Nonconflicting changes

Now, both of you should make some changes. To start with, make changes to different files. This will mean your work doesn't "conflict". Later, we'll see how to deal with changes to a shared file.

Both of you should commit, but not push, your changes to your respective files:

E.g., the leader:

In [7]:
os.chdir(working_dir)
In [8]:
%%writefile Wales.md
Mountains In Wales
==================

* Tryfan
* Yr Wyddfa
Writing Wales.md
In [9]:
%%bash
ls
Wales.md
__pycache__
index.md
lakeland.md
wsd.py
In [10]:
%%bash
git add Wales.md
git commit -m "Add wales"
[main 39740ea] Add wales
 1 file changed, 5 insertions(+)
 create mode 100644 Wales.md

And the partner:

In [11]:
os.chdir(partner_dir)
In [12]:
%%writefile Scotland.md
Mountains In Scotland
==================

* Ben Eighe
* Cairngorm
Overwriting Scotland.md
In [13]:
%%bash
ls
Scotland.md
index.md
lakeland.md
In [14]:
%%bash
git add Scotland.md
git commit -m "Add Scotland"
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
Cell In[14], line 1
----> 1 get_ipython().run_cell_magic('bash', '', 'git add Scotland.md\ngit commit -m "Add Scotland"\n')

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/IPython/core/interactiveshell.py:2478, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
   2476 with self.builtin_trap:
   2477     args = (magic_arg_s, cell)
-> 2478     result = fn(*args, **kwargs)
   2480 # The code below prevents the output from being displayed
   2481 # when using magics with decodator @output_can_be_silenced
   2482 # when the last Python token in the expression is a ';'.
   2483 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/IPython/core/magics/script.py:153, in ScriptMagics._make_script_magic.<locals>.named_script_magic(line, cell)
    151 else:
    152     line = script
--> 153 return self.shebang(line, cell)

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/IPython/core/magics/script.py:305, in ScriptMagics.shebang(self, line, cell)
    300 if args.raise_error and p.returncode != 0:
    301     # If we get here and p.returncode is still None, we must have
    302     # killed it but not yet seen its return code. We don't wait for it,
    303     # in case it's stuck in uninterruptible sleep. -9 = SIGKILL
    304     rc = p.returncode or -9
--> 305     raise CalledProcessError(rc, cell)

CalledProcessError: Command 'b'git add Scotland.md\ngit commit -m "Add Scotland"\n'' returned non-zero exit status 1.

One of you should now push with git push:

In [15]:
%%bash
git push
Everything up-to-date

Rejected push

The other should then push, but should receive an error message:

In [16]:
os.chdir(working_dir)
In [17]:
%%bash --no-raise-error
git push
To github.com:UCL/github-example.git
   c46bbc2..39740ea  main -> main

Do as it suggests. However, we need first to tell git how we want it to act when there are diverging branches (as in this case). We will set the default to be to create a merge commit, then we proceed to pull.

In [18]:
%%bash
git config --global pull.rebase false
git pull
From github.com:UCL/github-example
 * [new branch]      gh-pages   -> origin/gh-pages
 * [new branch]      master     -> origin/master
Already up to date.

Merge commits

A window may pop up with a suggested default commit message. This commit is special: it is a merge commit. It is a commit which combines your collaborator's work with your own.

Now, push again with git push. This time it works. If you look on GitHub, you'll now see that it contains both sets of changes.

In [19]:
%%bash
git push
Everything up-to-date

The partner now needs to pull down that commit:

In [20]:
os.chdir(partner_dir)
In [21]:
%%bash
git pull
From github.com:UCL/github-example
   c46bbc2..39740ea  main       -> origin/main
Already up to date.
In [22]:
%%bash
ls
Scotland.md
index.md
lakeland.md

Nonconflicted commits to the same file

Go through the whole process again, but this time, both of you should make changes to a single file, but make sure that you don't touch the same line. Again, the merge should work as before:

In [23]:
%%writefile Wales.md
Mountains In Wales
==================

* Tryfan
* Snowdon
Writing Wales.md
In [24]:
%%bash
git diff
In [25]:
%%bash
git commit -am "Translating from the Welsh"
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	Wales.md

nothing added to commit but untracked files present (use "git add" to track)
---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
Cell In[25], line 1
----> 1 get_ipython().run_cell_magic('bash', '', 'git commit -am "Translating from the Welsh"\n')

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/IPython/core/interactiveshell.py:2478, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
   2476 with self.builtin_trap:
   2477     args = (magic_arg_s, cell)
-> 2478     result = fn(*args, **kwargs)
   2480 # The code below prevents the output from being displayed
   2481 # when using magics with decodator @output_can_be_silenced
   2482 # when the last Python token in the expression is a ';'.
   2483 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/IPython/core/magics/script.py:153, in ScriptMagics._make_script_magic.<locals>.named_script_magic(line, cell)
    151 else:
    152     line = script
--> 153 return self.shebang(line, cell)

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/IPython/core/magics/script.py:305, in ScriptMagics.shebang(self, line, cell)
    300 if args.raise_error and p.returncode != 0:
    301     # If we get here and p.returncode is still None, we must have
    302     # killed it but not yet seen its return code. We don't wait for it,
    303     # in case it's stuck in uninterruptible sleep. -9 = SIGKILL
    304     rc = p.returncode or -9
--> 305     raise CalledProcessError(rc, cell)

CalledProcessError: Command 'b'git commit -am "Translating from the Welsh"\n'' returned non-zero exit status 1.
In [26]:
%%bash
git log --oneline
581eb0b Add Scotland
a873a29 Add Helvellyn
bc37e5f Include lakes in the scope
001011c Add lakeland
c693e31 Revert "Add a lie about a mountain"
124ae40 Change title
b6dc168 Add a lie about a mountain
902f791 First commit of discourse on UK topography
In [27]:
os.chdir(working_dir)
In [28]:
%%writefile Wales.md
Mountains In Wales
==================

* Pen y Fan
* Tryfan
* Snowdon
Overwriting Wales.md
In [29]:
%%bash
git commit -am "Add a beacon"
[main 456c4f5] Add a beacon
 1 file changed, 2 insertions(+), 1 deletion(-)
In [30]:
%%bash
git log --oneline
456c4f5 Add a beacon
39740ea Add wales
c46bbc2 Add Helvellyn
b964d3f Include lakes in the scope
a2c2d39 Add lakeland
a75b5e2 Revert "Add a lie about a mountain"
e439f8f Change title
34c7e4b Add a lie about a mountain
5a58c7b First commit of discourse on UK topography
In [31]:
%%bash
git push
To github.com:UCL/github-example.git
   39740ea..456c4f5  main -> main

Switching back to the other partner...

In [32]:
os.chdir(partner_dir)
In [33]:
%%bash --no-raise-error
git push
Everything up-to-date
In [34]:
%%bash
git pull
From github.com:UCL/github-example
   39740ea..456c4f5  main       -> origin/main
Already up to date.
In [35]:
%%bash
git push
Everything up-to-date
In [36]:
%%bash
git log --oneline --graph
* 581eb0b Add Scotland
* a873a29 Add Helvellyn
* bc37e5f Include lakes in the scope
* 001011c Add lakeland
* c693e31 Revert "Add a lie about a mountain"
* 124ae40 Change title
* b6dc168 Add a lie about a mountain
* 902f791 First commit of discourse on UK topography
In [37]:
os.chdir(working_dir)
In [38]:
%%bash
git pull
Already up to date.
In [39]:
%%bash
git log --graph --oneline
* 456c4f5 Add a beacon
* 39740ea Add wales
* c46bbc2 Add Helvellyn
* b964d3f Include lakes in the scope
* a2c2d39 Add lakeland
* a75b5e2 Revert "Add a lie about a mountain"
* e439f8f Change title
* 34c7e4b Add a lie about a mountain
* 5a58c7b First commit of discourse on UK topography
In [40]:
message="""
participant Palin as P
participant "Palin's repo" as PR
participant "Shared remote" as M
participant "Cleese's repo" as CR
participant Cleese as C

note left of P: git clone
M->PR: fetch commits
PR->P: working directory as at latest commit

note left of P: edit Scotland.md
note right of C: edit Wales.md

note left of P: git commit -am "Add scotland"
P->PR: create commit with Scotland file

note right of C: git commit -am "Add wales"
C->CR: create commit with Wales file

note left of P: git push
PR->M: update remote with changes

note right of C: git push
CR-->M: !Rejected change

note right of C: git pull
M->CR: Pull in Palin's last commit, merge histories
CR->C: Add Scotland.md to working directory

note right of C: git push
CR->M: Transfer merged history to remote

"""
from wsd import wsd
%matplotlib inline
wsd(message)
Out[40]:
No description has been provided for this image

Conflicting commits

Finally, go through the process again, but this time, make changes which touch the same line.

In [41]:
%%writefile Wales.md
Mountains In Wales
==================

* Pen y Fan
* Tryfan
* Snowdon
* Fan y Big
Overwriting Wales.md
In [42]:
%%bash
git commit -am "Add another Beacon"
git push
[main 4758fb8] Add another Beacon
 1 file changed, 1 insertion(+)
To github.com:UCL/github-example.git
   456c4f5..4758fb8  main -> main
In [43]:
os.chdir(partner_dir)
In [44]:
%%writefile Wales.md
Mountains In Wales
==================

* Pen y Fan
* Tryfan
* Snowdon
* Glyder Fawr
Overwriting Wales.md
In [45]:
%%bash --no-raise-error
git commit -am "Add Glyder"
git push
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	Wales.md

nothing added to commit but untracked files present (use "git add" to track)
Everything up-to-date

When you pull, instead of offering an automatic merge commit message, it says:

In [46]:
%%bash --no-raise-error
git pull
From github.com:UCL/github-example
   456c4f5..4758fb8  main       -> origin/main
Already up to date.

Resolving conflicts

Git couldn't work out how to merge the two different sets of changes.

You now need to manually resolve the conflict.

It has marked the conflicted area:

In [47]:
%%bash
cat Wales.md
Mountains In Wales
==================

* Pen y Fan
* Tryfan
* Snowdon
* Glyder Fawr

Manually edit the file, to combine the changes as seems sensible and get rid of the symbols:

In [48]:
%%writefile Wales.md
Mountains In Wales
==================

* Pen y Fan
* Tryfan
* Snowdon
* Glyder Fawr
* Fan y Big
Overwriting Wales.md

Commit the resolved file

Now commit the merged result:

In [49]:
%%bash
git commit -a --no-edit # I added a No-edit for this non-interactive session. You can edit the commit if you like.
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	Wales.md

nothing added to commit but untracked files present (use "git add" to track)
---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
Cell In[49], line 1
----> 1 get_ipython().run_cell_magic('bash', '', 'git commit -a --no-edit # I added a No-edit for this non-interactive session. You can edit the commit if you like.\n')

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/IPython/core/interactiveshell.py:2478, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
   2476 with self.builtin_trap:
   2477     args = (magic_arg_s, cell)
-> 2478     result = fn(*args, **kwargs)
   2480 # The code below prevents the output from being displayed
   2481 # when using magics with decodator @output_can_be_silenced
   2482 # when the last Python token in the expression is a ';'.
   2483 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/IPython/core/magics/script.py:153, in ScriptMagics._make_script_magic.<locals>.named_script_magic(line, cell)
    151 else:
    152     line = script
--> 153 return self.shebang(line, cell)

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/IPython/core/magics/script.py:305, in ScriptMagics.shebang(self, line, cell)
    300 if args.raise_error and p.returncode != 0:
    301     # If we get here and p.returncode is still None, we must have
    302     # killed it but not yet seen its return code. We don't wait for it,
    303     # in case it's stuck in uninterruptible sleep. -9 = SIGKILL
    304     rc = p.returncode or -9
--> 305     raise CalledProcessError(rc, cell)

CalledProcessError: Command 'b'git commit -a --no-edit # I added a No-edit for this non-interactive session. You can edit the commit if you like.\n'' returned non-zero exit status 1.
In [50]:
%%bash
git push
Everything up-to-date
In [51]:
os.chdir(working_dir)
In [52]:
%%bash
git pull
Already up to date.
In [53]:
%%bash
cat Wales.md
Mountains In Wales
==================

* Pen y Fan
* Tryfan
* Snowdon
* Fan y Big
In [54]:
%%bash
git log --oneline --graph
* 4758fb8 Add another Beacon
* 456c4f5 Add a beacon
* 39740ea Add wales
* c46bbc2 Add Helvellyn
* b964d3f Include lakes in the scope
* a2c2d39 Add lakeland
* a75b5e2 Revert "Add a lie about a mountain"
* e439f8f Change title
* 34c7e4b Add a lie about a mountain
* 5a58c7b First commit of discourse on UK topography

Distributed VCS in teams with conflicts

In [55]:
message="""
participant Palin as P
participant "Palin's repo" as PR
participant "Shared remote" as M
participant "Cleese's repo" as CR
participant Cleese as C

note left of P: edit the same line in wales.md
note right of C: edit the same line in wales.md
    
note left of P: git commit -am "update wales.md"
P->PR: add commit to local repo
    
note right of C: git commit -am "update wales.md"
C->CR: add commit to local repo
    
note left of P: git push
PR->M: transfer commit to remote
    
note right of C: git push
CR->M: !Rejected

note right of C: git pull
M->C: Make conflicted file with conflict markers
    
note right of C: edit file to resolve conflicts
note right of C: git add wales.md
note right of C: git commit
C->CR: Mark conflict as resolved

note right of C: git push
CR->M: Transfer merged history to remote

note left of P: git pull
M->SR: Download Cleese's resolution of conflict.
    
"""

wsd(message)
Out[55]:
No description has been provided for this image

The Levels of Git

In [56]:
message="""
Working Directory -> Staging Area : git add
Staging Area -> Local Repository : git commit
Working Directory -> Local Repository : git commit -a
Staging Area -> Working Directory : git checkout
Local Repository -> Staging Area : git reset
Local Repository -> Working Directory: git reset --hard
Local Repository -> Remote Repository : git push
Remote Repository -> Local Repository : git fetch
Local Repository -> Working Directory : git merge
Remote Repository -> Working Directory: git pull
"""

wsd(message)
Out[56]:
No description has been provided for this image

Editing directly on GitHub

Editing directly on GitHub

Note that you can also make changes in the GitHub website itself. Visit one of your files, and hit "edit".

Make a change in the edit window, and add an appropriate commit message.

That change now appears on the website, but not in your local copy. (Verify this).

Now pull, and check the change is now present on your local version.

Social Coding

GitHub as a social network

In addition to being a repository for code, and a way to publish code, GitHub is a social network.

You can follow the public work of other coders: go to the profile of your collaborator in your browser, and hit the "follow" button.

Check out the profiles of Linus Torvalds - creator of git (first git commit ever) and Linux - , Guido van Rossum - creator of Python -, or James Hetherington - the creator of these course notes.

Using GitHub to build up a good public profile of software projects you've worked on is great for your CV!