Save your work
The SWE waydevgitbasics
This post is for my friends who are in the Code In Place program. I suggest saving your work using a tool called
Git is a software for tracking file changes in a folder. We call each folder using git a git repository. Git is built for collaborative workflows so developers can work on the same repository without messing up each other’s stuff accidentally.
GitHub is the most popular online service that hosts git repositories online. A popular ELI18: GitHub is to git (repositories), what PornHub is to porn.
Install Git #
If you are using Windows, install Git for Windows here: https://git-scm.com/download/win
If you are using a Mac, install git using Homebrew by doing
brew install git
Set up a GitHub account #
Sign up here: https://github.com
Create a GitHub repository #
GitHub allows you to share your repository to the whole world (a.k.a. “open source” your code) by creating a public repository. If you rather be more selective about the viewership, create a private repository.
- Hit the
+button on the top right corner of the page and select
- Create a name for this repository. Let’s call it
- Add a one-sentence description for the repository.
- Choose your visibility preferences. You can always change it afterwards, so if you don’t know which one to pick yet, let’s go with
- Note down the
Remote URL(we’ll get back here shortly)
Make an existing folder a git repo #
Go to the folder where you stored all your code snippets. I assume you have a folder where you stored all your assignments as
.py files. If not, create one now!
We’ll be using the command line now - if you don’t know how to use it, just try! If you want to learn more before getting started, try this link.
- On Windows, right click your project folder in File Explorer, and select
Open Windows PowerShell here.
- On Mac, go to your project folder in Finder, right click and select
New Terminal at Folder. If you don’t see the option, you can activate it under
System Preferences > Keyboard > Shortcuts > Services.
Type the following to check the git version. This is a common way to find out if you have a command line tool (CLI) set up properly:
You should see something like
git version 2.31.1
Now, do this to make your folder a git repository:
What this command does is create a
.git folder in your folder. Do not touch this folder!
Note that you are now on a git branch called
Adding files to your git repo #
Now we will need to
add your files in the repository:
git add .
This command stages all the files (more accurately, all the tracked files) in your folder to be committed. Imagine you are in a restaurant, ordering food with an iPad; this step is analogous to adding food items to the virtual “basket”.
Now do this to
commit the files.
git commit -m “my first commit”
Committing - in our restaurant analogy - is sending your order to the kitchen. Note that the default option requires you to write a message for each commit.
To see your commit history, simply do
Note that each commit has a commit hash associated with it.
Under the hood, commits behave like snapshots. If you want to go back in time and restore your repository to what it is like at that snapshot, you can do
git checkout <commit_hash>.
Push your changes to GitHub #
You have committed your changes, but they are not on the cloud yet! You will need to specify where to upload your files to:
git remote add origin <remote_url>
remote URL from one of the previous steps?
Now that the remote URL is specified, go ahead and push it
git push -u origin master
This will upload all your changes on the master branch to GitHub.
And we are done! Your work is now saved to a git repository on GitHub!
Further exploration #
Try to complete the following tasks yourself:
- Modify a file. Add, commit, and push the changes to GitHub.
- Modify two files, but now add, commit, and push only one of them.
- Create a new branch called
yoloand delete all your files (except for the
.gitfolder). Commit the changes. Now, switch back to the
This is just the beginning of your git journey! To learn more, I recommend reading Atlassian’s git tutorials.