I feel so happy after creating my first repository on github and successfully adding all the files to it from my local machine!
It took me a lot of time and effort, and there were actually a few times when I thought that I wouldn’t be able to do it. It took me hours, and that is not how much it should have taken to just create a new repository.
I had earlier tried to create a repository using SSH clone and I wasn’t successful. Even this repository I have created is a HTTPS clone. I am yet to master a SSH clone repository. I get lost in the process after adding the SSH keys.
So here I’d like to document the whole procedure I used to create the repository (using HTTPS clone) and the problems I faced mid-way :-
First I went on my github profile and simply created a new repository with the name- Learn Python the Hard Way. This part is too easy and requires no real explanation.
Then I opened my terminal and followed the following procedure:-
1. Initialized the local repository (the directory where I hold all the python exercises) as a git repository.
jigsaw:Python$ git init
Initialized empty Git repository in /home/jigsaw/Python/.git/
2. Added the files from in the new local repository. This stages them for a commit to my git repository.
jigsaw:Python$ git add .
Note: At the end, I’ll also mention the difference between add . and add -A/–all
3. Checked the status of the files staged for a commit.
jigsaw:Python$ git status
On branch master
Changes to be committed:
(use “git rm –cached …” to unstage)
new file: Python File Modes
new file: Python Files I_O.pdf
new file: Untitled Document 1.py~
new file: ex1.py
new file: ex1.py~
new file: ex10.py
new file: ex10.py~
and so on..
4. Committed the files that I have staged to my git repository.
jigsaw:Python$ git commit -m “Required message”
[master (root-commit) e0954d9] First commit; Half way through Learn Python the Hard Way
51 files changed, 3488 insertions(+)
and so on..
5. Next I copied the HTTPS clone URL from the lower right hand corner of my repository on github and added it to the command as follows.
jigsaw:Python$ git remote add origin https://github.com/jigsaw2212/Learn-Python-The-Hard-Way.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘https://github.com/jigsaw2212/Learn-Python-The-Hard-Way.git’
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.
I was confused! I looked around, banged my head on the wall a few times and finally understood that what this error message was trying to tell me is that my local repo was not up-to-date with my master on github. This happened because I had added a README.md file upon creation of my repository on github, which made it out of sync with my local repo.
Hence, I first had to pull changes from the git repo to my local machine before pushing my whole project on to github.
jigsaw:Python$ git pull
jigsaw:Python$ git push origin master
And through this I was finally able to push all the scripts I had written in the exercises on to a new repository on github.
Link to repo- https://github.com/jigsaw2212/Learn-Python-The-Hard-Way
After that, I also created a branch for my project ( so that the master remains clean), using :-
jigsaw:Python$ git checkout -b LPTHW_Next_part
Means, create a new branch and move to it.
I can view the existing branches and the current branch I’m on using:-
jigsaw:Python$ git branch
Next time, I’ll try create using the SSH clone, so that I don’t have to log in again and again when I’m pushing files into it.