Two weeks ago, I joined a Data Science bootcamp and part of the learning outcomes for this week was independently navigating the Git Workflow to successfully upload a test project as part of the data portfolio to a GitHub repository.
Git is a version control system that runs locally on a personal computer and allows us to keep track of changes made to our projects. Git creates saved checkpoints knowns as commits that act as snapshots of the state of the projects.
GitHub is an online service where we can store Git repositories. While Git is installed and works locally on our personal computers, GitHub stores our repositories online and allows us to share and collaborate on projects
My project was a custom energy consumption segmentation analysis using k means. Other than local python, this project required additional installation of pandas, numpy for data processing and manipulation, scikit-learn for preprocessing, and matplotlib and seaborn for the data visualization.
The Git workflow happened in 4 distinct stages.
The Working Directory.
The working directory is the folder containing my project files where the terminal or Git is currently working from. These include:
- README.md file which is a description of the entire project written in raw markdown. It contains information about the project such as:
- What the project is about
- What dataset was used
- The tools used
- The analysis was performed
- Contents of each folder
- Important findings
- How another person can understand the project
- Data Folder which contains an excel spreadsheet named energy_consumption_data.csv that contained the consumer data.
- Scripts Folder which contains a K means python script named segmentation.py. Python script files typically end with the .py extension.
To create a working directory using Git Bash, I opened Gitbash on Desktop by clicking on the windows icon on the taskbar, typing in bash and clicking on Gitbash to open a new window. Once the window was open, I ran cd Desktop, to navigate to my Desktop where I created a new Folder Titled energy_consumption_analysis by running the command
mkdir energy_customer_data_analysis
The command cd means change directory and it allows you to navigate in and out of directories. cd allows you to enter a folder while cd .. allows you to move backward by exiting a directory or a folder into the parent directory. Inside energy_customer_data_analysis, I created two folders Data and Scripts simultaneously by running the mkdir command which allows you to create 2 folder simultaneously by listing two folder names separated by a space:
mkdir Data Scripts
command created two folders named Data and Scripts.
I also created the README.md file by running touch README.md in bash.
to make sure that I had created all the files and folders, I ran the ls command in bash which allowed me to see all the files and folders created in the working directory.
I copied the CSV file from my downloads into the Data folder then used bash to navigate into the Scripts folder by running cd Scripts since I was still inside my project folder. once inside the Scripts folder, I ran touch segmentation.py to create the python script file and then ran code . in bash to then open segmentation.py in VS Code.
After writing the python code in VS Code, I went back to bash and ran cd .. to exit the scripts folder and ran nano README.md to open the README file in Bash's text editor nano where i wrote the description for the project in Markdown. Once this was complete, Ctrl + O, then Enter to save then Ctrl + X to go back to bash. To see what's inside the README file, use cat README.md in bash to display text inside the README.md file and ls to inspect the project folder. Once satisfied with everything, we then move on to the next step.
The Staging Area.
The staging area is the preparation area for the next commit. Here, we Initialize Git, check changes and stage files. Before running any git commands, we check our location by running pwd . Pwd means print working directory and allows us to see the exact location we are working from.
Initialize git
Once we ascertain that we are in the correct project folder, we can initialize git by running the command
git init
which instructs Git to begin managing the project. When run successfully, we should see : `Initialized empty Git repository in (the working directory path)
We also run ls -la which allows us to see a .git folder which acts as Git's internal memory for the project.
Check Project Status
We check project status by running
git status
This allows git to show us exactly what is happening in our project and because this is a completely new project and repository that git has not started tracking, our project status is untracked.
Project Staging
We can add individual files in our directory by running git add <filename> to select the individual file or we can run git add . to add everything in the current directory. This moves the selected files from the working directory into the staging area.
We can check the status of our project by running git status and if the staging is successful, our selected files and folders should be listed in green.
The Commit.
We create the first commit by running
git commit -m "Add Energy Consumption Segmentation Analysis Project"
git commit creates a saved Git checkpoint while -m means the message we want to attach to the commit which in this case is "Add Energy Consumption Segmentation Analysis Project"
The message explains what was saved. A good commit message creates an understandable history of the project.
Commit History
We can view the commit history by running
git log
which shows us the commit history or by running:
git log --oneline
for a much shorter commit history.
Creating the Github Repository.
- We can create a new repository in GitHub through our browser.
- Go to Github.com
- Sign in.
- Once Signed in Click the
+button. - Select: New repository
- Enter Repository Name(e.g.,
energy_consumer_data_segmentation_analysis) - You can also Add an optional description such as:(
energy_consumer_data_analysis_using_Python) - Choose between a Public or Private Repository (keep in mind that a public repository can generally be viewed by anyone and should not be used for sensitive or confidential data. In our case, the data is synthetic and non-specific so a public repository is fine.)
- Leave options such as: Add README, Add .gitignore, and Add license unchecked.
- Select Create New Repository.
After creating the repository, navigate to the connection options, Select SSH if you have it already configured.
The Push.
Pushing means sending committed changes on our projects to GitHub.
After copying the SSH string, return to Git Bash. To check whether we are still in the project, run
pwd
If still in the folder, run:
git remote add origin git@github.com:USERNAME/project-name.git
USERNAME: is your GitHub Username
Project-name.git: is the name of the repository you've just created.
type git remote add origin and then press Shift + Insert on your keyboard to paste the SSH string you've just copied from GitHub to complete the command and then run it.
Checking the Remote.
A remote is a connection between our local repository and another repository which is in our case, the one on GitHub.
We check the remote by running:
git remote -v
origin git@github.com:USERNAME/project-name.git (fetch)
means that Git can retrieve information from the remote repository
origin git@github.com:USERNAME/project-name.git (push)
means that git can send commits to the remote Repository.
Pushing the Code
Pushing means sending the committed changes in my project to the Remote Git Repository.
we do this by running:
git push -u origin main
Bash will prompt you to enter the configured passphrase. if you have one, type it in and then Enter
Verifying the Push
Return to the repository page on your browser and refresh it.
GitHub should render the contents in your README.md file visually.
You should also be in a position to see your files and the contents in those files (i.e the CSV file in the Data folder and the .py file in the Scripts folder).
If we can see this, then our project has successfully been pushed to GitHub.
Top comments (0)