XClose

COMP0233: Research Software Engineering With Python

Home
Menu

Collaboration

NOTE: using bash/git commands is not fully supported on jupyterlite yet (due to single thread/process restriction), and the cells below might error out on the browser (jupyterlite) version of this notebook

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 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
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 30ceeb4] 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
* Ben Nevis
* Cairngorm
Writing Scotland.md
In [13]:
%%bash
ls
Scotland.md
index.md
lakeland.md
In [14]:
%%bash
git add Scotland.md
git commit -m "Add Scotland"
[main 36010d9] Add Scotland
 1 file changed, 6 insertions(+)
 create mode 100644 Scotland.md

One of you should now push with git push:

In [15]:
%%bash
git push
To github.com:UCL/github-example.git
   eeaf05c..36010d9  main -> main

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
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:UCL/github-example.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

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
   eeaf05c..36010d9  main       -> origin/main
 * [new branch]      gh-pages   -> origin/gh-pages
Merge made by the 'ort' strategy.
 Scotland.md | 6 ++++++
 1 file changed, 6 insertions(+)
 create mode 100644 Scotland.md

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
To github.com:UCL/github-example.git
   36010d9..212e4e6  main -> main

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
   36010d9..212e4e6  main       -> origin/main
Updating 36010d9..212e4e6
Fast-forward
 Wales.md | 5 +++++
 1 file changed, 5 insertions(+)
 create mode 100644 Wales.md
In [22]:
%%bash
ls
Scotland.md
Wales.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
Overwriting Wales.md
In [24]:
%%bash
git diff
diff --git a/Wales.md b/Wales.md
index f3e88b4..90f23ec 100644
--- a/Wales.md
+++ b/Wales.md
@@ -2,4 +2,4 @@ Mountains In Wales
 ==================
 
 * Tryfan
-* Yr Wyddfa
+* Snowdon
In [25]:
%%bash
git add Wales.md
git commit -m "Translating from the Welsh"
[main e59bb35] Translating from the Welsh
 1 file changed, 1 insertion(+), 1 deletion(-)
In [26]:
%%bash
git log --oneline
e59bb35 Translating from the Welsh
212e4e6 Merge branch 'main' of github.com:UCL/github-example
30ceeb4 Add wales
36010d9 Add Scotland
eeaf05c Add Helvellyn
684a361 Include lakes in the scope
be1d265 Add lakeland
9c8446b Add a lie about a mountain
4415406 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 add Wales.md
git commit -m "Add a beacon"
[main ab1254f] Add a beacon
 1 file changed, 2 insertions(+), 1 deletion(-)
In [30]:
%%bash
git log --oneline
ab1254f Add a beacon
212e4e6 Merge branch 'main' of github.com:UCL/github-example
30ceeb4 Add wales
36010d9 Add Scotland
eeaf05c Add Helvellyn
684a361 Include lakes in the scope
be1d265 Add lakeland
9c8446b Add a lie about a mountain
4415406 First commit of discourse on UK topography
In [31]:
%%bash
git push
To github.com:UCL/github-example.git
   212e4e6..ab1254f  main -> main

Switching back to the other partner...

In [32]:
os.chdir(partner_dir)
In [33]:
%%bash --no-raise-error
git push
To github.com:UCL/github-example.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:UCL/github-example.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
In [34]:
%%bash
git pull
From github.com:UCL/github-example
   212e4e6..ab1254f  main       -> origin/main
Auto-merging Wales.md
Merge made by the 'ort' strategy.
 Wales.md | 1 +
 1 file changed, 1 insertion(+)
In [35]:
%%bash
git push
To github.com:UCL/github-example.git
   ab1254f..4326076  main -> main
