Git Workflow #1: Existing project, GitHub last

From xkcd.

Inspiration for this post

Over the coming months, I plan on sharing a series of Git workflow tutorials. Getting a Git project set up can be a pretty simple process, but depending on the order in which you do things, set-up can get complicated. This first tutorial addresses one of those fairly complicated situations.

The schema for these tutorials is loosely based on the incredible Git + R bible by Jenny Bryan, Happy Git and GitHub for the useR. The only difference is that I will be showing the workflow for setting up the project using the command line, rather than RStudio.

Existing project, GitHub last

This first tutorial addresses the following situation: you have a folder for your project on your computer already. You would like this folder to be associated with a GitHub repository, but you haven’t created this repository yet, or you’ve created the GitHub repository and haven’t connected it with your local folder [^1].

[^1] Throughout this tutorial, “local” refers to the copy of the repository on your computer. “Remote” refers to the copy on GitHub.

Make a Git repo

The first step in this process is to make your local project into a Git repository. Start by using the command-line to navigate to the folder for your project (using cd). Once you are in the proper folder, run:

git init

This command either creates a new Git repository, or, in our case, converts an existing project into a Git repository. If you want to read more about git init, particularly the difference between git init and git clone, Atlassian has a useful article.

Now, since you’ve made edits / added files into your project, you want to add and commit those changes using the following commands:

git add -A
git commit -m "test commit"

You can make the commit message whatever you want. Note, git add -A adds all files in your repository. If you only want to add certain files, you can add them by name. For example, if you only want to add the file test.txt:

git add test.txt
git commit -m "test commit"

Make a GitHub repo

Next, we need to create a GitHub repository for our project. Depending on your workflow, you may have already created this repository. If not, log in to GitHub. Click the green “New repository” button. Ideally, you want the name of your new repository to match the name of the folder on your computer (but if not, it’s not a big deal). You don’t really want to initialize the repository with a README, but if you do, you can work around this later.

Once the repository has been set up, you can copy the HTTPS URL to your clipboard using the green “Clone or Download” button.

Connect the GitHub repo to your project

Now that you have a GitHub repository and you have a Git repository on your computer, you want to connect the two. To do this, run the following in the shell, using the URL that you just copied from GitHub:

git remote add origin <YOUR URL HERE>

Note, you don’t need the < and > on either side of the URL, just paste the URL as is.

You now need to tell your computer what the upstream branch should be, i.e. what branch on GitHub to pull updates from.

git branch --set-upstream-to=origin/master master

Reconcile the remote and local repositories

Now, assuming that you have edits both on your computer and on the GitHub repository, you need to be able to reconcile these changes so that the two are the same. This is assuming that you have files added in the folder on your computer and also something like a README or .gitignore on GitHub. To fix this problem, you first need to pull the changes from the GitHub repository onto your computer:

git pull --allow-unrelated-histories

The --allow-unrelated-histories portion of the command allows you to merge these two repositories (the local and the remote) even though they don’t have a common ancestor (you created them independently and made changes to both independently).

Now, you can push the changes from your local repository up to GitHub:

git push
Avatar
Cecina Babich Morrow
Compass PhD Program

My research interests range from harnessing data to improve mental healthcare to understanding global patterns of macroecology.

comments powered by Disqus

Related