Syncing to a Remote Repository

A remote repository is just another copy of the git repository to which yours is linked. It can be on the same comuputer in a different file location, on a different computer, for example a departmental servr, 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

    • Business model is paid private repo hosting, public for free
    • Education allows 5 private repos
  • bitbucket

  • gitlab

    • can be on premis

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.

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

train31@myVM:~/git-lesson/workshop-test$ git config credential.helper cache

git remote

train31@myVM:~/git-lesson/workshop-test$ git remote
origin
train31@myVM:~/git-lesson/workshop-test$ git remote -v
origin  https://github.com/christopheredsall/workshop-test.git (fetch)
origin  https://github.com/christopheredsall/workshop-test.git (push)

git fetch

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

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 trhough 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

Exercise 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

Exercise 2

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

Exercise

  • 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