Working with multiple remotes

Please open notebook rsepython-s1r10.ipynb

Distributed versus centralised

Older version control systems (cvs, svn) were “centralised”; the history was kept only on a server, and all commits required an internet.

Centralised Distributed
Server has history Every user has full history
To access history, need internet History always available
cvs, subversion(svn)git, mercurial (hg), bazaar (bzr)

With modern distributed systems, we can add a second remote. This might be a personal *fork* on github:

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(working_dir)

—————————————————————————

OSError Traceback (most recent call last)

in () </span>

3 git_dir = os.path.join(top_dir, ‘learning_git’)

4 working_dir=os.path.join(git_dir, ‘git_example’)

—-> 5 os.chdir(working_dir)

OSError: [Errno 2] No such file or directory: ‘/Users/uclrits/rsd-python/sec1git/learning_git/git_example/learning_git/git_example’

To create the additional remote:

%%bash
git checkout master
git remote add rpp-example git@github.com:rpp-example/github-example.git
git remote -v

Your branch is ahead of ‘origin/master’ by 1 commit.

rpp-example git@github.com:rpp-example/github-example.git (fetch)

rpp-example git@github.com:rpp-example/github-example.git (push)

local_bare ../bare_repo (fetch)

local_bare ../bare_repo (push)

span style="color:blue; font-family:monospace">    origin	git@github.com:UCL/github-example.git (fetch) </span>

origin git@github.com:UCL/github-example.git (push)

Switched to branch ‘master’

fatal: remote rpp-example already exists.

In this instance, as we already have a remote ‘rpp-example’ the command has failed.

We can make changes:

%%writefile Pennines.md

Mountains In the Pennines
========================

* Cross Fell
* Whernside

Overwriting Pennines.md

%%bash
git commit -am "Add Whernside"

[master 2ec725a] Add Whernside

1 file changed, 2 insertions(+), 1 deletion(-)

and push them to the new remote.

%%bash
git push -uf rpp-example master

Branch master set up to track remote branch master from rpp-example.

To git@github.com:rpp-example/github-example.git

+ 0e8ce67…2ec725a master -> master (forced update)

Referencing remotes

You can always refer to commits on a remote like this:

%%bash
git fetch
git log --oneline --left-right rpp-example/master...origin/master

< 2ec725a Add Whernside

< a19da7c Add github pages YAML frontmatter

From github.com:rpp-example/github-example

* [new branch] gh-pages -> rpp-example/gh-pages

To see the differences between remotes, for example.

To see what files you have changed that aren’t updated on a particular remote, for example:

%%bash
git diff --name-only origin/master

Pennines.md

index.md

When you reference remotes like this, you’re working with a cached copy of the last time you interacted with the remote.

You can do git fetch to update local data with the remotes without actually pulling.

You can also get useful information about whether tracking branches are ahead or behind the remote branches they track:

%%bash
git branch -vv

gh-pages fb21ea4 Add Whernside

* master 2ec725a [rpp-example/master] Add Whernside

Hosting Servers

Hosting a local server

bare_dir=os.path.join(git_dir, 'bare_repo')
os.chdir(git_dir)

—————————————————————————

OSError Traceback (most recent call last)

in () </span>

1 bare_dir=os.path.join(git_dir, ‘bare_repo’)

—-> 2 os.chdir(git_dir)

OSError: [Errno 2] No such file or directory: ‘/Users/uclrits/rsd-python/sec1git/learning_git/git_example/learning_git’

Create and initialise a bare repository:

%%bash
mkdir -p bare_repo
cd bare_repo
git init --bare

Initialized empty Git repository in /Users/uclrits/rsd-python/sec1git/learning_git/git_example/bare_repo/

os.chdir(working_dir)

—————————————————————————

OSError Traceback (most recent call last)

in () </span>

—-> 1 os.chdir(working_dir)

OSError: [Errno 2] No such file or directory: ‘/Users/uclrits/rsd-python/sec1git/learning_git/git_example/learning_git/git_example’

%%bash
git remote add local_bare ../bare_repo
git push -u local_bare master

fatal: remote local_bare already exists.

fatal: You are pushing to remote ‘local_bare’, which is not the upstream of

your current branch ‘master’, without telling me what to push

to update which remote branch.

%%bash
git remote -v

rpp-example git@github.com:rpp-example/github-example.git (fetch)

rpp-example git@github.com:rpp-example/github-example.git (push)

local_bare ../bare_repo (fetch)

local_bare ../bare_repo (push)

origin git@github.com:UCL/github-example.git (fetch)

origin git@github.com:UCL/github-example.git (push)

You can now work with this local repository, just as with any other git server.

If you have a colleague on a shared file system, you can use this approach to collaborate through that file system.

Next: Reading - Rebasing