Set project environment, Git version control and connect to GitHub from terminal prompt

Essential Anaconda Prompt commands to setup environment, git and connect with GitHub

Albert Kuc
14 min readJun 7, 2021
Anaconda Prompt
Anaconda Prompt

Setup environment, setup Git, setup GitHub. These could be covered in 3 separate articles, but the main focus here is on the terminal itself. My goal is to capture all conda/anaconda prompt essentials to manage a project.

1. Introduction

1.1. Initial motives to start using terminal

Recently I’ve been working through Python for DevOps book to level up my Python skills. In the early chapters it emphasises work in the terminal with some simple exercises. Additional practice using terminal gave me more confidence about using it. This planted a seed for the idea, that this might be the right time I should finally get more comfortable about using it.

Up until now, while I progressed learning Python using version control and environments, I have explored various methods and different kind of software. And of course I ended up using multiple IDE’s simultaneously. E.g. I write code and manage projects in PyCharm (earlier IntelliJ with Python plugin). But when it comes to the project environment I create and manage it using Anaconda. Then I reference to that environment again in PyCharm. It kept me programming because it worked, but as I write it now I can see how bad it looks like.

1.2. Additional inspirations

Before I move on to the main topic I would like to mention my YouTube mentors who encouraged me even further. I follow all three channels mentioned below and would recommend each if you are a self-learner like myself. Further down will reference some of the ideas from those videos.

Firstly I googled something like how to set environment in terminal and one of the first videos that showed up was one by Corey Schafer. I know his tutorials are very efficient, so I didn’t look any further.

Python Tutorial: How I Manage Multiple Projects, Virtual Environments, and Environment Variables

At the same time I came across another YT video, this time by TechWithTim, where Tim talks his point of view, on why it is beneficial to use Terminal over IDE to learn faster. There he explains, that often IDE makes your life easier, as you don’t necessarily need to know everything what’s happening in the background. Must admit I have learned myself the hard way, that this lack of understanding can lead you to a whole bunch of unexpected issues.

Should You STOP Using an IDE?

Lastly Luke Barousse, who is the latest addition to my subscription list. Just as I was about to begin taking notes, a fresh video by Luke popped up. There he walks you through how he sets up Git and uploads his project to GitHub. Below will be doing pretty much the same but with a different point of view.

Version control using Git and GitHub

2. Terminal essentials

I’m actually on a new device right now, hence required to run a few mandatory installations. The following instructions are captured from windows operating system point of view:

2.1. Miniconda3

Miniconda3 (download here) is a trimmed version of Anaconda which is a powerful data science toolkit and could be used alternatively (available here). Installing either of them will gain you access to the Anaconda Prompt, which will be required onwards.

At this point we have our terminal which is the Anaconda Prompt. Once opened it prints out a single line with the currently used default environment name (base) and the path where Miniconda3 was installed. This is everything we need to start using terminal.

(base) C:\Users\Albert>

2.2. Basic commands to operate inside prompt

Those would be essential to move within your drive/folder structure to the required location:

  • cls # clear screen
  • dir # displays all directories in the current location

#--- Change directory ---#

  • cd folder_name # changes directory to the specified folder location; folder must exist within the current location
  • cd.. # moves back to a lower level location. Try also cd\
  • cd /d c:\python\project # moves to specified location
  • d: # change drive to specified drive

#--- Make directory ---#

  • mkdir folder_name # creates a new folder in the current location
  • mkdir d:\existing_folder\new_folder1\new_folder2
    # the above creates folder in a folder inside the specified location

Python and conda specific commands:

  • python --version # prints python version (if python available)
  • where python # prints location of python.exe file(s) (if python available)
  • conda --version # prints conda version
  • conda --help # prints conda help message with useful conda instructions
  • conda info # prints details about conda, python and system
  • conda update -n base -c defaults conda update conda

Full list of conda commands available in this Conda Cheat Sheet

3. Environment setup

