Git Use

This page was written to remind me how to create Git projects using GitHub. I always forget the commands, and so here I decided to document what I need to do. Another good resource is git – the simple guide.

Create the Repository

Create the repository inside the folder that your code is in

george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$ git init
Initialised empty Git repository in /home/george/Work/PhD/Contiki/Code/Channel Packet Loss/.git/
george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$

Add Files to the Respository

Then you can add files into the repository, like this:

george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$ git add *
george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$

Commit these Changes

To actually commit these changes use the following. Then the files are committed to the HEAD, but not in your remote repository yet.

george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$ git commit -m “Initial PDR code commit”
(root-commit) 83c117a Initial PDR code commit
6 files changed, 244 insertions(+)
create mode 100644 Makefile
create mode 100644 Receiver.c
create mode 100644 Transmitter.c
create mode 100755 parse_data.sh
create mode 100644 project-conf.h
george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$

Linking with Remote Server

If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with the following. Then you will be able to push your changes to the selected remote server

george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$ git remote add origin https://github.com/georgesmart1/contiki-packet-delivery-ratio.git
george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$

Pushing to Server

Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository, execute the following. Change master to whatever branch you want to push your changes to. Depending on if you have changed things in the newly created repository or not, you may get the following error:

george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$ git push origin master
Username for ‘https://github.com’: georgesmart1
Password for ‘https://georgesmart1@github.com’:
To https://github.com/georgesmart1/contiki-packet-delivery-ratio.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to ‘https://github.com/georgesmart1/contiki-packet-delivery-ratio.git’
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. ‘git pull’)
hint: before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.
george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$

Here, for example, there were differences between the README.md file and the LICENCE files. Do as suggested, and merge changes.

george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$ git pull https://github.com/georgesmart1/contiki-packet-delivery-ratio.git
warning: no common commits
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 19 (delta 5), reused 0 (delta 0)
Unpacking objects: 100% (19/19), done.
From https://github.com/georgesmart1/contiki-packet-delivery-ratio
* branch HEAD -> FETCH_HEAD
Merge made by the ‘recursive’ strategy.
LICENSE | 21 +++++++++++++++++++++
README.md | 25 +++++++++++++++++++++++++
2 files changed, 46 insertions(+)
create mode 100644 LICENSE
create mode 100644 README.md
george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$

From this you see that the two files were retrieved and the local copy contains all of the remote data. Now we can re-push the code, this time it should work, since the local contains an up to date version of the remote.

george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$ git push origin master
Username for ‘https://github.com’: georgesmart1
Password for ‘https://georgesmart1@github.com’:
Counting objects: 11, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 3.38 KiB, done.
Total 10 (delta 2), reused 0 (delta 0)
To https://github.com/georgesmart1/contiki-packet-delivery-ratio.git
12be5eb..6f0c4b0 master -> master
george@marconi:~/Work/PhD/Contiki/Code/Channel Packet Loss$

Success.

GitHub Push

An online scrapbook full of half-baked projects and silly ideas.