Monday, February 04, 2013

Github primer - pushing code remotely

Someone has recently asked me to give him a no-brainer procedure to push a git repository onto github. So here it is for everyone's convenience...



Assuming you have created a repo on your Github account, let's say you want to commit all your Ruby projects:

james@ebony ~/dev/ruby $ git init
Initialized empty Git repository in /home/james/dev/ruby/.git/
james@ebony ~/dev/ruby $ touch README
james@ebony ~/dev/ruby $ git add README
james@ebony ~/dev/ruby $ git add TaxCalculator/TaxCalculator.rb
james@ebony ~/dev/ruby $ git commit -m 'first commit'
[master (root-commit) a97909f] first commit
 1 file changed, 103 insertions(+)
 create mode 100644 README
 create mode 100755 TaxCalculator/TaxCalculator.rb
james@ebony ~/dev/ruby $ git remote add origin https://github.com/jamesattard/ruby.git
james@ebony ~/dev/ruby $ git push origin master
Username for 'https://github.com': jamesattard
Password for 'https://jamesattard@github.com': 
To https://github.com/jamesattard/ruby.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/jamesattard/ruby.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.
james@ebony ~/dev/ruby $ git pull origin master
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://github.com/jamesattard/ruby
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 README.md |    2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 README.md
james@ebony ~/dev/ruby $ git push origin master
Username for 'https://github.com': jamesattard
Password for 'https://jamesattard@github.com': 
To https://github.com/jamesattard/ruby.git
   2de0c70..c96770d  master -> master
james@ebony ~/dev/ruby $ ls
README  README.md  TaxCalculator
james@ebony ~/dev/ruby $ git rm README.md
rm 'README.md'
james@ebony ~/dev/ruby $ ls
README  TaxCalculator
james@ebony ~/dev/ruby $ git commit -m 'remove old README.md'
[master 1cf431a] remove old README.md
 1 file changed, 2 deletions(-)
 delete mode 100644 README.md
james@ebony ~/dev/ruby $ git push origin master
Username for 'https://github.com': jamesattard
Password for 'https://jamesattard@github.com': 
To https://github.com/jamesattard/ruby.git
   c96770d..1cf431a  master -> master

Follow the above procedure, note some common errors and also what you need to do if you want to remove a file from the repo the correct way.
Post a Comment