Initially conda prompt launches with the (base) environment as default. It has some packages already installed. At first no other envs exists.

  • conda env list # prints all available environments
    conda info --envs # alternative to print all environments
  • conda list # prints all packages and versions in the current environment
  • pip list # prints all python packages within the current env

The difference between pip and conda is that pip allows to install Python packages, while conda packages don’t have to be Python packages. For that reason conda list most likely returns a longer list of packages. In most scenarios using pip is enough but there might be some cases where you will need to use conda instead.

3.1. Create a new environment (step by step)

Create and switch to a new environment (try it yourself as below will be listed a command to delete environments as well):

conda create --name env_name  # creates an empty environment (1)
# requires confirmation to proceed conda activate env_name # launch env_name in the terminal (2)
conda install python # install latest python version (3)
# requires confirmation to proceed
  1. conda create --name env_name # creates an empty environment with a specified name: env_name
  2. conda activate env_name # activate command switch terminal to use the specified environment
  3. conda install python # The way we created our environment is such that it is completely empty at first and we need to install python. This will install the latest python version available for your conda version (alternatively to install a specific python version it can be specified in the command line, e.g. conda install python=3.6.0)
  4. (optional) pip install package_name # from here we can use pip to install python packages, e.g. pip install numpy (or a specific package version, e.g. pip install numpy==1.20.1)

Note: To specify a package version with pip use == while for conda use =.

At any given point try pip list and conda list to see what packages are installed.

The above example creates a new environment in a step by step pattern.
This procedure can be shortened and the following example will combine steps 1, 3, 4.

3.2. Create new environment (compact)

Below an alternative method to create a new environment, with packages installed in a single line. This will install the latest versions of python with pandas, matplotlib and a specific numpy version. (Additionally a long list of other packages will get installed as they are required by python, numpy, matplotlib and pandas).

conda create --name new_env python numpy=1.20.1 pandas matplotlibconda activate new_env

Now we can use again conda activate new_env to switch to the new_env and use pip list or conda list to see what packages are installed.

3.3. Other useful commands

  • pip install package_name --upgrade # upgrade package using pip
    pip install package_name -U # alternative package upgrade with pip
    conda update package_name # upgrade package using conda
  • pip uninstall package_name # self-explanatory
    conda remove package_name #
  • conda deactivate # returns to the previously active environment
  • conda remove --name env_name --all # removes the entire environment (requires the environment to be deactivated first)
  • conda --help # displays instructions for using conda, e.g. a list of conda parameters used above. Each has it’s own help description which could be accessed like the following example: conda install --help.

3.4. Document environment details

Once the project is finished we can delete our environment. Later in the future if we would like to go back to that project or for anybody else to access it, it is often essential to operate within identical environment. Even the same packages with a different versions might lead to an unexpected output. Therefore it is worth to keep track of package versions being used. For this purpose we create requirements.txt and environment.yml files, each dedicated to pip and Conda respectively.

The syntax here is pretty straightforward and the below two lines will end up generating both files:

pip freeze > requirements.txtconda env export > environment.yml

Additional resources to find out more about requirements.txt and environment.yml file:

3.5. Environment setup conclusion and further references

At this point we moved within our local drive using terminal, created a new project environment using conda and installed packages with pip and conda.

To learn more you can refer to Managing environments and Managing packages at conda.io.

4. Git

So far we have no version control, which means there is no track for changes inside the specified project repository. For that purpose we need to get Git installed.

To learn what is Git and how it works refer to this link: https://www.youtube.com/watch?v=aw14VK9sN2s&t=74s

Download and install git from git-scm.com (and follow recommended selections during the installation).
Note: your terminal will not find git installed until you restart the prompt.

  • git --version # prints git version or an error in case git is not installed

Note: (optional)
With Git installed you can now use terminal through Git Bash as well. At first it will not recognise conda. To fix this refer to section 3. Make conda run in Git Bash in this codecademy discussion. Going further will continue using Anaconda Prompt.

4.1. Initial git setup

