GitHub is extremely frustrating at times. Especially when you spend more time tweaking your settings rather than writing / committing code. I’m going to walk through a couple situations that I’ve ran into. If you’re looking for a broader, simple tutorial, the git – the simple guide fits that bill nicely as well.
Connect an Existing Directory to a New GitHub Repo
Assumptions: You have an existing directory with your code and you’re setting up your GitHub repository.
- If you haven’t already, create your first GitHub Repo.
- Personally, I would suggest you do not create the readme or license yet. Doing so will force you to pull these files before you can push your code.
- Set up your local directory by following the steps here.
- Boils down to…
- Initializing a git directory in your code directory: git init
- Setting up a remote connection to the git page.
- When setting up the remote repository URL, it should include the .git
- Assuming you did not create a readme or license, those instructions will work.
- If you received an error about not being in sync with the master branch, you probably created the readme and license files on github’s website.
- To fix this, you need to use git pull origin master
- This will pull all of the files on github to your local directory.
- You are now up-to-date and can start making changes to the master branch.
Add a Development Branch to a Git Repo
If you’re like me, you want to have a working piece of software but still keep tweaking. The mind-blowing fact about Git is that your branches act like folders for your code changes. However, Git actually makes different versions of the code available when you “checkout” a particular branch.
To set up my branches (and verify they’re working), I followed this tutorial on using Git branches. Here’s an abridged version.
- Add a new branch to your local repo by typing git branch branchname
- You now have a branch called “branchname”.
- Creating a new branch will copy all of your code from the branch you were in at the time of creating the branch.
- “Checkout” the dev branch in order to make changes to files.
- Run git checkout branchname
- You’ll see that it outputs Switched to branch ‘branchname’
- Make some changes to a file and add those changes.
- git add filename
- git commit -m “Commit message”
- Push the changes (and branch) to github.
- git push origin branchname
- Since it’s the first time you’re creating this branch, you’ll see something like * [new branch] branchname -> branchname
- Future pushes will look more like branchid..branchid branchname -> branchname
- Now, if you want to go back to using your master program, you’ll have to run git checkout master
Hopefully these simple steps will save your an hour or two of frustration when you’re setting up your github repository on your local environment.