I am finally starting to understand git version control! It makes developing a project on different computers easy. Some of these notes were picked up from the super informative Reproducible Data Analysis in Jupyter video series by Jake VanderPlas, author of the Python Data Science Handbook.
First, go here, download and install git if you haven’t yet. Alright, here are my go-to steps and commands for git:
Beginning a New Project With Git
- Create new repository on GitHub
- Add a README
- Add a Python gitignore
- Add a license (Jake V. used an MIT license)
Clone Any Repository from GitHub
- Visit your new project, or any GitHub project, click the green “Clone or download” button and copy the link.
- In your terminal or command prompt, navigate to the directory where you want to clone your project.
- In terminal, enter:
git clone PASTE_URL_HERE
- Now cd into your project folder.
Push Your Local Computer Changes to the Master Repository
Let’s say you did some work on your computer and want to push the changes to GitHub. Enter these commands in terminal:
git add .
git commit -m "Add your commit note here"
git push origin master
*Above: git add .
= stage all files in project directory for master
**Substitute git add path\to\file\here
to stage only a single file to add to your master repository.
Fetch Changes From Master Branch to Your Local Computer
I like to update my local computer with any master branch changes before beginning work on it. Enter these commands in terminal:
git fetch
git pull origin master
Review Merge Conflicts
Sometimes, your code may conflict with changes in the master branch. You’ll find out if you try to push or pull changes and the auto-merge fails. Use “git status” to locate the files with the conflicts. Enter in your terminal:
git status
- Then follow these instructions to review the merge conflicts.
See also: Resolving Conflicts – Virtualenv Documentation
Or maybe you want to discard any local changes, then merge:
git fetch
git checkout .
git pull origin master
Recovering from a Corrupted Repository
git fsck --full --no-dangling
Supplementary Reading
CS Visualized: Useful Git Commands