Configure your name and email in git:

  • git config --global user.name "Your Name"
  • git config --global user.email your_email@example.com

You can now confirm either by running:
git config --global user.name and git config --global user.email .

Additional setup for git (optional):

5. Start a project

With conda environment management and git version control we have enough to manage our project locally.

Below will repeat the previous steps using an example. Once completed, in the section to follow, will connect to GitHub and move the project online.

Hand Tracking example project

For the purpose of this example I will follow up Hand Tracking tutorial shared on YouTube by freeCampCode.org as part of Advanced Computer Vision with Python.

Similar logic will apply to any other project.

5.1. Set project location

Create a new folder and direct terminal to that location

  • Method 1:
(base) C:\Users\Albert>d:  # switch drive(base) D:\>mkdir d:\Python\HandTracking
(base) D:\>cd Python\HandTracking
  • Method 2:
(base) C:\Users\Albert>mkdir d:\Python\HandTracking
(base) C:\Users\Albert>cd /d d:\python\handtracking

Note:
Creating folders is case sensitive but it is not for changing location.

5.2. Create a new environment and install packages

For the Hand Tracking project two packages are required: opencv-python and mediapipe.

  • Method 1 (unsuccessful):
conda create --name HandTracking python opencv-python mediapipe

The above resulted in PackageNotFoundError for both opencv-python and mediapipe as conda was not able to install the packages. What that means is that had to install them separately using pip.

  • Method 2:

Created a new environment first and installed packages using pip

conda create --name HandTracking pythonconda activate HandTrackingpip install opencv-python
pip install mediapipe

5.3. Create/modify a file for your project

At this point we can use any text editor to create and define a new .py file. This step is beyond the scope of this post, so feel free to proceed based on your preferences and carry on with the subsequent step when ready.

Example: initialise a raw text editor from terminal, such as Windows notepad to create/open a Python file:

notepad HandTrackingMin.py

The above will open an empty notepad file with specified name and if the file doesn’t exist, it will ask whether to create a new one. There we can update the file with some content and save using ctrl + s. A classic example would be print("Hello world"). Next in the prompt window execute it with python:

python HandTrackingMin.py

5.4. Initialise version control

Use git init to create a project structure inside the folder you are currently inside.

(HandTracking) d:\Python\HandTracking>git init

As a result a hidden folder structure will get created in the terminal location. Git will track all changes to your project from there.

git status  # returns the status of the project

git status outputs the current status of your project and if you followed along you will see a file or files listed as Untracked files.
(optional) Access more details about git status using git status --help or at https://git-scm.com/docs/git-status.

5.5. Commit changes

In order to commit, first we need to add to state all the files which should be captured. Either add a specific file using git add file_name.py or by adding all untracked files at once with git add -A.

git add HandTrackingMin.py

With git status we can differ here, that git now recognises the file as a new file under Changes to be committed.

To commit, a simple git commit with no other arguments opens vi editor for the purpose of writing a commit message. Alternatively this can be done in a single line using git commit -m "Commit message" although this is for short commit messages and not always allows to capture enough details.

Inside the vi text editor:
The first line of text is our commit title. Then there should be an empty line following with a remaining commit text. E.g.:

Initial commitAdded HandTrackingMin.py which access camera and detects hands in img

To exit and commit changes in Windows OS we need to hit Esc keyboard key and then type in :wq and hit Enter.

Learn more about commit messages here:

5.6. Track changes with git log

Now we can track our changes using git log:

(HandTracking) d:\Python\HandTracking>git log[master (root-commit) ab16631] Initial commit
1 file changed, 33 insertions(+)
create mode 100644 HandTrackingMin.py
(HandTracking) d:\Python\HandTracking>git log
commit ab1663193e25ee07c6b5ff419c75cb0a2ef88930 (HEAD -> master)
Author: Albert <albert@gmail.com>
Date: Wed Jun 2 16:19:49 2021 +0100
Initial commit Added HandTrackingMin.py which access camera and detects hands in img