In [36]:
%%bash
git log --oneline --graph
*   4326076 Merge branch 'main' of github.com:UCL/github-example
|\  
| * ab1254f Add a beacon
* | e59bb35 Translating from the Welsh
|/  
*   212e4e6 Merge branch 'main' of github.com:UCL/github-example
|\  
| * 36010d9 Add Scotland
* | 30ceeb4 Add wales
|/  
* eeaf05c Add Helvellyn
* 684a361 Include lakes in the scope
* be1d265 Add lakeland
* 9c8446b Add a lie about a mountain
* 4415406 First commit of discourse on UK topography
In [37]:
os.chdir(working_dir)
In [38]:
%%bash
git pull
From github.com:UCL/github-example
   ab1254f..4326076  main       -> origin/main
Updating ab1254f..4326076
Fast-forward
In [39]:
%%bash
git log --graph --oneline
*   4326076 Merge branch 'main' of github.com:UCL/github-example
|\  
| * ab1254f Add a beacon
* | e59bb35 Translating from the Welsh
|/  
*   212e4e6 Merge branch 'main' of github.com:UCL/github-example
|\  
| * 36010d9 Add Scotland
* | 30ceeb4 Add wales
|/  
* eeaf05c Add Helvellyn
* 684a361 Include lakes in the scope
* be1d265 Add lakeland
* 9c8446b Add a lie about a mountain
* 4415406 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

git add Scotland.md
note left of P: git commit -m "Add scotland"
P->PR: create commit with Scotland file

git add Wales.md
note right of C: git commit -m "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 add Wales.md
git commit -m "Add another Beacon"
git push
[main dbb2bcd] Add another Beacon
 1 file changed, 1 insertion(+)
To github.com:UCL/github-example.git
   4326076..dbb2bcd  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 add Wales.md
git commit -m "Add Glyder"
git push
[main ff8a609] Add Glyder
 1 file changed, 1 insertion(+)
To github.com:UCL/github-example.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:UCL/github-example.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

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
   4326076..dbb2bcd  main       -> origin/main
Auto-merging Wales.md
CONFLICT (content): Merge conflict in Wales.md
Automatic merge failed; fix conflicts and then commit the result.

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
<<<<<<< HEAD
* Glyder Fawr
=======
* Fan y Big
>>>>>>> dbb2bcd1c499620a8ab0b08c500756e7d08f4259

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 add Wales.md
git commit --no-edit # I added a No-edit for this non-interactive session. You can edit the commit if you like.
[main 8e1c96e] Merge branch 'main' of github.com:UCL/github-example
In [50]:
%%bash
git push
To github.com:UCL/github-example.git
   dbb2bcd..8e1c96e  main -> main
In [51]:
os.chdir(working_dir)
In [52]:
%%bash
git pull
From github.com:UCL/github-example
   dbb2bcd..8e1c96e  main       -> origin/main
Updating dbb2bcd..8e1c96e
Fast-forward
 Wales.md | 1 +
 1 file changed, 1 insertion(+)
In [53]:
%%bash
cat Wales.md
Mountains In Wales
==================

* Pen y Fan
* Tryfan
* Snowdon
* Glyder Fawr
* Fan y Big
In [54]:
%%bash
git log --oneline --graph
*   8e1c96e Merge branch 'main' of github.com:UCL/github-example
|\  
| * dbb2bcd Add another Beacon
* | ff8a609 Add Glyder
|/  
*   4326076 Merge branch 'main' of github.com:UCL/github-example
|\  
| * ab1254f Add a beacon
* | e59bb35 Translating from the Welsh
|/  
*   212e4e6 Merge branch 'main' of github.com:UCL/github-example
|\  
| * 36010d9 Add Scotland
* | 30ceeb4 Add wales
|/  
* eeaf05c Add Helvellyn
* 684a361 Include lakes in the scope
* be1d265 Add lakeland
* 9c8446b Add a lie about a mountain
* 4415406 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 add Wales.md
note left of P: git commit -m "update wales.md"
P->PR: add commit to local repo
    
note right of C: git add Wales.md
note right of C: git commit -m "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!