Pushing Django code to GitHub

Pushing code to Github

I assume that you've installed Git. If not. follow this guide: Installing Git.  In this guide I use Linux system and terminal. There should be no difference for Mac Os users.  Windows users should use Git bash.

Create git repository inside your project

 1. Go to your project root directory and init git repository:

$ git init

2. After, you should have got similar directory tree with .git folder inside:

├── api/
├── .git/
├── media/
├── todo/
├── todowoo/
├── db.sqlite3
├── .gitignore
├── manage.py
├── Procfile
└── requirements.txt

3.  If you haven't do it before configure your git by running commands:

 $ git config --global user.name "Jacek Ka."
 $ git config --global user.email jacek.ka@example.com

Prepare project for pushing to Github

3. Hide sensitive data which are not going to be pushed into Github. In Django projects you should hide SECRET_KEY, database credentials, any tokens etc. How to do that you find here: Secret should be kept secret. You can use as well packages like django-environ or python-decouple.

4. Create requirements.txt file. It will be helpful to install all dependencies:

(todowoo) revorete@E7440:~/todowoo_project_drf$ pip freeze > requirements.txt

5. Create .gitignore in the root project directory and add there files not to track e.g. .evn files, .idea/ directory. To generate it would be a helpful gitignore.io site.  This file could look like:

### Django ###


6. Stage/track all files (without those in .gitignore):

$ git add .

    If you want to stage/track only one file:   

$ git add <filename> 

7. In case you want to unstage a file:

$ git rm --cached <filename>

8. Commit changes:

$ git commit -m "First commit"

Push code to Github

9. Create new repository on Github. Click new repository and create a public one: 

![create new repository on Github](upload://eM1EhNdTmpBPQH4DXdyMGeILn7a.png)

10. Go to terminal on a local computer and add a remote repository.  The git remote add command takes two arguments:

  • A unique remote name, for example “origin”
  • A remote URL, which you can find on the Code sub-tab of your Git repo

![url of a repository](upload://xd6TsWKFm54PnMIgJ8sG9XhXKWt.png) 

$ git remote add origin https://github.com/croolicjah/todowoo-project-drf.git 


$ git remote add origin git@github.com:croolicjah/todowoo-project-drf.git

 11. Finally, push changes:

$ git push -u origin master

Well done. Check Github. Your repository is there.