And again using git status we now see there are no changes inside the repository.

6. Connect Git with GitHub

6.1. Initialise a new GitHub repository

Assume you are familiar with GitHub to be able to follow along below.
In case you need more guidance to set your GitHub repository refer to the video link: https://youtu.be/aw14VK9sN2s?t=510

  1. In your GitHub profile select Repositories and then New.
  2. Specify your project (repository) name, add a short description (optional) and click on Create repository. There are a few other optional selections which will ignore for now.

Here we have two options to choose:

  • clone GitHub repository to our drive
    Use this to clone (copy) any online repository from GitHub to your drive, which will also initialise Git in that location.
git clone https://github.com/username/repository_name.git
  • connect existing local Git repository with GitHub repository
    As we have local Git with commits already defined, we will use this option in section 6.3.

6.2. Master branch vs. Main branch

To learn about Git branches check this link:
https://www.toolsqa.com/git/branch-in-git/

At this point I realised there is a discrepancy between Git and GitHub. A newly created Git repository generated a default branch master, while GitHub refers to main branch nowadays. A little research shows that industry is moving away from master branch towards main branch, but Git seems to be a bit behind to introduce this change. This might not be necessary in the near future. Use git status to check your branch name. Without getting into further details will simply update my branch.

# rename branch
git branch -m master main

Then confirm using git status:

(HandTracking) d:\Python\HandTracking>git status
On branch main
nothing to commit, working tree clean

Update (2021/09/22): Modifying git branch for every new project is a bit annoying. Nowadays Git allows to specify the default branch name during installation. In already installed Git you are able to change the detault branch from the terminal itself (solution found here):

git config --global init.defaultBranch main 

6.3. Connecting local repository with GitHub

Use the following syntax pointing to your GitHub repository:

git remote add origin https://github.com/username/repo_name.git
git branch -M main
git push -u origin main
  • The first line creates a link between Git and GitHub. GitHub repository link can be obtained from the repository you have created a few steps earlier.
  • Second line is changing the branch to main. If you fallowed every step on the way you should already be working on main branch and this line should not make changes.
  • While with the first line you have established a connection between Git and GitHub, it is the git push command which triggers to update GitHub repository to match your local Git. At this step you will be asked to verify your identity by logging to your GitHub account. Having success with that, your local and online repositories will match. To verify check your GitHub repository using your browser to see that it captures all the local commits.

7. Further commits

  • Add to stage any modified or created file for commit git add FileName.py.
  • Check git status prior to commit to confirm every file is listed in the section Changes to be committed (green font).
    Note: any further change to a staged file will not be captured. E.g. if you added to stage a file at 10AM (but not committed) and modified it at 11AM, the version ready for commit is the one from 10AM. In this case git status will list the same file twice in Changes to be committed and Changes not staged for commit. To include the latest modifications to your future commit you need to add that file to stage once more.
  • git commit opens vi editor for new commit details
  • Now git status will notify that Your branch is ahead of ‘origin/master’ by 1 commit. This means that your local Git has more commits that GitHub
  • git push -u origin main update GitHub to match local Git

8. Conclusions

My aim in this article was to capture the most essential commands and steps related to the terminal prompt. Went through this procedure myself and had to investigate many questions on the way, while experimenting with some new knowledge. The above content initially started as notes for myself in a form of a notepad list capturing commands for conda, git and github and was later extended into a sort of a step by step journal. The order of steps is not set in stone, but in the combination presented above worked with no issues. Some details were omitted on purpose as they were beyond the scope, but external reference links were included.

Overall we have gone through all the basic commands required to use Anaconda Prompt terminal to setup and manage a project directory (step 2). Then we have learned some useful conda and pip commands to setup a new environment with miniconda/anaconda (step 3) and how install Git for version control (step 4). Next we have repeated the process with a real project example completing with a change commit (step 5). Finally we established connection with GitHub repository to push changes to our profile (step 6).

--

--