Lo-Fi Python

Jan 25, 2020

"Git" The Basics: A Version Control Cheat Sheet

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 to 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

  1. Create new repository on GitHub
  2. Add a README
  3. Add a Python gitignore
  4. Add a license. Jake V. used an MIT license.

Clone Any Repository from GitHub

  1. Visit your new project, or any GitHub project, click the green "Clone or download" button and copy the link.
  2. In your terminal or command prompt, navigate to the directory where you want to clone your project.
  3. In terminal, enter: git clone REPO_URL
  4. Now cd into your project folder.

Push Your Local Computer Changes to the Remote Repository

Let's say you did some work on your computer and want to push the changes to GitHub. Enter these commands in terminal:

1
2
3
git add .
git commit -m "Add your commit note here"
git push origin main

Above: "git add ." stages all files in project directory for main. Add your commit message, then push your changes to the remote repo.

Fetch Changes From Main Branch to Your Local Computer

You might want to update your local computer with any main branch changes before beginning work on it. Enter these commands in terminal:

1
2
git fetch
git pull origin main

Review Merge Conflicts

Sometimes, your code may conflict with changes in the main 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:

1
git status

Then follow these instructions to review the merge conflicts.

Or maybe you want to discard any local changes, then merge:

1
2
3
git fetch
git checkout
git pull origin main

Recovering from a Corrupted Repository

1
git fsck --full --no-dangling

Additional Reading

Revert local repo to a previous commit, then push your repo to the remote.

1
2
git reset --hard rollback_commit_id
git push origin main

Supplementary Reading

CS Visualized: Useful Git Commands

8 Git Commands I Use Every Day

On Commit Messages

Pandas Library Git Workflows