Functions and Classes in Python

Syncing to a Remote Repository

Overview:

  • Teaching: 10 min
  • Exercises: 5 min

Questions

  • What are branches?
  • How should I use branches?
  • Once I've made my changes in the branch how do I merge them back?

Objectives

  • Know that branches can be used to compartmentalise different pieces of work.
  • Understand how and when to create branches.

A remote repository is just another copy of the git repository to which yours is linked. It can be on the same computer in a different file location, on a different computer, for example a departmental server, on the laptop of a collaborator who is also working with you or on a git hosting service.

Git Hosting

The main sites are

  • github

  • bitbucket

  • gitlab

    • can be on premise e.g. at Bath

Create a Repository on GitHub

You create a new repository by clicking the green "New Repository", give it a name, and optionally a description, licence, gitignore and README.

Once the repository has been set up click on the Clone button and copy the code required to clne the repository:

git clone https://github.com/<your-git-username>/<your-respository>.git

When you clone the repository make sure you don't clone into an existing repository!

Authentication

When we cloned the repository earlier that was a read only operation at the remote end. Now we are going to write to the remote repository we need to be authorised to do it. To avoid having to repeatedly put in the passowrd we'll ask git to cache the credentials

git config credential.helper cache

Alternatively you can use ssh keys to manage github authentication.

git remote

git remote informs you which remotes are set up for your local copy of the repository:

git remote
origin

and if you add the -v flag gives the full location of your remote repositories:

git remote -v
origin  https://github.com/christopheredsall/workshop-test.git (fetch)
origin  https://github.com/christopheredsall/workshop-test.git (push)

git fetch

Fetch copies a remote branch to your local machine, but does not merge it with your local branch.

git fetch
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
From https://github.com/christopheredsall/workshop-test
   582e532..765c3bb  master     -> origin/master

git pull

If you want to fetch and merge a remote then git pull does a git fetch followed by a git merge

git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
nothing to commit, working directory clean
git pull
Updating 582e532..765c3bb
Fast-forward
 file2.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 file2.md

git push

If we have made changes to our local repository we want to get them in to the remote. We use the git push command to do that. The first time through it will give you a warning about how the default behavour has changed. You can do what it suggests to get rid of the warning next time

git config --global push.default simple
git push
Username for 'https://github.com': christopheredsall
Password for 'https://christopheredsall@github.com': 
Counting objects: 3, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 320 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/christopheredsall/workshop-test.git
   765c3bb..e031f0f  master -> master

Exercises

1

  • create a repository on github
  • clone the repository
git clone https://github.com/christopheredsall/workshop-test.git
  • find the name of the remote repository

It is called origin by default

git remote
  • find the URL of the remote repository
git remote -v

2

  • Edit a file in the github interface and commit it
  • pull the commit to you local repository
git pull

3

  • make a local change and commit it
nano new.md
git add new.md
git commit -m "some new work from my laptop"
  • push to github
git config --global push.default simple
git push

Key Points:

  • You can make use of free git hosting services to backup and simplify collaborating with your colleagues.
  • Some organisations also provide private hosted solutions e.g Bath github
  • If you are working with remotes fetch and merge changes with git pull
  • Once you have merged with your changes you can git push changes to the remote.